import json import logging from collections import OrderedDict from typing import Iterator, Optional from urllib import parse import msgpack from ably.pubsub.http.annotations import HttpAnnotations from ably.pubsub.request.paginatedresult import PaginatedResult, format_params from ably.pubsub.types.channeldetails import ChannelDetails from ably.pubsub.types.channeloptions import ChannelOptions from ably.pubsub.types.message import ( Message, MessageAction, MessageVersion, assign_idempotent_ids, make_message_response_handler, make_single_message_response_handler, validate_message_size, ) from ably.pubsub.types.messagepatch import check_patches_to_send from ably.pubsub.types.operations import MessageOperation, PublishResult, UpdateDeleteResult from ably.pubsub.types.presence import Presence from ably.pubsub.util.crypto import get_cipher from ably.pubsub.util.exceptions import ( AblyException, IncompatibleClientIdException, catch_all, ) log = logging.getLogger(__name__) class Channel: __annotations: HttpAnnotations def __init__(self, ably, name, options: Optional[ChannelOptions] = None): self.__ably = ably self.__name = name # RSL2: quote_plus is form encoding, and its `+` for a space is a literal plus in a path self.__base_path = '/channels/{}/'.format(parse.quote(name, safe=':')) self._set_options(options or ChannelOptions()) self.__presence = Presence(self) self.__annotations = HttpAnnotations(self) @catch_all async def history(self, direction=None, limit: int = None, start=None, end=None): """Returns the history for this channel""" params = format_params({}, direction=direction, start=start, end=end, limit=limit) path = self.__base_path + 'messages' + params message_handler = make_message_response_handler(self.cipher) return await PaginatedResult.paginated_query( self.ably.http, url=path, response_processor=message_handler) def __publish_request_body(self, messages): """ Helper private method, separated from publish() to test RSL1j """ # Idempotent publishing if self.ably.options.idempotent_rest_publishing: assign_idempotent_ids(messages) request_body_list = [] for m in messages: if m.client_id == '*': raise IncompatibleClientIdException( 'Wildcard client_id is reserved and cannot be used when publishing messages', 400, 40012) elif m.client_id is not None and not self.ably.auth.can_assume_client_id(m.client_id): raise IncompatibleClientIdException( f'Cannot publish with client_id \'{m.client_id}\' as it is incompatible with the ' f'current configured client_id \'{self.ably.auth.client_id}\'', 400, 40012) if self.cipher: m.encrypt(self.cipher) request_body_list.append(m) # RSL1i validate_message_size(request_body_list, self.ably.options.max_message_size) request_body = [ message.as_dict(binary=self.ably.options.use_binary_protocol) for message in request_body_list] if len(request_body) == 1: request_body = request_body[0] return request_body async def _publish(self, arg, *args, **kwargs): if isinstance(arg, Message): return await self.publish_message(arg, *args, **kwargs) elif isinstance(arg, list): return await self.publish_messages(arg, *args, **kwargs) elif isinstance(arg, str): return await self.publish_name_data(arg, *args, **kwargs) else: raise TypeError(f'Unexpected type {type(arg)}') async def publish_message(self, message, params=None): return await self.publish_messages([message], params) async def publish_messages(self, messages, params=None): request_body = self.__publish_request_body(messages) if not self.ably.options.use_binary_protocol: request_body = json.dumps(request_body, separators=(',', ':')) else: request_body = msgpack.packb(request_body, use_bin_type=True) path = self.__base_path + 'messages' if params: params = {k: str(v).lower() if isinstance(v, bool) else v for k, v in params.items()} path += '?' + parse.urlencode(params) response = await self.ably.http.post(path, body=request_body) # Parse response to extract serials result_data = response.to_native() if result_data and isinstance(result_data, dict): return PublishResult.from_dict(result_data) return PublishResult() async def publish_name_data(self, name, data): messages = [Message(name, data)] return await self.publish_messages(messages) async def publish(self, *args, **kwargs): """Publishes a message on this channel. :Parameters: - `name`: the name for this message. - `data`: the data for this message. - `messages`: list of `Message` objects to be published. - `message`: a single `Message` objet to be published :attention: You can publish using `name` and `data` OR `messages` OR `message`, never all three. """ # For backwards compatibility if len(args) == 0: if len(kwargs) == 0: return await self.publish_name_data(None, None) if 'name' in kwargs or 'data' in kwargs: name = kwargs.pop('name', None) data = kwargs.pop('data', None) return await self.publish_name_data(name, data, **kwargs) if 'messages' in kwargs: messages = kwargs.pop('messages') return await self.publish_messages(messages, **kwargs) return await self._publish(*args, **kwargs) async def status(self): """Retrieves current channel active status with no. of publishers, subscribers, presence_members etc""" # RSL8 path = '/channels/{}'.format(parse.quote(self.name, safe=':')) response = await self.ably.http.get(path) obj = response.to_native() return ChannelDetails.from_dict(obj) async def _send_update( self, message: Message, action: MessageAction, operation: Optional[MessageOperation] = None, params: Optional[dict] = None, ): """Internal method to send update/delete/append operations.""" if not message.serial: raise AblyException( "Message serial is required for update/delete/append operations", status_code=400, code=40003, ) if not operation: version = None else: version = MessageVersion( client_id=operation.client_id, description=operation.description, metadata=operation.metadata ) # Create a new message with the operation fields update_message = Message( name=message.name, data=message.data, client_id=message.client_id, serial=message.serial, action=action, version=version, extras=message.extras, annotations=message.annotations, patches=message.patches, ) # Encrypt if needed if self.cipher: update_message.encrypt(self.cipher) # Serialize the message request_body = update_message.as_dict(binary=self.ably.options.use_binary_protocol) if not self.ably.options.use_binary_protocol: request_body = json.dumps(request_body, separators=(',', ':')) else: request_body = msgpack.packb(request_body, use_bin_type=True) # Build path with params path = self.__base_path + 'messages/{}'.format(parse.quote(message.serial, safe=':')) if params: params = {k: str(v).lower() if isinstance(v, bool) else v for k, v in params.items()} path += '?' + parse.urlencode(params) # Send request response = await self.ably.http.patch(path, body=request_body) # Parse response result_data = response.to_native() if result_data and isinstance(result_data, dict): return UpdateDeleteResult.from_dict(result_data) return UpdateDeleteResult() async def update_message(self, message: Message, operation: MessageOperation = None, params: dict = None): """Updates an existing message on this channel. Parameters: - message: Message object to update. Must have a serial field. - operation: Optional MessageOperation containing description and metadata for the update. - params: Optional dict of query parameters. Returns: - UpdateDeleteResult containing the version serial of the updated message. """ return await self._send_update(message, MessageAction.MESSAGE_UPDATE, operation, params) async def delete_message(self, message: Message, operation: MessageOperation = None, params: dict = None): """Deletes a message on this channel. Parameters: - message: Message object to delete. Must have a serial field. - operation: Optional MessageOperation containing description and metadata for the delete. - params: Optional dict of query parameters. Returns: - UpdateDeleteResult containing the version serial of the deleted message. """ return await self._send_update(message, MessageAction.MESSAGE_DELETE, operation, params) async def append_message(self, message: Message, operation: MessageOperation = None, params: dict = None): """Appends data to an existing message on this channel. Parameters: - message: Message object with data to append. Must have a serial field. - operation: Optional MessageOperation containing description and metadata for the append. - params: Optional dict of query parameters. Returns: - UpdateDeleteResult containing the version serial of the appended message. """ return await self._send_update(message, MessageAction.MESSAGE_APPEND, operation, params) async def patch_message(self, message: Message, operation: MessageOperation = None, params: dict = None): """Patches an existing message on this channel (PSRFC-44): changes parts of its data at JSON Pointer paths, and its headers, without sending the whole message. Parameters: - message: Message object with a serial field, and the changes in its patches field, a MessagePatches. Its other fields are sent as they are. - operation: Optional MessageOperation containing description and metadata for the patch. - params: Optional dict of query parameters. Returns: - UpdateDeleteResult containing the version serial of the patched message. """ check_patches_to_send(message) return await self._send_update(message, MessageAction.MESSAGE_PATCH, operation, params) async def get_message(self, serial_or_message): """Retrieves a single message by its serial. Parameters: - serial_or_message: Either a string serial or a Message object with a serial field. Returns: - Message object for the requested serial. Raises: - AblyException: If the serial is missing or the message cannot be retrieved. """ # Extract serial from string or Message object if isinstance(serial_or_message, str): serial = serial_or_message elif isinstance(serial_or_message, Message): serial = serial_or_message.serial else: serial = None if not serial: raise AblyException( 'This message lacks a serial. Make sure you have enabled "Message annotations, ' 'updates, and deletes" in channel settings on your dashboard.', status_code=400, code=40003, ) # Build the path path = self.__base_path + 'messages/' + parse.quote(serial, safe=':') # Make the request response = await self.ably.http.get(path) # Create Message from the response message_handler = make_single_message_response_handler(self.cipher) return message_handler(response) async def get_message_versions(self, serial_or_message, params=None): """Retrieves version history for a message. Parameters: - serial_or_message: Either a string serial or a Message object with a serial field. - params: Optional dict of query parameters for pagination (e.g., limit, start, end, direction). Returns: - PaginatedResult containing Message objects representing each version. Raises: - AblyException: If the serial is missing or versions cannot be retrieved. """ # Extract serial from string or Message object if isinstance(serial_or_message, str): serial = serial_or_message elif isinstance(serial_or_message, Message): serial = serial_or_message.serial else: serial = None if not serial: raise AblyException( 'This message lacks a serial. Make sure you have enabled "Message annotations, ' 'updates, and deletes" in channel settings on your dashboard.', status_code=400, code=40003, ) # Build the path params_str = format_params({}, **params) if params else '' path = self.__base_path + 'messages/' + parse.quote(serial, safe=':') + '/versions' + params_str # Create message handler for decoding message_handler = make_message_response_handler(self.cipher) # Return paginated result return await PaginatedResult.paginated_query( self.ably.http, url=path, response_processor=message_handler ) @property def ably(self): return self.__ably @property def name(self): return self.__name @property def base_path(self): return self.__base_path @property def cipher(self): """The cipher built from `options.cipher`, or None when the channel is not encrypted""" return self.__cipher # TB2 @property def options(self) -> ChannelOptions: """The channel's options, as last set""" return self.__options @property def presence(self): return self.__presence @property def annotations(self) -> HttpAnnotations: return self.__annotations # RSL7 async def set_options(self, options: ChannelOptions) -> None: """Replaces the channel's options. The options are replaced whole rather than merged, so options without a cipher turn encryption off. """ self._set_options(options) def _set_options(self, options: ChannelOptions) -> None: self.__options = options # The cipher carries the CBC chaining state from one message to the next, so it is built # once per set of options rather than on every read self.__cipher = get_cipher(options.cipher) if options.cipher is not None else None class Channels: def __init__(self, rest): self.__ably = rest self.__all: dict = OrderedDict() @staticmethod def _channel_options(options, kwargs) -> Optional[ChannelOptions]: """What `get` was given - a ChannelOptions, a dict of its fields or keyword arguments - as a ChannelOptions, or None if it was given no options""" if options is None and kwargs: return ChannelOptions(**kwargs) if isinstance(options, dict): return ChannelOptions.from_dict(options) return options # RSN3 def get(self, name, options: Optional[ChannelOptions] = None, **kwargs): """Returns the channel called `name`, creating it if need be. Options given for an existing channel replace its options (RSN3c). """ if isinstance(name, bytes): name = name.decode('ascii') options = self._channel_options(options, kwargs) if name not in self.__all: result = self.__all[name] = Channel(self.__ably, name, options) else: result = self.__all[name] if options is not None: result._set_options(options) return result def __getitem__(self, key): return self.get(key) def __contains__(self, item): if isinstance(item, Channel): name = item.name elif isinstance(item, bytes): name = item.decode('ascii') else: name = item return name in self.__all def __iter__(self) -> Iterator[str]: return iter(self.__all.values()) # RSN4 def release(self, name: str): """Releases a Channel object, deleting it, and enabling it to be garbage collected. If the channel does not exist, nothing happens. It also removes any listeners associated with the channel. Parameters ---------- name: str Channel name """ if name not in self.__all: return del self.__all[name]