Skip to content

Commit a63db34

Browse files
Add Calloptions to publish and notify api (#50)
* Add Calloptions to publish and notify api * Incorportate reviewer comment
1 parent 4c00ec8 commit a63db34

File tree

8 files changed

+68
-27
lines changed

8 files changed

+68
-27
lines changed

tests/test_communication/test_simplenotifier.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ def create_destination_uri(self):
3232

3333
async def test_send_notification(self):
3434
notifier = SimpleNotifier(MockUTransport())
35-
status = await notifier.notify(self.create_topic(), self.create_destination_uri(), None)
35+
status = await notifier.notify(self.create_topic(), self.create_destination_uri())
3636
self.assertEqual(status.code, UCode.OK)
3737

3838
async def test_send_notification_with_payload(self):
3939
uri = UUri(authority_name="Neelam")
4040
notifier = SimpleNotifier(MockUTransport())
41-
status = await notifier.notify(self.create_topic(), self.create_destination_uri(), UPayload.pack(uri))
41+
status = await notifier.notify(self.create_topic(), self.create_destination_uri(), payload=UPayload.pack(uri))
4242
self.assertEqual(status.code, UCode.OK)
4343

4444
async def test_register_listener(self):

tests/test_communication/test_simplepublisher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ def create_topic(self):
2828

2929
async def test_send_publish(self):
3030
publisher = SimplePublisher(MockUTransport())
31-
status = await publisher.publish(self.create_topic(), None)
31+
status = await publisher.publish(self.create_topic())
3232
self.assertEqual(status.code, UCode.OK)
3333

3434
async def test_send_publish_with_stuffed_payload(self):
3535
uri = UUri(authority_name="Neelam")
3636
publisher = SimplePublisher(MockUTransport())
37-
status = await publisher.publish(self.create_topic(), UPayload.pack_to_any(uri))
37+
status = await publisher.publish(self.create_topic(), payload=UPayload.pack_to_any(uri))
3838
self.assertEqual(status.code, UCode.OK)
3939

4040
def test_constructor_transport_none(self):
@@ -52,7 +52,7 @@ async def test_publish_topic_none(self):
5252
uri = UUri(authority_name="Neelam")
5353

5454
with self.assertRaises(ValueError) as context:
55-
await publisher.publish(None, UPayload.pack_to_any(uri))
55+
await publisher.publish(None, payload=UPayload.pack_to_any(uri))
5656
self.assertEqual(str(context.exception), "Publish topic missing")
5757

5858

tests/test_communication/test_uclient.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ def test_create_upclient_with_null_transport(self):
5151
UClient(None)
5252

5353
async def test_send_notification(self):
54-
status = await UClient(MockUTransport()).notify(create_topic(), create_destination_uri(), None)
54+
status = await UClient(MockUTransport()).notify(create_topic(), create_destination_uri())
5555
self.assertEqual(status.code, UCode.OK)
5656

5757
async def test_send_notification_with_payload(self):
5858
uri = UUri(authority_name="neelam")
59-
status = await UClient(MockUTransport()).notify(create_topic(), create_destination_uri(), UPayload.pack(uri))
59+
status = await UClient(MockUTransport()).notify(
60+
create_topic(), create_destination_uri(), payload=UPayload.pack(uri)
61+
)
6062
self.assertEqual(status.code, UCode.OK)
6163

6264
async def test_register_listener(self):
@@ -85,19 +87,24 @@ async def test_unregister_listener_not_registered(self):
8587
self.assertEqual(status.code, UCode.INVALID_ARGUMENT)
8688

8789
async def test_send_publish(self):
88-
status = await UClient(MockUTransport()).publish(create_topic(), None)
90+
status = await UClient(MockUTransport()).publish(create_topic())
8991
self.assertEqual(status.code, UCode.OK)
9092

9193
async def test_send_publish_with_stuffed_payload(self):
9294
uri = UUri(authority_name="neelam")
93-
status = await UClient(MockUTransport()).publish(create_topic(), UPayload.pack_to_any(uri))
95+
status = await UClient(MockUTransport()).publish(create_topic(), payload=UPayload.pack_to_any(uri))
96+
self.assertEqual(status.code, UCode.OK)
97+
98+
async def test_send_publish_with_stuffed_payload_and_calloptions(self):
99+
uri = UUri(authority_name="neelam")
100+
status = await UClient(MockUTransport()).publish(
101+
create_topic(), CallOptions(token="134"), payload=UPayload.pack_to_any(uri)
102+
)
94103
self.assertEqual(status.code, UCode.OK)
95104

96105
async def test_invoke_method_with_payload(self):
97106
payload = UPayload.pack_to_any(UUri())
98-
future_result = asyncio.ensure_future(
99-
UClient(MockUTransport()).invoke_method(create_method_uri(), payload, None)
100-
)
107+
future_result = asyncio.ensure_future(UClient(MockUTransport()).invoke_method(create_method_uri(), payload))
101108
response = await future_result
102109
self.assertIsNotNone(response)
103110
self.assertFalse(future_result.exception())
@@ -132,10 +139,10 @@ async def test_invoke_method_with_multi_invoke_transport(self):
132139
rpc_client = UClient(MockUTransport())
133140
payload = UPayload.pack_to_any(UUri())
134141

135-
future_result1 = asyncio.ensure_future(rpc_client.invoke_method(create_method_uri(), payload, None))
142+
future_result1 = asyncio.ensure_future(rpc_client.invoke_method(create_method_uri(), payload))
136143
response = await future_result1
137144
self.assertIsNotNone(response)
138-
future_result2 = asyncio.ensure_future(rpc_client.invoke_method(create_method_uri(), payload, None))
145+
future_result2 = asyncio.ensure_future(rpc_client.invoke_method(create_method_uri(), payload))
139146
response2 = await future_result2
140147

141148
self.assertIsNotNone(response2)
@@ -195,7 +202,7 @@ async def test_request_handler_for_notification(self):
195202
handler = create_autospec(RequestHandler, instance=True)
196203

197204
await client.register_request_handler(create_method_uri(), handler)
198-
self.assertEqual(await client.notify(create_topic(), transport.get_source(), None), UStatus(code=UCode.OK))
205+
self.assertEqual(await client.notify(create_topic(), transport.get_source()), UStatus(code=UCode.OK))
199206

200207

201208
def create_topic():

uprotocol/communication/notifier.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"""
1414

1515
from abc import ABC, abstractmethod
16+
from typing import Optional
1617

18+
from uprotocol.communication.calloptions import CallOptions
1719
from uprotocol.communication.upayload import UPayload
1820
from uprotocol.transport.ulistener import UListener
1921
from uprotocol.v1.uri_pb2 import UUri
@@ -29,12 +31,15 @@ class Notifier(ABC):
2931
"""
3032

3133
@abstractmethod
32-
async def notify(self, topic: UUri, destination: UUri, payload: UPayload) -> UStatus:
34+
async def notify(
35+
self, topic: UUri, destination: UUri, options: Optional[CallOptions] = None, payload: Optional[UPayload] = None
36+
) -> UStatus:
3337
"""
34-
Send a notification to a given topic passing a payload.
38+
Send a notification to a given topic.
3539
3640
:param topic: The topic to send the notification to.
3741
:param destination: The destination to send the notification to.
42+
:param options: Call options for the notification.
3843
:param payload: The payload to send with the notification.
3944
:return: Returns the UStatus with the status of the notification.
4045
"""

uprotocol/communication/publisher.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"""
1414

1515
from abc import ABC, abstractmethod
16+
from typing import Optional
1617

18+
from uprotocol.communication.calloptions import CallOptions
1719
from uprotocol.communication.upayload import UPayload
1820
from uprotocol.v1.uri_pb2 import UUri
1921
from uprotocol.v1.ustatus_pb2 import UStatus
@@ -30,11 +32,14 @@ class Publisher(ABC):
3032
"""
3133

3234
@abstractmethod
33-
async def publish(self, topic: UUri, payload: UPayload) -> UStatus:
35+
async def publish(
36+
self, topic: UUri, options: Optional[CallOptions] = None, payload: Optional[UPayload] = None
37+
) -> UStatus:
3438
"""
35-
Publish a message to a topic passing UPayload as the payload.
39+
Publish a message to a topic.
3640
3741
:param topic: The topic to publish to.
42+
:param options: Call options for the publish.
3843
:param payload: The UPayload to publish.
3944
:return: An instance of UStatus indicating the status of the publish operation.
4045
"""

uprotocol/communication/simplenotifier.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from typing import Optional
1616

17+
from uprotocol.communication.calloptions import CallOptions
1718
from uprotocol.communication.notifier import Notifier
1819
from uprotocol.communication.upayload import UPayload
1920
from uprotocol.transport.builder.umessagebuilder import UMessageBuilder
@@ -45,16 +46,23 @@ def __init__(self, transport: UTransport):
4546
raise ValueError(UTransport.TRANSPORT_NOT_INSTANCE_ERROR)
4647
self.transport = transport
4748

48-
async def notify(self, topic: UUri, destination: UUri, payload: Optional[UPayload] = None) -> UStatus:
49+
async def notify(
50+
self, topic: UUri, destination: UUri, options: Optional[CallOptions] = None, payload: Optional[UPayload] = None
51+
) -> UStatus:
4952
"""
5053
Send a notification to a given topic.
5154
5255
:param topic: The topic to send the notification to.
5356
:param destination: The destination to send the notification to.
57+
:param options: Call options for the notification.
5458
:param payload: The payload to send with the notification.
5559
:return: Returns the UStatus with the status of the notification.
5660
"""
5761
builder = UMessageBuilder.notification(topic, destination)
62+
if options:
63+
builder.with_priority(options.priority)
64+
builder.with_ttl(options.timeout)
65+
builder.with_token(options.token)
5866
return await self.transport.send(builder.build() if payload is None else builder.build_from_upayload(payload))
5967

6068
async def register_notification_listener(self, topic: UUri, listener: UListener) -> UStatus:

uprotocol/communication/simplepublisher.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
SPDX-License-Identifier: Apache-2.0
1313
"""
1414

15+
from typing import Optional
16+
17+
from uprotocol.communication.calloptions import CallOptions
1518
from uprotocol.communication.publisher import Publisher
1619
from uprotocol.communication.upayload import UPayload
1720
from uprotocol.transport.builder.umessagebuilder import UMessageBuilder
@@ -33,16 +36,23 @@ def __init__(self, transport: UTransport):
3336
raise ValueError(UTransport.TRANSPORT_NOT_INSTANCE_ERROR)
3437
self.transport = transport
3538

36-
async def publish(self, topic: UUri, payload: UPayload) -> UStatus:
39+
async def publish(
40+
self, topic: UUri, options: Optional[CallOptions] = None, payload: Optional[UPayload] = None
41+
) -> UStatus:
3742
"""
3843
Publishes a message to a topic using the provided payload.
3944
4045
:param topic: The topic to publish the message to.
46+
:param options: Call options for the publish.
4147
:param payload: The payload to be published.
4248
:return: An instance of UStatus indicating the status of the publish operation.
4349
"""
4450
if topic is None:
4551
raise ValueError("Publish topic missing")
4652

47-
message = UMessageBuilder.publish(topic).build_from_upayload(payload)
48-
return await self.transport.send(message)
53+
builder = UMessageBuilder.publish(topic)
54+
if options:
55+
builder.with_priority(options.priority)
56+
builder.with_ttl(options.timeout)
57+
builder.with_token(options.token)
58+
return await self.transport.send(builder.build_from_upayload(payload))

uprotocol/communication/uclient.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,19 @@ async def unregister_listener(self, topic: UUri, listener: UListener) -> UStatus
108108
"""
109109
return await self.subscriber.unregister_listener(topic, listener)
110110

111-
async def notify(self, topic: UUri, destination: UUri, payload: UPayload) -> UStatus:
111+
async def notify(
112+
self, topic: UUri, destination: UUri, options: Optional[CallOptions] = None, payload: Optional[UPayload] = None
113+
) -> UStatus:
112114
"""
113115
Send a notification to a given topic.
114116
115117
:param topic: The topic to send the notification to.
116118
:param destination: The destination to send the notification to.
119+
:param options: Call options for the notification.
117120
:param payload: The payload to send with the notification.
118121
:return: Returns the UStatus with the status of the notification.
119122
"""
120-
return await self.notifier.notify(topic, destination, payload)
123+
return await self.notifier.notify(topic, destination, options, payload)
121124

122125
async def register_notification_listener(self, topic: UUri, listener: UListener) -> UStatus:
123126
"""
@@ -139,15 +142,18 @@ async def unregister_notification_listener(self, topic: UUri, listener: UListene
139142
"""
140143
return await self.notifier.unregister_notification_listener(topic, listener)
141144

142-
async def publish(self, topic: UUri, payload: UPayload) -> UStatus:
145+
async def publish(
146+
self, topic: UUri, options: Optional[CallOptions] = None, payload: Optional[UPayload] = None
147+
) -> UStatus:
143148
"""
144149
Publishes a message to a topic using the provided payload.
145150
146151
:param topic: The topic to publish the message to.
152+
:param options: Call options for the publish.
147153
:param payload: The payload to be published.
148154
:return: An instance of UStatus indicating the status of the publish operation.
149155
"""
150-
return await self.publisher.publish(topic, payload)
156+
return await self.publisher.publish(topic, options, payload)
151157

152158
async def register_request_handler(self, method: UUri, handler):
153159
"""

0 commit comments

Comments
 (0)