Skip to content

Commit 27b07c5

Browse files
authored
Update README.md
1 parent 0f99389 commit 27b07c5

File tree

1 file changed

+100
-9
lines changed

1 file changed

+100
-9
lines changed

README.md

+100-9
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,34 @@
1212
4. You can then run example with `python example.py`.
1313

1414
# Getting started
15+
1516
## Minimal application
16-
```Python
17+
18+
main.py
19+
```python
1720
from aidbox_python_sdk.main import create_app as _create_app
1821
from aidbox_python_sdk.settings import Settings
1922
from aidbox_python_sdk.sdk import SDK
2023

24+
2125
settings = Settings(**{})
22-
sdk = SDK(settings, resources=resources, seeds=seeds)
26+
sdk = SDK(settings, resources={}, seeds={})
27+
28+
29+
def create_app():
30+
app = await _create_app(SDK)
31+
return app
2332

24-
async def create_app():
25-
return await _create_app(sdk)
2633

34+
async def create_gunicorn_app() -> web.Application:
35+
return create_app()
2736
```
2837

2938
## Register handler for operation
30-
```Python
39+
```python
3140
import logging
3241
from aiohttp import web
42+
from aidbox_python_sdk.types import SDKOperation, SDKOperationRequest
3343

3444
from yourappfolder import sdk
3545

@@ -39,7 +49,7 @@ from yourappfolder import sdk
3949
path=["signup", "register", {"name": "date"}, {"name": "test"}],
4050
timeout=60000 ## Optional parameter to set a custom timeout for operation in milliseconds
4151
)
42-
def signup_register_op(operation, request):
52+
def signup_register_op(_operation: SDKOperation, request: SDKOperationRequest):
4353
"""
4454
POST /signup/register/21.02.19/testvalue
4555
PATCH /signup/register/22.02.19/patchtestvalue
@@ -51,8 +61,86 @@ def signup_register_op(operation, request):
5161

5262
```
5363

54-
## Validate request
55-
```Python
64+
## Usage of AppKeys
65+
66+
To access Aidbox Client, SDK, settings, DB Proxy the `app` (`web.Application`) is extended by default with the following app keys that are defined in `aidbox_python_sdk.app_keys` module:
67+
68+
```python
69+
from aidbox_python_sdk import app_keys as ak
70+
from aidbox_python_sdk.types import SDKOperation, SDKOperationRequest
71+
72+
@sdk.operation(["POST"], ["example"])
73+
async def update_organization_op(_operation: SDKOperation, request: SDKOperationRequest):
74+
app = request.app
75+
client = app[ak.client] # AsyncAidboxClient
76+
sdk = app[ak.sdk] # SDK
77+
settings = app[ak.settings] # Settings
78+
db = app[ak.db] # DBProxy
79+
return web.json_response()
80+
```
81+
82+
## Usage of FHIR Client
83+
84+
FHIR Client is not plugged in by default, however, to use it you can extend the app by adding new AppKey
85+
86+
app/app_keys.py
87+
```python
88+
from fhirpy import AsyncFHIRClient
89+
90+
fhir_client: web.AppKey[AsyncFHIRClient] = web.AppKey("fhir_client", AsyncFHIRClient)
91+
```
92+
93+
main.py
94+
```python
95+
from collections.abc import AsyncGenerator
96+
97+
from aidbox_python_sdk.main import create_app as _create_app
98+
from aidbox_python_sdk.settings import Settings
99+
from aidbox_python_sdk.sdk import SDK
100+
from aiohttp import BasicAuth, web
101+
from fhirpy import AsyncFHIRClient
102+
103+
from app import app_keys as ak
104+
105+
settings = Settings(**{})
106+
sdk = SDK(settings, resources={}, seeds={)
107+
108+
def create_app():
109+
app = await _create_app(SDK)
110+
app.cleanup_ctx.append(fhir_clients_ctx)
111+
return app
112+
113+
114+
async def create_gunicorn_app() -> web.Application:
115+
return create_app()
116+
117+
118+
async def fhir_clients_ctx(app: web.Application) -> AsyncGenerator[None, None]:
119+
app[ak.fhir_client] = await init_fhir_client(app[ak.settings], "/fhir")
120+
121+
yield
122+
123+
124+
async def init_fhir_client(settings: Settings, prefix: str = "") -> AsyncFHIRClient:
125+
basic_auth = BasicAuth(
126+
login=settings.APP_INIT_CLIENT_ID,
127+
password=settings.APP_INIT_CLIENT_SECRET,
128+
)
129+
130+
return AsyncFHIRClient(
131+
f"{settings.APP_INIT_URL}{prefix}",
132+
authorization=basic_auth.encode(),
133+
dump_resource=lambda x: x.model_dump(),
134+
)
135+
```
136+
137+
After that, you can use `app[ak.fhir_client]` that has the type `AsyncFHIRClient` everywhere where the app is available.
138+
139+
140+
## Usage of request_schema
141+
```python
142+
from aidbox_python_sdk.types import SDKOperation, SDKOperationRequest
143+
56144
schema = {
57145
"required": ["params", "resource"],
58146
"properties": {
@@ -76,14 +164,17 @@ schema = {
76164

77165

78166
@sdk.operation(["POST"], ["Organization", {"name": "id"}, "$update"], request_schema=schema)
79-
async def update_organization_handler(operation, request):
167+
async def update_organization_op(_operation: SDKOperation, request: SDKOperationRequest):
80168
location = request["params"]["location"]
81169
return web.json_response({"location": location})
82170
```
171+
83172
### Valid request example
84173
```shell
85174
POST /Organization/org-1/$update?abc=xyz&location=us
86175

87176
organizationType: non-profit
88177
employeesCount: 10
89178
```
179+
180+

0 commit comments

Comments
 (0)