Skip to content

Commit 6afbd3d

Browse files
committed
Add Python API documentation for core classes and methods
1 parent a8fd3dd commit 6afbd3d

File tree

1 file changed

+311
-0
lines changed

1 file changed

+311
-0
lines changed

docs/python-api.md

Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
# Python API Documentation
2+
3+
## Core Classes
4+
5+
### Message 📦
6+
Base class for messages passed between components. This class provides the foundation for all message types in the interoperability framework.
7+
8+
**Usage:** Subclass `Message` and decorate with `@dataclass` to define message properties. This approach provides type hints and automatic initialization.
9+
10+
**Example:**
11+
```python
12+
from iop import Message
13+
from dataclasses import dataclass
14+
15+
@dataclass
16+
class MyRequest(Message):
17+
request_string: str = None
18+
```
19+
20+
### BusinessService 🔄
21+
Base class for business services that receive and process incoming data. Business services act as entry points for data into your interoperability solution.
22+
23+
**Key Methods:**
24+
25+
- `on_process_input(self, message_input: Message) -> None`
26+
- Handles incoming messages from adapter
27+
- Parameters:
28+
- `message_input`: The incoming message to process
29+
- Returns: None
30+
31+
- `send_request_sync(self, target: str, request: Message, timeout: int = -1) -> Message`
32+
- Sends a synchronous request and waits for response
33+
- Parameters:
34+
- `target`: Name of the target component
35+
- `request`: Message to send
36+
- `timeout`: Maximum wait time in seconds (-1 for default)
37+
- Returns: Response message
38+
39+
- `send_request_async(self, target: str, request: Message) -> None`
40+
- Sends an asynchronous request without waiting
41+
- Parameters:
42+
- `target`: Name of the target component
43+
- `request`: Message to send
44+
- Returns: None
45+
46+
**Basic Example:**
47+
```python
48+
from iop import BusinessService
49+
50+
class MyService(BusinessService):
51+
def on_process_input(self, message_input):
52+
self.log_info(f"Received: {message_input}")
53+
```
54+
55+
**Advanced Example with Adapter:**
56+
```python
57+
from iop import BusinessService, Message
58+
from dataclasses import dataclass
59+
60+
@dataclass
61+
class MyRequest(Message):
62+
file_path: str = None
63+
data: str = None
64+
65+
class MyService(BusinessService):
66+
def get_adapter_type():
67+
"""Enable pull mode for the BusinessService"""
68+
return "Ens.InboundAdapter"
69+
70+
def on_process_input(self, message_input):
71+
self.log_info(f"Received: {message_input}")
72+
with open(message_input.file_path, 'r') as file:
73+
data = file.read()
74+
request = MyRequest(data=data)
75+
self.send_request_async("MyBusinessOperation", request)
76+
```
77+
78+
### BusinessOperation 🔧
79+
Base class for business operations that process requests and perform specific business logic.
80+
81+
**Key Methods:**
82+
83+
- `on_message(self, request: Message) -> Message`
84+
- Process incoming request messages
85+
- Parameters:
86+
- `request`: The incoming message to process
87+
- Returns: Response message
88+
89+
- `send_request_sync(self, target: str, request: Message, timeout: int = -1) -> Message`
90+
- Send synchronous request and wait for response
91+
- Parameters and returns same as BusinessService
92+
93+
- `send_request_async(self, target: str, request: Message) -> None`
94+
- Send asynchronous request without waiting
95+
- Parameters and returns same as BusinessService
96+
97+
**Example:**
98+
```python
99+
from iop import BusinessOperation, Message
100+
from dataclasses import dataclass
101+
102+
@dataclass
103+
class MyRequest(Message):
104+
request_string: str = None
105+
106+
@dataclass
107+
class MyResponse(Message):
108+
my_string: str = None
109+
110+
class MyOperation(BusinessOperation):
111+
def on_message(self, request):
112+
self.log_info(f"Received: {request}")
113+
return MyResponse(my_string="Hello World")
114+
```
115+
116+
### BusinessProcess ‍💼
117+
Base class for business processes that orchestrate message flow between components.
118+
119+
**Key Methods:**
120+
121+
- `on_request(self, request: Message) -> None`
122+
- Handle initial incoming requests
123+
- Parameters:
124+
- `request`: The incoming request to process
125+
- Returns: None
126+
127+
- `on_response(self, request: Message, response: Message, call_request: Message, call_response: Message, completion_key: str) -> None`
128+
- Handle asynchronous responses
129+
- Parameters:
130+
- `request`: Original request
131+
- `response`: Current response
132+
- `call_request`: Request that generated this response
133+
- `call_response`: The response being processed
134+
- `completion_key`: Unique identifier for the response chain
135+
- Returns: None
136+
137+
- `on_complete(self, request: Message, response: Message) -> None`
138+
- Called after all responses are received
139+
- Parameters:
140+
- `request`: Original request
141+
- `response`: Final response
142+
- Returns: None
143+
144+
- `reply(self, response: Message) -> None`
145+
- Send response back to the caller
146+
- Parameters:
147+
- `response`: Response message to send
148+
- Returns: None
149+
150+
**Example:**
151+
```python
152+
from iop import BusinessProcess, Message
153+
from dataclasses import dataclass
154+
155+
@dataclass
156+
class MyRequest(Message):
157+
request_string: str = None
158+
159+
@dataclass
160+
class MyResponse(Message):
161+
my_string: str = None
162+
163+
class MyProcess(BusinessProcess):
164+
def on_request(self, request):
165+
self.log_info(f"Received: {request}")
166+
self.send_request_async("MyBusinessOperation", request)
167+
168+
def on_response(self, request, response, call_request, call_response, completion_key):
169+
self.log_info(f"Received: {response}")
170+
self.reply(response)
171+
```
172+
173+
### Adapter Classes 🔌
174+
175+
#### InboundAdapter
176+
Base class for adapters that receive external data.
177+
178+
**Key Methods:**
179+
180+
- `on_task(self) -> None`
181+
- Called at configured intervals to check for new data
182+
- Override this method to implement custom data acquisition logic
183+
- Returns: None
184+
185+
#### OutboundAdapter
186+
Base class for adapters that send data to external systems.
187+
188+
**Key Methods:**
189+
190+
- `on_keepalive(self) -> None`
191+
- Called periodically to maintain external connections
192+
- Implement connection maintenance logic here
193+
- Returns: None
194+
195+
196+
### Common Methods 🛠️
197+
Available in all component classes:
198+
199+
**Logging Methods:**
200+
201+
- `log_info(self, message: str) -> None`
202+
- Log informational message for general information
203+
204+
- `log_error(self, message: str) -> None`
205+
- Log error message for errors and exceptions
206+
207+
- `log_warning(self, message: str) -> None`
208+
- Log warning message for potential issues
209+
210+
- `log_alert(self, message: str) -> None`
211+
- Log alert message for critical situations
212+
213+
**Lifecycle Methods:**
214+
215+
- `on_init(self) -> None`
216+
- Initialize component when it starts
217+
- Override to add custom initialization
218+
219+
- `on_tear_down(self) -> None`
220+
- Clean up resources when component stops
221+
- Override to add custom cleanup logic
222+
223+
- `on_connected(self) -> None`
224+
- Handle connection setup when connections are established
225+
- Override to add custom connection logic
226+
227+
228+
### Director Class 🎭
229+
Manages InterSystems IRIS productions and business services, particularly for non-polling services.
230+
231+
**Key Methods:**
232+
233+
**Production Management:**
234+
235+
- `start_production(production_name: str = None) -> None`
236+
- Start a production
237+
- If no name provided, uses default production
238+
239+
- `stop_production() -> None`
240+
- Stop the currently running production
241+
242+
- `restart_production() -> None`
243+
- Restart the current production
244+
245+
- `shutdown_production() -> None`
246+
- Gracefully shutdown the production
247+
248+
- `status_production() -> dict`
249+
- Get current production status
250+
- Returns dictionary with production details
251+
252+
**Business Service Management:**
253+
254+
- `create_business_service(target: str) -> object`
255+
- Create an instance of a business service
256+
- Parameters:
257+
- `target`: Name of the business service in production
258+
- Returns: Business service instance
259+
260+
- `get_business_service(target: str) -> object`
261+
- Get an existing business service instance
262+
- Parameters:
263+
- `target`: Name of the business service in production
264+
- Returns: Business service instance
265+
266+
- `test_component(target: str, message=None, classname: str=None, body=None) -> object`
267+
- Test a production component
268+
- Parameters:
269+
- `target`: Component name
270+
- `message`: Optional message instance
271+
- `classname`: Optional message class name
272+
- `body`: Optional message body
273+
- Returns: Component response
274+
275+
**Production Logging:**
276+
277+
- `log_production() -> None`
278+
- Start real-time production log monitoring
279+
- Press Ctrl+C to stop
280+
281+
- `log_production_top(top: int) -> None`
282+
- Display last N log entries
283+
- Parameters:
284+
- `top`: Number of entries to show
285+
286+
**Production Configuration:**
287+
288+
- `set_default_production(production_name: str) -> None`
289+
- Set the default production name
290+
291+
- `get_default_production() -> str`
292+
- Get the current default production name
293+
294+
**Example Usage:**
295+
296+
In a flask application :
297+
298+
```python
299+
from iop import Director
300+
301+
from flask import Flask
302+
303+
app = Flask(__name__)
304+
305+
director = Director()
306+
307+
@app.route('/')
308+
def hello_world():
309+
bs = director.get_business_service("MyBusinessService")
310+
return bs.on_process_input("Hello, World!")
311+
```

0 commit comments

Comments
 (0)