@@ -64,3 +64,173 @@ Use the `iop` command line to register your component:
6464``` bash
6565iop --migrate /path/to/your/project/setting.py
6666```
67+
68+ ## Business Service
69+
70+ Two kinds of business services can be created in Python:
71+
72+ - Business Service
73+ - Pulling Business Service
74+
75+ ### Business Service
76+
77+ To create a business service, use the following code:
78+
79+ ``` python
80+ from iop import BusinessService
81+
82+ class MyBusinessService (BusinessService ):
83+
84+ def on_process_input (self , message_input : ' MyRequest' ):
85+ # This method is called when the service is called
86+ self .log_info(" [Python] MyBusinessService:on_process_input() is called with message: " + message_input.request_string)
87+ response = MyResponse(" MyBusinessService:on_process_input() echos" )
88+ return response
89+ ```
90+
91+ ### Pulling Business Service
92+
93+ To create a business service that runs every 5 seconds, use the following code:
94+
95+ ``` python
96+ from iop import BusinessService
97+
98+ class MyBusinessService (BusinessService ):
99+
100+ def get_adapter_type ():
101+ # This is mandatory to schedule the service
102+ # By default, the service will be scheduled every 5 seconds
103+ return " Ens.InboundAdapter"
104+
105+ def on_process_input (self ):
106+ # This method is called every 5 seconds
107+ self .log_info(" [Python] MyBusinessService:on_process_input() is called" )
108+ ```
109+
110+ ## Flask app sending a message to an Business Service
111+
112+ To send a message to a business service, use the following code:
113+
114+
115+ ``` python
116+ from flask import Flask, request
117+ from iop import Director
118+
119+ app = Flask(__name__ )
120+
121+ director = Director()
122+
123+ @app.route (' /send_message' , methods = [' POST' ])
124+ def send_message ():
125+ message = request.json
126+ bs = director.get_business_service(" Python.MyBusinessService" )
127+ resp = bs.on_process_input(message)
128+ return resp
129+ ```
130+
131+ ## Business Process
132+
133+ To create a business process, use the following code:
134+
135+ ``` python
136+ from iop import BusinessProcess
137+
138+ class MyBusinessProcess (BusinessProcess ):
139+
140+ def on_message (self , message_input : ' MyRequest' ):
141+ # Called from service/process/operation, message is of type MyRequest with property request_string
142+ self .log_info(" [Python] MyBusinessProcess:on_message() is called with message: " + message_input.request_string)
143+ response = MyResponse(" MyBusinessProcess:on_message() echos" )
144+ return response
145+ ```
146+
147+ ### Async calls
148+
149+ There is three ways to make async calls in Python:
150+
151+ 1 . Using the ` asyncio ` library.
152+ 2 . Using the native ` send_request_async ` method.
153+ 3 . Using the ` send_multi_request_sync ` method.
154+
155+ #### Using the ` asyncio ` library
156+
157+ To make an async call with asyncio, use the following code:
158+
159+ ``` python
160+ import asyncio
161+ import random
162+
163+ from iop import BusinessProcess
164+ from msg import MyMessage
165+
166+
167+ class MyAsyncNGBP (BusinessProcess ):
168+
169+ def on_message (self , request ):
170+
171+ results = asyncio.run(self .await_response(request))
172+
173+ for result in results:
174+ self .log_info(f " Received response: { result.message} " )
175+
176+ async def await_response (self , request ):
177+ # create 1 to 10 messages
178+ tasks = []
179+ for i in range (random.randint(1 , 10 )):
180+ tasks.append(self .send_request_async_ng(" Python.MyAsyncNGBO" ,
181+ MyMessage(message = f " Message { i} " )))
182+
183+ return await asyncio.gather(* tasks)
184+ ```
185+
186+ #### Using the native ` send_request_async ` method
187+
188+ To make an async call with the native ` send_request_async ` method, use the following code:
189+
190+ ``` python
191+ from grongier.pex import BusinessProcess
192+ from msg import MyMessage
193+
194+
195+ class MyBP (BusinessProcess ):
196+
197+ def on_message (self , request ):
198+ msg_one = MyMessage(message = " Message1" )
199+ msg_two = MyMessage(message = " Message2" )
200+
201+ self .send_request_async(" Python.MyBO" , msg_one,completion_key = " 1" )
202+ self .send_request_async(" Python.MyBO" , msg_two,completion_key = " 2" )
203+
204+ def on_response (self , request , response , call_request , call_response , completion_key ):
205+ if completion_key == " 1" :
206+ self .response_one = call_response
207+ elif completion_key == " 2" :
208+ self .response_two = call_response
209+
210+ def on_complete (self , request , response ):
211+ self .log_info(f " Received response one: { self .response_one.message} " )
212+ self .log_info(f " Received response two: { self .response_two.message} " )
213+ ```
214+
215+ #### Using the ` send_multi_request_sync ` method
216+
217+ To make an async call with the ` send_multi_request_sync ` method, use the following code:
218+
219+ ``` python
220+ from iop import BusinessProcess
221+ from msg import MyMessage
222+
223+
224+ class MyMultiBP (BusinessProcess ):
225+
226+ def on_message (self , request ):
227+ msg_one = MyMessage(message = " Message1" )
228+ msg_two = MyMessage(message = " Message2" )
229+
230+ tuple_responses = self .send_multi_request_sync([(" Python.MyMultiBO" , msg_one),
231+ (" Python.MyMultiBO" , msg_two)])
232+
233+ self .log_info(" All requests have been processed" )
234+ for target,request,response,status in tuple_responses:
235+ self .log_info(f " Received response: { response.message} " )
236+ ```
0 commit comments