Skip to content

Commit 0a49f60

Browse files
committed
Add space and message_type to Subscribe Message + tests
1 parent f221647 commit 0a49f60

File tree

7 files changed

+326
-8
lines changed

7 files changed

+326
-8
lines changed

pubnub/endpoints/pubsub/publish.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Publish(Endpoint, TimeTokenOverrideMixin):
1515
def __init__(self, pubnub):
1616
super(Publish, self).__init__(pubnub)
1717
self._channel = None
18-
self._space = None
18+
self._space_id = None
1919
self._message = None
2020
self._message_type = None
2121
self._should_store = None
@@ -29,8 +29,8 @@ def channel(self, channel):
2929
self._channel = str(channel)
3030
return self
3131

32-
def space(self, space):
33-
self._space = str(space)
32+
def space_id(self, space_id):
33+
self._space_id = str(space_id)
3434
return self
3535

3636
def message(self, message):
@@ -104,8 +104,8 @@ def custom_params(self):
104104
if self._message_type is not None:
105105
params['type'] = str(self._message_type)
106106

107-
if self._space is not None:
108-
params['space-id'] = str(self._space)
107+
if self._space_id is not None:
108+
params['space-id'] = str(self._space_id)
109109

110110
return params
111111

pubnub/models/consumer/pubsub.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
class PNMessageResult(object):
5-
def __init__(self, message, subscription, channel, timetoken, user_metadata=None, publisher=None):
6-
5+
def __init__(self, message, subscription, channel, timetoken, user_metadata=None, publisher=None, message_type=None,
6+
space_id=None):
77
if subscription is not None:
88
assert isinstance(subscription, str)
99

@@ -29,6 +29,8 @@ def __init__(self, message, subscription, channel, timetoken, user_metadata=None
2929
self.timetoken = timetoken
3030
self.user_metadata = user_metadata
3131
self.publisher = publisher
32+
self.type = message_type
33+
self.space_id = space_id
3234

3335

3436
class PNSignalMessageResult(PNMessageResult):

pubnub/models/server/subscribe.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from pubnub.models.consumer.message_type import PNMessageType
2+
3+
14
class SubscribeEnvelope:
25
def __init__(self, messages=None, metadata=None):
36
assert isinstance(messages, (list, None))
@@ -30,6 +33,8 @@ def __init__(self):
3033
self.publish_metadata = None
3134
self.only_channel_subscription = False
3235
self.type = 0
36+
self.message_type = None
37+
self.space_id = None
3338

3439
@classmethod
3540
def from_json(cls, json_input):
@@ -49,6 +54,16 @@ def from_json(cls, json_input):
4954
message.publish_metadata = PublishMetadata.from_json(json_input['p'])
5055
if 'e' in json_input:
5156
message.type = json_input['e']
57+
58+
if 'si' in json_input:
59+
message.space_id = json_input['si']
60+
61+
if 'mt' in json_input:
62+
if 'e' in json_input:
63+
message.message_type = PNMessageType.from_response(json_input['mt'], json_input['e'])
64+
else:
65+
message.message_type = PNMessageType(json_input['mt'])
66+
5267
return message
5368

5469

pubnub/workers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def _process_incoming_payload(self, message):
172172
channel=channel,
173173
subscription=subscription_match,
174174
timetoken=publish_meta_data.publish_timetoken,
175-
publisher=publisher
175+
publisher=publisher,
176+
message_type=message.message_type,
177+
space_id=message.space_id
176178
)
177179
self._listener_manager.announce_message(pn_message_result)

tests/integrational/asyncio/test_subscribe.py

+117
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from unittest.mock import patch
77
from pubnub.models.consumer.pubsub import PNMessageResult
8+
from pubnub.models.consumer.message_type import PNMessageType
89
from pubnub.pubnub_asyncio import PubNubAsyncio, AsyncioEnvelope, SubscribeListener
910
from tests.helper import pnconf_sub_copy, pnconf_enc_sub_copy
1011
from tests.integrational.vcr_asyncio_sleeper import get_sleeper, VCR599Listener, VCR599ReconnectionManager
@@ -382,3 +383,119 @@ async def test_unsubscribe_all(event_loop, sleeper=asyncio.sleep):
382383
assert envelope.status.original_response['status'] == 200
383384

384385
await pubnub.stop()
386+
387+
388+
@get_sleeper('tests/integrational/fixtures/asyncio/subscription/publish_space_id.yaml')
389+
@pn_vcr.use_cassette(
390+
'tests/integrational/fixtures/asyncio/subscription/publish_space_id.yaml',
391+
filter_query_parameters=['pnsdk', 'l_cg', 'l_pres'],
392+
match_on=['method', 'scheme', 'host', 'port', 'string_list_in_path', 'string_list_in_query'],
393+
)
394+
@pytest.mark.asyncio
395+
async def test_subscribe_publish_space_id(event_loop, sleeper=asyncio.sleep):
396+
pubnub_sub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)
397+
pubnub_pub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)
398+
399+
patch_pubnub(pubnub_sub)
400+
patch_pubnub(pubnub_pub)
401+
402+
pubnub_sub.config.uuid = 'test-subscribe-asyncio-uuid-sub'
403+
pubnub_pub.config.uuid = 'test-subscribe-asyncio-uuid-pub'
404+
405+
callback = VCR599Listener(1)
406+
channel = "test-subscribe-asyncio-ch"
407+
message = "hey"
408+
space_id = "HelloSpace"
409+
pubnub_sub.add_listener(callback)
410+
pubnub_sub.subscribe().channels(channel).execute()
411+
412+
await callback.wait_for_connect()
413+
414+
publish_future = asyncio.ensure_future(
415+
pubnub_pub.publish().channel(channel).message(message).space_id(space_id).future()
416+
)
417+
subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel))
418+
419+
await asyncio.wait([
420+
publish_future,
421+
subscribe_message_future
422+
])
423+
424+
publish_envelope = publish_future.result()
425+
subscribe_envelope = subscribe_message_future.result()
426+
427+
assert isinstance(subscribe_envelope, PNMessageResult)
428+
assert subscribe_envelope.channel == channel
429+
assert subscribe_envelope.subscription is None
430+
assert subscribe_envelope.message == message
431+
assert subscribe_envelope.space_id == space_id
432+
assert subscribe_envelope.timetoken > 0
433+
434+
assert isinstance(publish_envelope, AsyncioEnvelope)
435+
assert publish_envelope.result.timetoken > 0
436+
assert publish_envelope.status.original_response[0] == 1
437+
438+
pubnub_sub.unsubscribe().channels(channel).execute()
439+
# await callback.wait_for_disconnect()
440+
441+
pubnub_pub.stop()
442+
pubnub_sub.stop()
443+
444+
445+
@get_sleeper('tests/integrational/fixtures/asyncio/subscription/publish_message_type.yaml')
446+
@pn_vcr.use_cassette(
447+
'tests/integrational/fixtures/asyncio/subscription/publish_message_type.yaml',
448+
filter_query_parameters=['pnsdk', 'l_cg', 'l_pres'],
449+
match_on=['method', 'scheme', 'host', 'port', 'string_list_in_path', 'string_list_in_query'],
450+
)
451+
@pytest.mark.asyncio
452+
async def test_subscribe_publish_message_type(event_loop, sleeper=asyncio.sleep):
453+
pubnub_sub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)
454+
pubnub_pub = PubNubAsyncio(pnconf_sub_copy(), custom_event_loop=event_loop)
455+
456+
patch_pubnub(pubnub_sub)
457+
patch_pubnub(pubnub_pub)
458+
459+
pubnub_sub.config.uuid = 'test-subscribe-asyncio-uuid-sub'
460+
pubnub_pub.config.uuid = 'test-subscribe-asyncio-uuid-pub'
461+
462+
callback = VCR599Listener(1)
463+
channel = "test-subscribe-asyncio-ch"
464+
message = "hey"
465+
message_type = "MyOwnCustomMessageType"
466+
pubnub_sub.add_listener(callback)
467+
pubnub_sub.subscribe().channels(channel).execute()
468+
469+
await callback.wait_for_connect()
470+
471+
publish_future = asyncio.ensure_future(
472+
pubnub_pub.publish().channel(channel).message(message).message_type(message_type).future()
473+
)
474+
subscribe_message_future = asyncio.ensure_future(callback.wait_for_message_on(channel))
475+
476+
await asyncio.wait([
477+
publish_future,
478+
subscribe_message_future
479+
])
480+
481+
publish_envelope = publish_future.result()
482+
subscribe_envelope = subscribe_message_future.result()
483+
484+
assert isinstance(subscribe_envelope, PNMessageResult)
485+
assert subscribe_envelope.channel == channel
486+
assert subscribe_envelope.subscription is None
487+
assert subscribe_envelope.message == message
488+
489+
assert isinstance(subscribe_envelope.type, PNMessageType)
490+
assert str(subscribe_envelope.type) == message_type
491+
assert subscribe_envelope.timetoken > 0
492+
493+
assert isinstance(publish_envelope, AsyncioEnvelope)
494+
assert publish_envelope.result.timetoken > 0
495+
assert publish_envelope.status.original_response[0] == 1
496+
497+
pubnub_sub.unsubscribe().channels(channel).execute()
498+
# await callback.wait_for_disconnect()
499+
500+
pubnub_pub.stop()
501+
pubnub_sub.stop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
User-Agent:
6+
- PubNub-Python-Asyncio/7.1.0
7+
method: GET
8+
uri: https://ps.pndsn.com/v2/subscribe/sub-c-33f55052-190b-11e6-bfbc-02ee2ddab7fe/test-subscribe-asyncio-ch/0?tt=0&uuid=test-subscribe-asyncio-uuid-sub
9+
response:
10+
body:
11+
string: '{"t":{"t":"16745835146497675","r":43},"m":[]}'
12+
headers:
13+
Access-Control-Allow-Methods:
14+
- GET
15+
Access-Control-Allow-Origin:
16+
- '*'
17+
Cache-Control:
18+
- no-cache
19+
Connection:
20+
- keep-alive
21+
Content-Length:
22+
- '45'
23+
Content-Type:
24+
- text/javascript; charset="UTF-8"
25+
Date:
26+
- Tue, 24 Jan 2023 18:09:50 GMT
27+
status:
28+
code: 200
29+
message: OK
30+
url: https://ps.pndsn.com/v2/subscribe/sub-c-33f55052-190b-11e6-bfbc-02ee2ddab7fe/test-subscribe-asyncio-ch/0?tt=0&pnsdk=PubNub-Python-Asyncio%2F7.1.0&uuid=test-subscribe-asyncio-uuid-sub
31+
- request:
32+
body: null
33+
headers:
34+
User-Agent:
35+
- PubNub-Python-Asyncio/7.1.0
36+
method: GET
37+
uri: https://ps.pndsn.com/publish/pub-c-739aa0fc-3ed5-472b-af26-aca1b333ec52/sub-c-33f55052-190b-11e6-bfbc-02ee2ddab7fe/0/test-subscribe-asyncio-ch/0/%22hey%22?seqn=1&type=MyOwnCustomMessageType&uuid=test-subscribe-asyncio-uuid-pub
38+
response:
39+
body:
40+
string: '[1,"Sent","16745837903154482"]'
41+
headers:
42+
Access-Control-Allow-Methods:
43+
- GET
44+
Access-Control-Allow-Origin:
45+
- '*'
46+
Cache-Control:
47+
- no-cache
48+
Connection:
49+
- keep-alive
50+
Content-Length:
51+
- '30'
52+
Content-Type:
53+
- text/javascript; charset="UTF-8"
54+
Date:
55+
- Tue, 24 Jan 2023 18:09:50 GMT
56+
status:
57+
code: 200
58+
message: OK
59+
url: https://ps.pndsn.com/publish/pub-c-739aa0fc-3ed5-472b-af26-aca1b333ec52/sub-c-33f55052-190b-11e6-bfbc-02ee2ddab7fe/0/test-subscribe-asyncio-ch/0/%22hey%22?type=MyOwnCustomMessageType&seqn=1&pnsdk=PubNub-Python-Asyncio%2F7.1.0&uuid=test-subscribe-asyncio-uuid-pub
60+
- request:
61+
body: null
62+
headers:
63+
User-Agent:
64+
- PubNub-Python-Asyncio/7.1.0
65+
method: GET
66+
uri: https://ps.pndsn.com/v2/subscribe/sub-c-33f55052-190b-11e6-bfbc-02ee2ddab7fe/test-subscribe-asyncio-ch/0?tr=43&tt=16745835146497675&uuid=test-subscribe-asyncio-uuid-sub
67+
response:
68+
body:
69+
string: '{"t":{"t":"16745837903154482","r":43},"m":[{"a":"2","f":0,"i":"test-subscribe-asyncio-uuid-pub","s":1,"p":{"t":"16745837903154482","r":43},"k":"sub-c-33f55052-190b-11e6-bfbc-02ee2ddab7fe","c":"test-subscribe-asyncio-ch","d":"hey","mt":"MyOwnCustomMessageType"}]}'
70+
headers:
71+
Access-Control-Allow-Methods:
72+
- GET
73+
Access-Control-Allow-Origin:
74+
- '*'
75+
Cache-Control:
76+
- no-cache
77+
Connection:
78+
- keep-alive
79+
Content-Encoding:
80+
- gzip
81+
Content-Type:
82+
- text/javascript; charset="UTF-8"
83+
Date:
84+
- Tue, 24 Jan 2023 18:09:50 GMT
85+
Transfer-Encoding:
86+
- chunked
87+
status:
88+
code: 200
89+
message: OK
90+
url: https://ps.pndsn.com/v2/subscribe/sub-c-33f55052-190b-11e6-bfbc-02ee2ddab7fe/test-subscribe-asyncio-ch/0?tt=16745835146497675&tr=43&pnsdk=PubNub-Python-Asyncio%2F7.1.0&uuid=test-subscribe-asyncio-uuid-sub
91+
version: 1

0 commit comments

Comments
 (0)