12
12
4 . You can then run example with ` python example.py ` .
13
13
14
14
# Getting started
15
+
15
16
## Minimal application
16
- ``` Python
17
+
18
+ main.py
19
+ ``` python
17
20
from aidbox_python_sdk.main import create_app as _create_app
18
21
from aidbox_python_sdk.settings import Settings
19
22
from aidbox_python_sdk.sdk import SDK
20
23
24
+
21
25
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
23
32
24
- async def create_app ():
25
- return await _create_app(sdk)
26
33
34
+ async def create_gunicorn_app () -> web.Application:
35
+ return create_app()
27
36
```
28
37
29
38
## Register handler for operation
30
- ``` Python
39
+ ``` python
31
40
import logging
32
41
from aiohttp import web
42
+ from aidbox_python_sdk.types import SDKOperation, SDKOperationRequest
33
43
34
44
from yourappfolder import sdk
35
45
@@ -39,7 +49,7 @@ from yourappfolder import sdk
39
49
path = [" signup" , " register" , {" name" : " date" }, {" name" : " test" }],
40
50
timeout = 60000 # # Optional parameter to set a custom timeout for operation in milliseconds
41
51
)
42
- def signup_register_op (operation , request ):
52
+ def signup_register_op (_operation : SDKOperation , request : SDKOperationRequest ):
43
53
"""
44
54
POST /signup/register/21.02.19/testvalue
45
55
PATCH /signup/register/22.02.19/patchtestvalue
@@ -51,8 +61,86 @@ def signup_register_op(operation, request):
51
61
52
62
```
53
63
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
+
56
144
schema = {
57
145
" required" : [" params" , " resource" ],
58
146
" properties" : {
@@ -76,14 +164,17 @@ schema = {
76
164
77
165
78
166
@ 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 ):
80
168
location = request[" params" ][" location" ]
81
169
return web.json_response({" location" : location})
82
170
```
171
+
83
172
# ## Valid request example
84
173
```shell
85
174
POST / Organization/ org- 1 / $ update? abc=xyz& location=us
86
175
87
176
organizationType: non- profit
88
177
employeesCount: 10
89
178
```
179
+
180
+
0 commit comments