@@ -26,17 +26,23 @@ The Mercury framework is 100% event-driven and all communications are asynchrono
2626it suspends the calling function and uses temporary Inbox as a callback function. The called function will send
2727the reply to the callback function which in turns wakes up the calling function.
2828
29- To make a RPC call, you can use the ` request ` method.
29+ To make a RPC call, you can use the ` request ` or the ` send_request ` method.
30+ With the latter, you can send the request as an event and set event metadata such as correlation-ID.
3031
3132``` python
32- request(self , route: str , timeout_seconds: float ,
33- headers: dict = None , body: any = None ,
34- correlation_id: str = None ) -> EventEnvelope
33+ def request (self , route : str , timeout_seconds : float ,
34+ headers : dict = None , body : any = None ,
35+ correlation_id : str = None ) -> EventEnvelope:
3536
3637# example
37- result = po.request (' hello.world.2' , 2.0 , headers = {' some_key' : ' some_value' }, body = ' hello world' )
38+ result = po.send_request (' hello.world.2' , 2.0 , headers = {' some_key' : ' some_value' }, body = ' hello world' )
3839print (result.get_body())
3940
41+ # send request using an event directly
42+ def send_request (self , event : EventEnvelope, timeout_seconds : float ) -> EventEnvelope:
43+
44+ # example
45+ result = po.send_request(event, 2.0 )
4046```
4147
4248Note that Mercury supports Python primitive or dictionary in the message body. If you put other object, it may throw
@@ -47,7 +53,7 @@ serialization exception or the object may become empty.
4753To make an asynchronous call, use the ` send ` method.
4854
4955``` python
50- send(self , route: str , headers: dict = None , body: any = None , reply_to: str = None , me = True ) -> None
56+ def send (self , route : str , headers : dict = None , body : any = None , reply_to : str = None , me = True ) -> None " "
5157```
5258
5359You may put key- value pairs in the " headers" field for holding parameters. For message payload, put Python primitive
@@ -56,7 +62,7 @@ or dictionary in the "body" field.
5662# ## Deferred delivery
5763
5864```python
59- send_later(self , route: str , headers: dict = None , body: any = None , seconds: float = 1.0 ) -> None
65+ def send_later(self , route: str , headers: dict = None , body: any = None , seconds: float = 1.0 ) -> None :
6066```
6167
6268### Call-back
@@ -148,7 +154,7 @@ Broadcast is the easiest way to do "pub/sub". To broadcast an event to multiple
148154use the ` broadcast ` method.
149155
150156``` python
151- broadcast(self , route: str , headers: dict = None , body: any = None ) -> None
157+ def broadcast (self , route : str , headers : dict = None , body : any = None ) -> None :
152158
153159# example
154160po.broadcast(" hello.world.1" , body = " this is a broadcast message from " + platform.get_origin())
@@ -160,7 +166,7 @@ po.broadcast("hello.world.1", body="this is a broadcast message from "+platform.
160166You can perform join-n-fork RPC calls using a parallel version of the request, ` parallel_request ` method.
161167
162168``` python
163- parallel_request(self , events: list , timeout_seconds: float ) -> list
169+ def parallel_request (self , events : list , timeout_seconds : float ) -> list :
164170
165171# illustrate parallel RPC requests
166172event_list = list ()
172178 print (' Received' , len (result), ' RPC responses:' )
173179 for res in result:
174180 print (" HEADERS =" , res.get_headers(), " , BODY =" , res.get_body(),
175- " , STATUS =" , res.get_status(),
176- " , EXEC =" , res.get_exec_time(), " , ROUND TRIP =" , res.get_round_trip(), " ms" )
181+ " , STATUS =" , res.get_status(),
182+ " , EXEC =" , res.get_exec_time(), " , ROUND TRIP =" , res.get_round_trip(), " ms" )
177183except TimeoutError as e:
178184 print (" Exception: " , str (e))
179185```
@@ -188,14 +194,14 @@ However, if you want to do store-n-forward pub/sub for certain use cases, you ma
188194Following are some useful pub/sub API:
189195
190196``` python
191- def feature_enabled ()
192- def create_topic (topic : str )
193- def delete_topic (topic : str )
194- def publish (topic : str , headers : dict = None , body : any = None )
195- def subscribe (self , topic : str , route : str , parameters : list = None )
196- def unsubscribe (self , topic : str , route : str )
197- def exists (topic : str )
198- def list_topics ()
197+ def feature_enabled ():
198+ def create_topic (topic : str ):
199+ def delete_topic (topic : str ):
200+ def publish (topic : str , headers : dict = None , body : any = None ):
201+ def subscribe (self , topic : str , route : str , parameters : list = None ):
202+ def unsubscribe (self , topic : str , route : str ):
203+ def exists (topic : str ):
204+ def list_topics ():
199205
200206```
201207Some pub/sub engine would require additional parameters when subscribing a topic. For Kafka, you must provide the
@@ -222,7 +228,7 @@ serialization yourself. The payload can be a dict, bool, str, int or float.
222228To check if a target service is available, you can use the ` exists ` method.
223229
224230``` python
225- exists(self , routes: any )
231+ def exists (self , routes : any ) -> bool :
226232
227233# input can be a route name or a list of routes
228234# it will return true only when all routes are available
0 commit comments