Skip to content

Commit 32db48d

Browse files
Add missing call() method to namespace classes (Fixes miguelgrinberg#800)
1 parent ed08a01 commit 32db48d

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

src/socketio/asyncio_namespace.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ async def send(self, data, to=None, room=None, skip_sid=None,
7171
namespace=namespace or self.namespace,
7272
callback=callback)
7373

74+
async def call(self, event, data=None, to=None, sid=None, namespace=None,
75+
timeout=None):
76+
"""Emit a custom event to a client and wait for the response.
77+
78+
The only difference with the :func:`socketio.Server.call` method is
79+
that when the ``namespace`` argument is not given the namespace
80+
associated with the class is used.
81+
"""
82+
return await self.server.call(event, data=data, to=to, sid=sid,
83+
namespace=namespace or self.namespace,
84+
timeout=timeout)
85+
7486
async def close_room(self, room, namespace=None):
7587
"""Close a room.
7688
@@ -193,6 +205,17 @@ async def send(self, data, namespace=None, callback=None):
193205
namespace=namespace or self.namespace,
194206
callback=callback)
195207

208+
async def call(self, event, data=None, namespace=None, timeout=None):
209+
"""Emit a custom event to the server and wait for the response.
210+
211+
The only difference with the :func:`socketio.Client.call` method is
212+
that when the ``namespace`` argument is not given the namespace
213+
associated with the class is used.
214+
"""
215+
return await self.client.call(event, data=data,
216+
namespace=namespace or self.namespace,
217+
timeout=timeout)
218+
196219
async def disconnect(self):
197220
"""Disconnect a client.
198221

src/socketio/namespace.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ def send(self, data, to=None, room=None, skip_sid=None, namespace=None,
6262
namespace=namespace or self.namespace,
6363
callback=callback)
6464

65+
def call(self, event, data=None, to=None, sid=None, namespace=None,
66+
timeout=None):
67+
"""Emit a custom event to a client and wait for the response.
68+
69+
The only difference with the :func:`socketio.Server.call` method is
70+
that when the ``namespace`` argument is not given the namespace
71+
associated with the class is used.
72+
"""
73+
return self.server.call(event, data=data, to=to, sid=sid,
74+
namespace=namespace or self.namespace,
75+
timeout=timeout)
76+
6577
def enter_room(self, sid, room, namespace=None):
6678
"""Enter a room.
6779
@@ -171,8 +183,7 @@ def emit(self, event, data=None, namespace=None, callback=None):
171183
namespace=namespace or self.namespace,
172184
callback=callback)
173185

174-
def send(self, data, room=None, skip_sid=None, namespace=None,
175-
callback=None):
186+
def send(self, data, room=None, namespace=None, callback=None):
176187
"""Send a message to the server.
177188
178189
The only difference with the :func:`socketio.Client.send` method is
@@ -182,6 +193,17 @@ def send(self, data, room=None, skip_sid=None, namespace=None,
182193
return self.client.send(data, namespace=namespace or self.namespace,
183194
callback=callback)
184195

196+
def call(self, event, data=None, namespace=None, timeout=None):
197+
"""Emit a custom event to the server and wait for the response.
198+
199+
The only difference with the :func:`socketio.Client.call` method is
200+
that when the ``namespace`` argument is not given the namespace
201+
associated with the class is used.
202+
"""
203+
return self.client.call(event, data=data,
204+
namespace=namespace or self.namespace,
205+
timeout=timeout)
206+
185207
def disconnect(self):
186208
"""Disconnect from the server.
187209

tests/asyncio/test_asyncio_namespace.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,31 @@ def test_send(self):
157157
callback='cb',
158158
)
159159

160+
def test_call(self):
161+
ns = asyncio_namespace.AsyncNamespace('/foo')
162+
mock_server = mock.MagicMock()
163+
mock_server.call = AsyncMock()
164+
ns._set_server(mock_server)
165+
_run(ns.call('ev', data='data', to='sid'))
166+
ns.server.call.mock.assert_called_with(
167+
'ev',
168+
data='data',
169+
to='sid',
170+
sid=None,
171+
namespace='/foo',
172+
timeout=None,
173+
)
174+
_run(ns.call('ev', data='data', sid='sid', namespace='/bar',
175+
timeout=45))
176+
ns.server.call.mock.assert_called_with(
177+
'ev',
178+
data='data',
179+
to=None,
180+
sid='sid',
181+
namespace='/bar',
182+
timeout=45,
183+
)
184+
160185
def test_enter_room(self):
161186
ns = asyncio_namespace.AsyncNamespace('/foo')
162187
ns._set_server(mock.MagicMock())
@@ -298,6 +323,20 @@ def test_send_client(self):
298323
'data', namespace='/bar', callback='cb'
299324
)
300325

326+
def test_call_client(self):
327+
ns = asyncio_namespace.AsyncClientNamespace('/foo')
328+
mock_client = mock.MagicMock()
329+
mock_client.call = AsyncMock()
330+
ns._set_client(mock_client)
331+
_run(ns.call('ev', data='data'))
332+
ns.client.call.mock.assert_called_with(
333+
'ev', data='data', namespace='/foo', timeout=None
334+
)
335+
_run(ns.call('ev', data='data', namespace='/bar', timeout=45))
336+
ns.client.call.mock.assert_called_with(
337+
'ev', data='data', namespace='/bar', timeout=45
338+
)
339+
301340
def test_disconnect_client(self):
302341
ns = asyncio_namespace.AsyncClientNamespace('/foo')
303342
mock_client = mock.MagicMock()

tests/common/test_namespace.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,34 @@ def test_send(self):
112112
callback='cb',
113113
)
114114

115+
def test_call(self):
116+
ns = namespace.Namespace('/foo')
117+
ns._set_server(mock.MagicMock())
118+
ns.call('ev', data='data', to='sid')
119+
ns.server.call.assert_called_with(
120+
'ev',
121+
data='data',
122+
to='sid',
123+
sid=None,
124+
namespace='/foo',
125+
timeout=None,
126+
)
127+
ns.call(
128+
'ev',
129+
data='data',
130+
sid='sid',
131+
namespace='/bar',
132+
timeout=45,
133+
)
134+
ns.server.call.assert_called_with(
135+
'ev',
136+
data='data',
137+
to=None,
138+
sid='sid',
139+
namespace='/bar',
140+
timeout=45,
141+
)
142+
115143
def test_enter_room(self):
116144
ns = namespace.Namespace('/foo')
117145
ns._set_server(mock.MagicMock())
@@ -204,6 +232,17 @@ def test_send_client(self):
204232
'data', namespace='/bar', callback='cb'
205233
)
206234

235+
def test_call_client(self):
236+
ns = namespace.ClientNamespace('/foo')
237+
ns._set_client(mock.MagicMock())
238+
ns.call('ev', data='data')
239+
ns.client.call.assert_called_with(
240+
'ev', data='data', namespace='/foo', timeout=None)
241+
ns.call('ev', data='data', namespace='/bar', timeout=45)
242+
ns.client.call.assert_called_with(
243+
'ev', data='data', namespace='/bar', timeout=45
244+
)
245+
207246
def test_disconnect_client(self):
208247
ns = namespace.ClientNamespace('/foo')
209248
ns._set_client(mock.MagicMock())

0 commit comments

Comments
 (0)