Skip to content

Commit ba33368

Browse files
Sebastian Molendapubnub-release-bot
andauthored
Add Custom Message Type (#199)
* Add custom message type support for the following APIs: publish, signal, share file, subscribe and history * PubNub SDK v9.1.0 release. --------- Co-authored-by: PubNub Release Bot <120067856+pubnub-release-bot@users.noreply.github.com>
1 parent dc1a8e8 commit ba33368

File tree

14 files changed

+223
-102
lines changed

14 files changed

+223
-102
lines changed

.pubnub.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: python
2-
version: 9.0.0
2+
version: 9.1.0
33
schema: 1
44
scm: github.com/pubnub/python
55
sdks:
@@ -18,7 +18,7 @@ sdks:
1818
distributions:
1919
- distribution-type: library
2020
distribution-repository: package
21-
package-name: pubnub-9.0.0
21+
package-name: pubnub-9.1.0
2222
location: https://pypi.org/project/pubnub/
2323
supported-platforms:
2424
supported-operating-systems:
@@ -97,8 +97,8 @@ sdks:
9797
-
9898
distribution-type: library
9999
distribution-repository: git release
100-
package-name: pubnub-9.0.0
101-
location: https://github.com/pubnub/python/releases/download/v9.0.0/pubnub-9.0.0.tar.gz
100+
package-name: pubnub-9.1.0
101+
location: https://github.com/pubnub/python/releases/download/v9.1.0/pubnub-9.1.0.tar.gz
102102
supported-platforms:
103103
supported-operating-systems:
104104
Linux:
@@ -169,6 +169,11 @@ sdks:
169169
license-url: https://github.com/aio-libs/aiohttp/blob/master/LICENSE.txt
170170
is-required: Required
171171
changelog:
172+
- date: 2024-11-19
173+
version: v9.1.0
174+
changes:
175+
- type: feature
176+
text: "Add custom message type support for the following APIs: Publish, signal, share file, subscribe and history."
172177
- date: 2024-10-02
173178
version: v9.0.0
174179
changes:

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v9.1.0
2+
November 19 2024
3+
4+
#### Added
5+
- Add custom message type support for the following APIs: Publish, signal, share file, subscribe and history.
6+
17
## v9.0.0
28
October 02 2024
39

pubnub/endpoints/fetch_messages.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class FetchMessages(Endpoint):
3333

3434
def __init__(self, pubnub, channels: Union[str, List[str]] = None, start: int = None, end: int = None,
3535
count: int = None, include_meta: bool = None, include_message_actions: bool = None,
36-
include_message_type: bool = None, include_uuid: bool = None, decrypt_messages: bool = False):
36+
include_message_type: bool = None, include_uuid: bool = None, decrypt_messages: bool = False,
37+
include_custom_message_type: bool = None):
3738
Endpoint.__init__(self, pubnub)
3839
self._channels = []
3940
if channels:
@@ -46,6 +47,7 @@ def __init__(self, pubnub, channels: Union[str, List[str]] = None, start: int =
4647
self._include_message_type = include_message_type
4748
self._include_uuid = include_uuid
4849
self._decrypt_messages = decrypt_messages
50+
self._include_custom_message_type = include_custom_message_type
4951

5052
def channels(self, channels: Union[str, List[str]]) -> 'FetchMessages':
5153
utils.extend_list(self._channels, channels)
@@ -84,6 +86,11 @@ def include_message_type(self, include_message_type: bool) -> 'FetchMessages':
8486
self._include_message_type = include_message_type
8587
return self
8688

89+
def include_custom_message_type(self, include_custom_message_type: bool) -> 'FetchMessages':
90+
assert isinstance(include_custom_message_type, bool)
91+
self._include_custom_message_type = include_custom_message_type
92+
return self
93+
8794
def include_uuid(self, include_uuid: bool) -> 'FetchMessages':
8895
assert isinstance(include_uuid, bool)
8996
self._include_uuid = include_uuid
@@ -108,6 +115,9 @@ def custom_params(self):
108115
if self._include_message_type is not None:
109116
params['include_message_type'] = "true" if self._include_message_type else "false"
110117

118+
if self._include_custom_message_type is not None:
119+
params['include_custom_message_type'] = "true" if self._include_custom_message_type else "false"
120+
111121
if self.include_message_actions and self._include_uuid is not None:
112122
params['include_uuid'] = "true" if self._include_uuid else "false"
113123

pubnub/endpoints/file_operations/publish_file_message.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, pubnub):
2222
self._cipher_key = None
2323
self._replicate = None
2424
self._ptto = None
25+
self._custom_message_type = None
2526

2627
def meta(self, meta):
2728
self._meta = meta
@@ -53,6 +54,10 @@ def file_name(self, file_name):
5354
self._file_name = file_name
5455
return self
5556

57+
def custom_message_type(self, custom_message_type: str) -> 'PublishFileMessage':
58+
self._custom_message_type = custom_message_type
59+
return self
60+
5661
def _encrypt_message(self, message):
5762
if self._cipher_key:
5863
return PubNubCryptodome(self._pubnub.config).encrypt(self._cipher_key, utils.write_value_as_string(message))
@@ -90,6 +95,10 @@ def custom_params(self):
9095
"ttl": self._ttl,
9196
"store": 1 if self._should_store else 0
9297
})
98+
99+
if self._custom_message_type:
100+
params['custom_message_type'] = utils.url_encode(self._custom_message_type)
101+
93102
return params
94103

95104
def is_auth_required(self):

pubnub/endpoints/file_operations/send_file.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ def __init__(self, pubnub):
2424
self._file_object = None
2525
self._replicate = None
2626
self._ptto = None
27+
self._custom_message_type = None
2728

2829
def file_object(self, fd):
2930
self._file_object = fd
3031
return self
3132

33+
def custom_message_type(self, custom_message_type: str):
34+
self._custom_message_type = custom_message_type
35+
return self
36+
3237
def build_params_callback(self):
3338
return lambda a: {}
3439

@@ -124,23 +129,24 @@ def name(self):
124129
return "Send file to S3"
125130

126131
def sync(self):
127-
self._file_upload_envelope = FetchFileUploadS3Data(self._pubnub).\
128-
channel(self._channel).\
129-
file_name(self._file_name).sync()
132+
self._file_upload_envelope = FetchFileUploadS3Data(self._pubnub) \
133+
.channel(self._channel) \
134+
.file_name(self._file_name).sync()
130135

131136
response_envelope = super(SendFileNative, self).sync()
132137

133-
publish_file_response = PublishFileMessage(self._pubnub).\
134-
channel(self._channel).\
135-
meta(self._meta).\
136-
message(self._message).\
137-
file_id(response_envelope.result.file_id).\
138-
file_name(response_envelope.result.name).\
139-
should_store(self._should_store).\
140-
ttl(self._ttl).\
141-
replicate(self._replicate).\
142-
ptto(self._ptto).\
143-
cipher_key(self._cipher_key).sync()
138+
publish_file_response = PublishFileMessage(self._pubnub) \
139+
.channel(self._channel) \
140+
.meta(self._meta) \
141+
.message(self._message) \
142+
.file_id(response_envelope.result.file_id) \
143+
.file_name(response_envelope.result.name) \
144+
.should_store(self._should_store) \
145+
.ttl(self._ttl) \
146+
.replicate(self._replicate) \
147+
.ptto(self._ptto) \
148+
.custom_message_type(self._custom_message_type) \
149+
.cipher_key(self._cipher_key).sync()
144150

145151
response_envelope.result.timestamp = publish_file_response.result.timestamp
146152
return response_envelope

pubnub/endpoints/pubsub/publish.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ class Publish(Endpoint, TimeTokenOverrideMixin):
2929
_ptto: Optional[int]
3030
_ttl: Optional[int]
3131

32-
def __init__(self, pubnub, channel: str = None, message: any = None,
33-
should_store: Optional[bool] = None, use_post: Optional[bool] = None, meta: Optional[any] = None,
34-
replicate: Optional[bool] = None, ptto: Optional[int] = None, ttl: Optional[int] = None):
32+
def __init__(self, pubnub, channel: str = None, message: any = None, should_store: Optional[bool] = None,
33+
use_post: Optional[bool] = None, meta: Optional[any] = None, replicate: Optional[bool] = None,
34+
ptto: Optional[int] = None, ttl: Optional[int] = None, custom_message_type: Optional[str] = None):
3535

3636
super(Publish, self).__init__(pubnub)
3737
self._channel = channel
3838
self._message = message
3939
self._should_store = should_store
4040
self._use_post = use_post
4141
self._meta = meta
42+
self._custom_message_type = custom_message_type
4243
self._replicate = replicate
4344
self._ptto = ptto
4445
self._ttl = ttl
@@ -70,6 +71,10 @@ def meta(self, meta: any) -> 'Publish':
7071
self._meta = meta
7172
return self
7273

74+
def custom_message_type(self, custom_message_type: str) -> 'Publish':
75+
self._custom_message_type = custom_message_type
76+
return self
77+
7378
def ttl(self, ttl: int) -> 'Publish':
7479
self._ttl = ttl
7580
return self
@@ -105,6 +110,9 @@ def custom_params(self):
105110
if self._meta:
106111
params['meta'] = utils.write_value_as_string(self._meta)
107112

113+
if self._custom_message_type:
114+
params['custom_message_type'] = utils.url_encode(self._custom_message_type)
115+
108116
if self._should_store is not None:
109117
if self._should_store:
110118
params["store"] = "1"

pubnub/endpoints/signal.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Optional
12
from pubnub import utils
23
from pubnub.endpoints.endpoint import Endpoint
34
from pubnub.enums import HttpMethod, PNOperationType
@@ -17,10 +18,11 @@ class Signal(Endpoint):
1718
_channel: str
1819
_message: any
1920

20-
def __init__(self, pubnub, channel: str = None, message: any = None):
21+
def __init__(self, pubnub, channel: str = None, message: any = None, custom_message_type: Optional[str] = None):
2122
Endpoint.__init__(self, pubnub)
2223
self._channel = channel
2324
self._message = message
25+
self._custom_message_type = custom_message_type
2426

2527
def channel(self, channel) -> 'Signal':
2628
self._channel = str(channel)
@@ -30,6 +32,10 @@ def message(self, message) -> 'Signal':
3032
self._message = message
3133
return self
3234

35+
def custom_message_type(self, custom_message_type: str) -> 'Signal':
36+
self._custom_message_type = custom_message_type
37+
return self
38+
3339
def build_path(self):
3440
stringified_message = utils.write_value_as_string(self._message)
3541
msg = utils.url_encode(stringified_message)
@@ -39,7 +45,11 @@ def build_path(self):
3945
)
4046

4147
def custom_params(self):
42-
return {}
48+
params = {}
49+
if self._custom_message_type:
50+
params['custom_message_type'] = utils.url_encode(self._custom_message_type)
51+
52+
return params
4353

4454
def http_method(self):
4555
return HttpMethod.GET

pubnub/models/consumer/pubsub.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33

44
class PNMessageResult(object):
5-
def __init__(self, message, subscription, channel, timetoken, user_metadata=None, publisher=None, error=None):
5+
def __init__(self, message, subscription, channel, timetoken, user_metadata=None, publisher=None,
6+
error=None, custom_message_type=None):
67

78
if subscription is not None:
89
assert isinstance(subscription, str)
@@ -30,6 +31,7 @@ def __init__(self, message, subscription, channel, timetoken, user_metadata=None
3031
self.user_metadata = user_metadata
3132
self.publisher = publisher
3233
self.error = error
34+
self.custom_message_type = custom_message_type
3335

3436

3537
class PNSignalMessageResult(PNMessageResult):

pubnub/models/server/subscribe.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(self):
3030
self.publish_metadata = None
3131
self.only_channel_subscription = False
3232
self.type = 0
33+
self.custom_message_type = None
3334

3435
@classmethod
3536
def from_json(cls, json_input):
@@ -49,6 +50,8 @@ def from_json(cls, json_input):
4950
message.publish_metadata = PublishMetadata.from_json(json_input['p'])
5051
if 'e' in json_input:
5152
message.type = json_input['e']
53+
if 'cmt' in json_input:
54+
message.custom_message_type = json_input['cmt']
5255
return message
5356

5457

pubnub/pubnub_core.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494

9595
class PubNubCore:
9696
"""A base class for PubNub Python API implementations"""
97-
SDK_VERSION = "9.0.0"
97+
SDK_VERSION = "9.1.0"
9898
SDK_NAME = "PubNub-Python"
9999

100100
TIMESTAMP_DIVIDER = 1000
@@ -215,12 +215,13 @@ def where_now(self, user_id: Optional[str] = None):
215215

216216
def publish(self, channel: str = None, message: any = None, should_store: Optional[bool] = None,
217217
use_post: Optional[bool] = None, meta: Optional[any] = None, replicate: Optional[bool] = None,
218-
ptto: Optional[int] = None, ttl: Optional[int] = None) -> Publish:
218+
ptto: Optional[int] = None, ttl: Optional[int] = None, custom_message_type: Optional[str] = None
219+
) -> Publish:
219220
""" Sends a message to all channel subscribers. A successfully published message is replicated across PubNub's
220221
points of presence and sent simultaneously to all subscribed clients on a channel.
221222
"""
222223
return Publish(self, channel=channel, message=message, should_store=should_store, use_post=use_post, meta=meta,
223-
replicate=replicate, ptto=ptto, ttl=ttl)
224+
replicate=replicate, ptto=ptto, ttl=ttl, custom_message_type=custom_message_type)
224225

225226
def grant(self):
226227
""" Deprecated. Use grant_token instead """
@@ -274,8 +275,8 @@ def fire(self, channel: str = None, message: any = None, use_post: Optional[bool
274275
meta: Optional[any] = None) -> Fire:
275276
return Fire(self, channel=channel, message=message, use_post=use_post, meta=meta)
276277

277-
def signal(self, channel: str = None, message: any = None) -> Signal:
278-
return Signal(self, channel=channel, message=message)
278+
def signal(self, channel: str = None, message: any = None, custom_message_type: Optional[str] = None) -> Signal:
279+
return Signal(self, channel=channel, message=message, custom_message_type=custom_message_type)
279280

280281
def set_uuid_metadata(self, uuid: str = None, include_custom: bool = None, custom: dict = None,
281282
include_status: bool = True, include_type: bool = True, status: str = None, type: str = None,

0 commit comments

Comments
 (0)