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
@@ -48,7 +49,7 @@ def serial_from_msg_or_serial(msg_or_serial):
4849 return message_serial
4950
5051
51- def construct_validate_annotation (msg_or_serial , annotation : dict | Annotation ):
52+ def construct_validate_annotation (msg_or_serial , annotation : dict ):
5253 """
5354 Construct and validate an Annotation from input values.
5455
@@ -71,11 +72,8 @@ def construct_validate_annotation(msg_or_serial, annotation: dict | Annotation):
7172 status_code = 400 ,
7273 code = 40003 ,
7374 )
74- elif isinstance (annotation , Annotation ):
75- annotation_values = annotation .as_dict ()
76- else :
77- annotation_values = annotation
7875
76+ annotation_values = annotation .copy ()
7977 annotation_values ['message_serial' ] = message_serial
8078
8179 return Annotation .from_values (annotation_values )
@@ -108,23 +106,27 @@ def __base_path_for_serial(self, serial):
108106 channel_path = '/channels/{}/' .format (parse .quote_plus (self .__channel .name , safe = ':' ))
109107 return channel_path + 'messages/' + parse .quote_plus (serial , safe = ':' ) + '/annotations'
110108
111- async def publish (self , msg_or_serial , annotation_values , params = None , timeout = None ):
109+ async def publish (
110+ self ,
111+ msg_or_serial ,
112+ annotation : dict | Annotation ,
113+ params : dict | None = None ,
114+ ):
112115 """
113116 Publish an annotation on a message.
114117
115118 Args:
116119 msg_or_serial: Either a message serial (string) or a Message object
117- annotation_values : Dict containing annotation properties (type, name, data, etc.)
120+ annotation : Dict containing annotation properties (type, name, data, etc.) or Annotation object
118121 params: Optional dict of query parameters
119- timeout: Optional timeout for the HTTP request
120122
121123 Returns:
122124 None
123125
124126 Raises:
125127 AblyException: If the request fails or inputs are invalid
126128 """
127- annotation = construct_validate_annotation (msg_or_serial , annotation_values )
129+ annotation = construct_validate_annotation (msg_or_serial , annotation )
128130
129131 # Convert to wire format
130132 request_body = annotation .as_dict (binary = self .__channel .ably .options .use_binary_protocol )
@@ -145,9 +147,14 @@ async def publish(self, msg_or_serial, annotation_values, params=None, timeout=N
145147 path += '?' + parse .urlencode (params )
146148
147149 # 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 ):
150+ await self .__channel .ably .http .post (path , body = request_body )
151+
152+ async def delete (
153+ self ,
154+ msg_or_serial ,
155+ annotation : dict | Annotation ,
156+ params : dict | None = None ,
157+ ):
151158 """
152159 Delete an annotation on a message.
153160
@@ -156,9 +163,8 @@ async def delete(self, msg_or_serial, annotation_values, params=None, timeout=No
156163
157164 Args:
158165 msg_or_serial: Either a message serial (string) or a Message object
159- annotation_values : Dict containing annotation properties
166+ annotation : Dict containing annotation properties or Annotation object
160167 params: Optional dict of query parameters
161- timeout: Optional timeout for the HTTP request
162168
163169 Returns:
164170 None
@@ -167,11 +173,14 @@ async def delete(self, msg_or_serial, annotation_values, params=None, timeout=No
167173 AblyException: If the request fails or inputs are invalid
168174 """
169175 # 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 )
176+ if isinstance (annotation , Annotation ):
177+ annotation_values = annotation .as_dict ()
178+ else :
179+ annotation_values = annotation .copy ()
180+ annotation_values ['action' ] = AnnotationAction .ANNOTATION_DELETE
181+ return await self .publish (msg_or_serial , annotation_values , params )
173182
174- async def get (self , msg_or_serial , params = None ):
183+ async def get (self , msg_or_serial , params : dict | None = None ):
175184 """
176185 Retrieve annotations for a message with pagination support.
177186
0 commit comments