99from ably .http .paginatedresult import PaginatedResult , format_params
1010from ably .types .annotation import (
1111 Annotation ,
12+ AnnotationAction ,
1213 make_annotation_response_handler ,
1314)
1415from ably .types .message import Message
@@ -74,7 +75,7 @@ def construct_validate_annotation(msg_or_serial, annotation: dict | Annotation):
7475 elif isinstance (annotation , Annotation ):
7576 annotation_values = annotation .as_dict ()
7677 else :
77- annotation_values = annotation
78+ annotation_values = annotation . copy ()
7879
7980 annotation_values ['message_serial' ] = message_serial
8081
@@ -108,23 +109,27 @@ def __base_path_for_serial(self, serial):
108109 channel_path = '/channels/{}/' .format (parse .quote_plus (self .__channel .name , safe = ':' ))
109110 return channel_path + 'messages/' + parse .quote_plus (serial , safe = ':' ) + '/annotations'
110111
111- async def publish (self , msg_or_serial , annotation_values , params = None , timeout = None ):
112+ async def publish (
113+ self ,
114+ msg_or_serial ,
115+ annotation : dict | Annotation ,
116+ params : dict | None = None ,
117+ ):
112118 """
113119 Publish an annotation on a message.
114120
115121 Args:
116122 msg_or_serial: Either a message serial (string) or a Message object
117- annotation_values : Dict containing annotation properties (type, name, data, etc.)
123+ annotation : Dict containing annotation properties (type, name, data, etc.) or Annotation object
118124 params: Optional dict of query parameters
119- timeout: Optional timeout for the HTTP request
120125
121126 Returns:
122127 None
123128
124129 Raises:
125130 AblyException: If the request fails or inputs are invalid
126131 """
127- annotation = construct_validate_annotation (msg_or_serial , annotation_values )
132+ annotation = construct_validate_annotation (msg_or_serial , annotation )
128133
129134 # Convert to wire format
130135 request_body = annotation .as_dict (binary = self .__channel .ably .options .use_binary_protocol )
@@ -145,9 +150,14 @@ async def publish(self, msg_or_serial, annotation_values, params=None, timeout=N
145150 path += '?' + parse .urlencode (params )
146151
147152 # Send request
148- await self .__channel .ably .http .post (path , body = request_body , timeout = timeout )
149-
150- async def delete (self , msg_or_serial , annotation_values , params = None , timeout = None ):
153+ await self .__channel .ably .http .post (path , body = request_body )
154+
155+ async def delete (
156+ self ,
157+ msg_or_serial ,
158+ annotation : dict | Annotation ,
159+ params : dict | None = None ,
160+ ):
151161 """
152162 Delete an annotation on a message.
153163
@@ -156,9 +166,8 @@ async def delete(self, msg_or_serial, annotation_values, params=None, timeout=No
156166
157167 Args:
158168 msg_or_serial: Either a message serial (string) or a Message object
159- annotation_values : Dict containing annotation properties
169+ annotation : Dict containing annotation properties or Annotation object
160170 params: Optional dict of query parameters
161- timeout: Optional timeout for the HTTP request
162171
163172 Returns:
164173 None
@@ -167,11 +176,14 @@ async def delete(self, msg_or_serial, annotation_values, params=None, timeout=No
167176 AblyException: If the request fails or inputs are invalid
168177 """
169178 # Set action to delete
170- annotation_values = annotation_values .copy ()
171- annotation_values ['action' ] = 'annotation.delete'
172- return await self .publish (msg_or_serial , annotation_values , params , timeout )
179+ if isinstance (annotation , Annotation ):
180+ annotation_values = annotation .as_dict ()
181+ else :
182+ annotation_values = annotation .copy ()
183+ annotation_values ['action' ] = AnnotationAction .ANNOTATION_DELETE
184+ return await self .publish (msg_or_serial , annotation_values , params )
173185
174- async def get (self , msg_or_serial , params = None ):
186+ async def get (self , msg_or_serial , params : dict | None = None ):
175187 """
176188 Retrieve annotations for a message with pagination support.
177189
0 commit comments