Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom Message Type #199

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: python
version: 9.0.0
version: 9.1.0
schema: 1
scm: github.com/pubnub/python
sdks:
Expand All @@ -18,7 +18,7 @@ sdks:
distributions:
- distribution-type: library
distribution-repository: package
package-name: pubnub-9.0.0
package-name: pubnub-9.1.0
location: https://pypi.org/project/pubnub/
supported-platforms:
supported-operating-systems:
Expand Down Expand Up @@ -97,8 +97,8 @@ sdks:
-
distribution-type: library
distribution-repository: git release
package-name: pubnub-9.0.0
location: https://github.com/pubnub/python/releases/download/v9.0.0/pubnub-9.0.0.tar.gz
package-name: pubnub-9.1.0
location: https://github.com/pubnub/python/releases/download/v9.1.0/pubnub-9.1.0.tar.gz
supported-platforms:
supported-operating-systems:
Linux:
Expand Down Expand Up @@ -169,6 +169,11 @@ sdks:
license-url: https://github.com/aio-libs/aiohttp/blob/master/LICENSE.txt
is-required: Required
changelog:
- date: 2024-11-19
version: v9.1.0
changes:
- type: feature
text: "Add custom message type support for the following APIs: Publish, signal, share file, subscribe and history."
- date: 2024-10-02
version: v9.0.0
changes:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v9.1.0
November 19 2024

#### Added
- Add custom message type support for the following APIs: Publish, signal, share file, subscribe and history.

## v9.0.0
October 02 2024

Expand Down
12 changes: 11 additions & 1 deletion pubnub/endpoints/fetch_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class FetchMessages(Endpoint):

def __init__(self, pubnub, channels: Union[str, List[str]] = None, start: int = None, end: int = None,
count: int = None, include_meta: bool = None, include_message_actions: bool = None,
include_message_type: bool = None, include_uuid: bool = None, decrypt_messages: bool = False):
include_message_type: bool = None, include_uuid: bool = None, decrypt_messages: bool = False,
include_custom_message_type: bool = None):
Endpoint.__init__(self, pubnub)
self._channels = []
if channels:
Expand All @@ -46,6 +47,7 @@ def __init__(self, pubnub, channels: Union[str, List[str]] = None, start: int =
self._include_message_type = include_message_type
self._include_uuid = include_uuid
self._decrypt_messages = decrypt_messages
self._include_custom_message_type = include_custom_message_type

def channels(self, channels: Union[str, List[str]]) -> 'FetchMessages':
utils.extend_list(self._channels, channels)
Expand Down Expand Up @@ -84,6 +86,11 @@ def include_message_type(self, include_message_type: bool) -> 'FetchMessages':
self._include_message_type = include_message_type
return self

def include_custom_message_type(self, include_custom_message_type: bool) -> 'FetchMessages':
assert isinstance(include_custom_message_type, bool)
self._include_custom_message_type = include_custom_message_type
return self

def include_uuid(self, include_uuid: bool) -> 'FetchMessages':
assert isinstance(include_uuid, bool)
self._include_uuid = include_uuid
Expand All @@ -108,6 +115,9 @@ def custom_params(self):
if self._include_message_type is not None:
params['include_message_type'] = "true" if self._include_message_type else "false"

if self._include_custom_message_type is not None:
params['include_custom_message_type'] = "true" if self._include_custom_message_type else "false"

if self.include_message_actions and self._include_uuid is not None:
params['include_uuid'] = "true" if self._include_uuid else "false"

Expand Down
9 changes: 9 additions & 0 deletions pubnub/endpoints/file_operations/publish_file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, pubnub):
self._cipher_key = None
self._replicate = None
self._ptto = None
self._custom_message_type = None

def meta(self, meta):
self._meta = meta
Expand Down Expand Up @@ -53,6 +54,10 @@ def file_name(self, file_name):
self._file_name = file_name
return self

def custom_message_type(self, custom_message_type: str) -> 'PublishFileMessage':
self._custom_message_type = custom_message_type
return self

def _encrypt_message(self, message):
if self._cipher_key:
return PubNubCryptodome(self._pubnub.config).encrypt(self._cipher_key, utils.write_value_as_string(message))
Expand Down Expand Up @@ -90,6 +95,10 @@ def custom_params(self):
"ttl": self._ttl,
"store": 1 if self._should_store else 0
})

if self._custom_message_type:
params['custom_message_type'] = utils.url_encode(self._custom_message_type)

return params

def is_auth_required(self):
Expand Down
34 changes: 20 additions & 14 deletions pubnub/endpoints/file_operations/send_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ def __init__(self, pubnub):
self._file_object = None
self._replicate = None
self._ptto = None
self._custom_message_type = None

def file_object(self, fd):
self._file_object = fd
return self

def custom_message_type(self, custom_message_type: str):
self._custom_message_type = custom_message_type
return self

def build_params_callback(self):
return lambda a: {}

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

def sync(self):
self._file_upload_envelope = FetchFileUploadS3Data(self._pubnub).\
channel(self._channel).\
file_name(self._file_name).sync()
self._file_upload_envelope = FetchFileUploadS3Data(self._pubnub) \
.channel(self._channel) \
.file_name(self._file_name).sync()

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

publish_file_response = PublishFileMessage(self._pubnub).\
channel(self._channel).\
meta(self._meta).\
message(self._message).\
file_id(response_envelope.result.file_id).\
file_name(response_envelope.result.name).\
should_store(self._should_store).\
ttl(self._ttl).\
replicate(self._replicate).\
ptto(self._ptto).\
cipher_key(self._cipher_key).sync()
publish_file_response = PublishFileMessage(self._pubnub) \
.channel(self._channel) \
.meta(self._meta) \
.message(self._message) \
.file_id(response_envelope.result.file_id) \
.file_name(response_envelope.result.name) \
.should_store(self._should_store) \
.ttl(self._ttl) \
.replicate(self._replicate) \
.ptto(self._ptto) \
.custom_message_type(self._custom_message_type) \
.cipher_key(self._cipher_key).sync()

response_envelope.result.timestamp = publish_file_response.result.timestamp
return response_envelope
Expand Down
14 changes: 11 additions & 3 deletions pubnub/endpoints/pubsub/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ class Publish(Endpoint, TimeTokenOverrideMixin):
_ptto: Optional[int]
_ttl: Optional[int]

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

super(Publish, self).__init__(pubnub)
self._channel = channel
self._message = message
self._should_store = should_store
self._use_post = use_post
self._meta = meta
self._custom_message_type = custom_message_type
self._replicate = replicate
self._ptto = ptto
self._ttl = ttl
Expand Down Expand Up @@ -70,6 +71,10 @@ def meta(self, meta: any) -> 'Publish':
self._meta = meta
return self

def custom_message_type(self, custom_message_type: str) -> 'Publish':
self._custom_message_type = custom_message_type
return self

def ttl(self, ttl: int) -> 'Publish':
self._ttl = ttl
return self
Expand Down Expand Up @@ -105,6 +110,9 @@ def custom_params(self):
if self._meta:
params['meta'] = utils.write_value_as_string(self._meta)

if self._custom_message_type:
params['custom_message_type'] = utils.url_encode(self._custom_message_type)

if self._should_store is not None:
if self._should_store:
params["store"] = "1"
Expand Down
14 changes: 12 additions & 2 deletions pubnub/endpoints/signal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
from pubnub import utils
from pubnub.endpoints.endpoint import Endpoint
from pubnub.enums import HttpMethod, PNOperationType
Expand All @@ -17,10 +18,11 @@ class Signal(Endpoint):
_channel: str
_message: any

def __init__(self, pubnub, channel: str = None, message: any = None):
def __init__(self, pubnub, channel: str = None, message: any = None, custom_message_type: Optional[str] = None):
Endpoint.__init__(self, pubnub)
self._channel = channel
self._message = message
self._custom_message_type = custom_message_type

def channel(self, channel) -> 'Signal':
self._channel = str(channel)
Expand All @@ -30,6 +32,10 @@ def message(self, message) -> 'Signal':
self._message = message
return self

def custom_message_type(self, custom_message_type: str) -> 'Signal':
self._custom_message_type = custom_message_type
return self

def build_path(self):
stringified_message = utils.write_value_as_string(self._message)
msg = utils.url_encode(stringified_message)
Expand All @@ -39,7 +45,11 @@ def build_path(self):
)

def custom_params(self):
return {}
params = {}
if self._custom_message_type:
params['custom_message_type'] = utils.url_encode(self._custom_message_type)

return params

def http_method(self):
return HttpMethod.GET
Expand Down
4 changes: 3 additions & 1 deletion pubnub/models/consumer/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


class PNMessageResult(object):
def __init__(self, message, subscription, channel, timetoken, user_metadata=None, publisher=None, error=None):
def __init__(self, message, subscription, channel, timetoken, user_metadata=None, publisher=None,
error=None, custom_message_type=None):

if subscription is not None:
assert isinstance(subscription, str)
Expand Down Expand Up @@ -30,6 +31,7 @@ def __init__(self, message, subscription, channel, timetoken, user_metadata=None
self.user_metadata = user_metadata
self.publisher = publisher
self.error = error
self.custom_message_type = custom_message_type


class PNSignalMessageResult(PNMessageResult):
Expand Down
3 changes: 3 additions & 0 deletions pubnub/models/server/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self):
self.publish_metadata = None
self.only_channel_subscription = False
self.type = 0
self.custom_message_type = None

@classmethod
def from_json(cls, json_input):
Expand All @@ -49,6 +50,8 @@ def from_json(cls, json_input):
message.publish_metadata = PublishMetadata.from_json(json_input['p'])
if 'e' in json_input:
message.type = json_input['e']
if 'cmt' in json_input:
message.custom_message_type = json_input['cmt']
return message


Expand Down
11 changes: 6 additions & 5 deletions pubnub/pubnub_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

class PubNubCore:
"""A base class for PubNub Python API implementations"""
SDK_VERSION = "9.0.0"
SDK_VERSION = "9.1.0"
SDK_NAME = "PubNub-Python"

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

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

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

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

def set_uuid_metadata(self, uuid: str = None, include_custom: bool = None, custom: dict = None,
include_status: bool = True, include_type: bool = True, status: str = None, type: str = None,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='pubnub',
version='9.0.0',
version='9.1.0',
description='PubNub Real-time push service in the cloud',
author='PubNub',
author_email='[email protected]',
Expand Down
Loading
Loading