Skip to content

Commit ede01a3

Browse files
committed
refactor: remove methoddispatch dependency and simplify _publish logic
1 parent d0d15ad commit ede01a3

File tree

3 files changed

+38
-113
lines changed

3 files changed

+38
-113
lines changed

ably/rest/channel.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Iterator
77
from urllib import parse
88

9-
from methoddispatch import SingleDispatch, singledispatch
109
import msgpack
1110

1211
from ably.http.paginatedresult import PaginatedResult, format_params
@@ -19,7 +18,7 @@
1918
log = logging.getLogger(__name__)
2019

2120

22-
class Channel(SingleDispatch):
21+
class Channel:
2322
def __init__(self, ably, name, options):
2423
self.__ably = ably
2524
self.__name = name
@@ -76,15 +75,19 @@ def __publish_request_body(self, messages):
7675

7776
return request_body
7877

79-
@singledispatch
80-
def _publish(self, arg, *args, **kwargs):
81-
raise TypeError('Unexpected type %s' % type(arg))
78+
async def _publish(self, arg, *args, **kwargs):
79+
if isinstance(arg, Message):
80+
return await self.publish_message(arg, *args, **kwargs)
81+
elif isinstance(arg, list):
82+
return await self.publish_messages(arg, *args, **kwargs)
83+
elif isinstance(arg, str):
84+
return await self.publish_name_data(arg, *args, **kwargs)
85+
else:
86+
raise TypeError('Unexpected type %s' % type(arg))
8287

83-
@_publish.register(Message)
8488
async def publish_message(self, message, params=None, timeout=None):
8589
return await self.publish_messages([message], params, timeout=timeout)
8690

87-
@_publish.register(list)
8891
async def publish_messages(self, messages, params=None, timeout=None):
8992
request_body = self.__publish_request_body(messages)
9093
if not self.ably.options.use_binary_protocol:
@@ -98,7 +101,6 @@ async def publish_messages(self, messages, params=None, timeout=None):
98101
path += '?' + parse.urlencode(params)
99102
return await self.ably.http.post(path, body=request_body, timeout=timeout)
100103

101-
@_publish.register(str)
102104
async def publish_name_data(self, name, data, timeout=None):
103105
messages = [Message(name, data)]
104106
return await self.publish_messages(messages, timeout=timeout)

0 commit comments

Comments
 (0)