From 4ffebf41a12bb9c4983537cb2a41e28265c2c5d9 Mon Sep 17 00:00:00 2001 From: isaev-vs Date: Wed, 30 Aug 2023 19:22:10 +0300 Subject: [PATCH] Rename to --- distributed_websocket/_broker.py | 8 ++++---- distributed_websocket/_message.py | 20 ++++++++++---------- distributed_websocket/_subscriptions.py | 4 ++-- distributed_websocket/manager.py | 2 +- tests/test_manager.py | 4 ++-- tests/test_message.py | 8 ++++---- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/distributed_websocket/_broker.py b/distributed_websocket/_broker.py index 5443877..1ef07e1 100644 --- a/distributed_websocket/_broker.py +++ b/distributed_websocket/_broker.py @@ -67,8 +67,8 @@ async def publish(self, channel: str, message: Any) -> None: async def get_message(self, **kwargs) -> Message | None: message = await self._messages.get() if self.has_subscribers(message['channel']): - typ, topic, conn_id, data = untag_broker_message(message['data']) - return Message(data=data, typ=typ, topic=topic, conn_id=conn_id) + type, topic, conn_id, data = untag_broker_message(message['data']) + return Message(data=data, type=type, topic=topic, conn_id=conn_id) def has_subscribers(self, channel: str) -> bool: return channel in self._subscribers @@ -111,8 +111,8 @@ async def get_message(self, **kwargs) -> Message | None: ignore_subscribe_messages=True ) if message: - typ, topic, conn_id, data = untag_broker_message(message['data']) - return Message(data=data, typ=typ, topic=topic, conn_id=conn_id) + type, topic, conn_id, data = untag_broker_message(message['data']) + return Message(data=data, type=type, topic=topic, conn_id=conn_id) def _create_inmemory_broker() -> InMemoryBroker: diff --git a/distributed_websocket/_message.py b/distributed_websocket/_message.py index e69be86..00e7ef3 100644 --- a/distributed_websocket/_message.py +++ b/distributed_websocket/_message.py @@ -46,17 +46,17 @@ def tag_client_message(data: dict) -> Any: def validate_incoming_message(data: dict) -> None: - typ, topic, conn_id = ( + type, topic, conn_id = ( data.get('type'), data.get('topic'), data.get('conn_id'), ) if not is_valid_type_client_message(data): - raise ValueError(f'Invalid message type: {typ}') - if topic is None and typ not in __NULL_TOPIC_ALLOWED_TYPES: - raise ValueError(f'Invalid message type "{typ}" with no topic') - if conn_id is None and typ in __REQUIRE_CONN_ID_TYPES: - raise ValueError(f'Invalid message type "{typ}" with no conn_id') + raise ValueError(f'Invalid message type: {type}') + if topic is None and type not in __NULL_TOPIC_ALLOWED_TYPES: + raise ValueError(f'Invalid message type "{type}" with no topic') + if conn_id is None and type in __REQUIRE_CONN_ID_TYPES: + raise ValueError(f'Invalid message type "{type}" with no conn_id') def untag_broker_message(data: dict | str) -> tuple: @@ -70,11 +70,11 @@ def __init__( self, *, data: Any, - typ: str, + type: str, topic: str | None = None, conn_id: str | list[str] | None = None, ) -> None: - self.typ = typ + self.type = type self.topic = topic self.conn_id = conn_id self.data = data @@ -83,13 +83,13 @@ def __init__( def from_client_message(cls, *, data: dict) -> 'Message': return cls( data=data, - typ=data.pop('type', None), + type=data.pop('type', None), topic=data.pop('topic', None), conn_id=data.pop('conn_id', None), ) def __serialize__(self) -> dict[str, Any]: - self.data['type'] = self.typ + self.data['type'] = self.type self.data['topic'] = self.topic self.data['conn_id'] = self.conn_id return self.data diff --git a/distributed_websocket/_subscriptions.py b/distributed_websocket/_subscriptions.py index 718e17c..cd9d8b1 100644 --- a/distributed_websocket/_subscriptions.py +++ b/distributed_websocket/_subscriptions.py @@ -11,7 +11,7 @@ def is_subscription_message(message: Message) -> bool: - return message.typ == 'subscribe' or message.typ == 'unsubscribe' + return message.type == 'subscribe' or message.type == 'unsubscribe' def _is_valid_subscription(topic: str) -> bool: @@ -46,7 +46,7 @@ def unsubscribe(connection: Connection, message: Message) -> None: def handle_subscription_message( connection: Connection, message: Message ) -> None: - if message.typ == 'subscribe': + if message.type == 'subscribe': subscribe(connection, message) else: unsubscribe(connection, message) diff --git a/distributed_websocket/manager.py b/distributed_websocket/manager.py index 2bb32fa..625623a 100644 --- a/distributed_websocket/manager.py +++ b/distributed_websocket/manager.py @@ -134,7 +134,7 @@ def send_by_conn_id(self, message: Message) -> None: def _get_outgoing_message_handler( self, message: Message ) -> Callable[[Message], T | Coroutine[Any, Any, T]]: - return getattr(self, message.typ, self.send) + return getattr(self, message.type, self.send) def send_msg(self, message: Message) -> None: self._get_outgoing_message_handler(message)(message) diff --git a/tests/test_manager.py b/tests/test_manager.py index 684d4fe..294b678 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -25,7 +25,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: manager.send( Message( data={'msg': 'hello'}, - typ='send', + type='send', topic='tests/1', ) ) @@ -55,7 +55,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: manager.send( Message( data={'msg': 'hello'}, - typ='send', + type='send', topic='tests/1', ) ) diff --git a/tests/test_message.py b/tests/test_message.py index 56217df..8f95e44 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -6,7 +6,7 @@ def test_message_01(): m = Message( data={'msg': 'hello'}, - typ='send', + type='send', topic='test', conn_id='test', ) @@ -25,16 +25,16 @@ def test_message_02(): data={'msg': 'hello', 'type': 'send', 'topic': 'test', 'conn_id': 'test'} ) assert m.data == {'msg': 'hello'} - assert m.typ == 'send' + assert m.type == 'send' assert m.topic == 'test' assert m.conn_id == 'test' def test_untag_broker_message_01(): - typ, topic, conn_id, data = untag_broker_message( + type, topic, conn_id, data = untag_broker_message( '{"msg": "hello", "type": "send", "topic": "test", "conn_id": "test"}' ) - assert typ == 'send' + assert type == 'send' assert topic == 'test' assert conn_id == 'test' assert data == {'msg': 'hello'}