Skip to content

Commit cc6f1db

Browse files
committed
Use web.AppKey instead of string keys (with backward compatibility)
1 parent 96d65d6 commit cc6f1db

File tree

5 files changed

+57
-40
lines changed

5 files changed

+57
-40
lines changed

aidbox_python_sdk/app_keys.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from aiohttp import web
2+
3+
from aidbox_python_sdk.aidboxpy import AsyncAidboxClient
4+
from aidbox_python_sdk.db import DBProxy
5+
from aidbox_python_sdk.sdk import SDK
6+
from aidbox_python_sdk.settings import Settings
7+
8+
db: web.AppKey[DBProxy] = web.AppKey("db", DBProxy)
9+
client: web.AppKey[AsyncAidboxClient] = web.AppKey("client", AsyncAidboxClient)
10+
sdk: web.AppKey[SDK] = web.AppKey("sdk", SDK)
11+
settings: web.AppKey[Settings] = web.AppKey("settings", Settings)

aidbox_python_sdk/handlers.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import asyncio
22
import logging
3+
from typing import Any
34

45
from aiohttp import web
56
from fhirpy.base.exceptions import OperationOutcome
67

8+
from . import app_keys as ak
9+
710
logger = logging.getLogger("aidbox_sdk")
811
routes = web.RouteTableDef()
912

1013

11-
async def subscription(request, data):
14+
async def subscription(request: web.Request, data: dict):
1215
logger.debug("Subscription handler: %s", data["handler"])
1316
if "handler" not in data or "event" not in data:
1417
logger.error("`handler` and/or `event` param is missing, data: %s", data)
1518
raise web.HTTPBadRequest()
16-
handler = request.app["sdk"].get_subscription_handler(data["handler"])
19+
handler = request.app[ak.sdk].get_subscription_handler(data["handler"])
1720
if not handler:
1821
logger.error("Subscription handler `%s` was not found", "handler")
1922
raise web.HTTPNotFound()
@@ -23,12 +26,12 @@ async def subscription(request, data):
2326
return web.json_response({})
2427

2528

26-
async def operation(request, data):
29+
async def operation(request: web.Request, data: dict[str, Any]):
2730
logger.debug("Operation handler: %s", data["operation"]["id"])
2831
if "operation" not in data or "id" not in data["operation"]:
2932
logger.error("`operation` or `operation[id]` param is missing, data: %s", data)
3033
raise web.HTTPBadRequest()
31-
handler = request.app["sdk"].get_operation_handler(data["operation"]["id"])
34+
handler = request.app[ak.sdk].get_operation_handler(data["operation"]["id"])
3235
if not handler:
3336
logger.error("Operation handler `%s` was not found", data["handler"])
3437
raise web.HTTPNotFound()

aidbox_python_sdk/main.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import logging
44
import sys
55
from pathlib import Path
6+
from typing import cast
67

78
from aiohttp import BasicAuth, client_exceptions, web
89
from fhirpy.base.exceptions import OperationOutcome
910

11+
from . import app_keys as ak
1012
from .aidboxpy import AsyncAidboxClient
1113
from .db import DBProxy
1214
from .handlers import routes
@@ -47,28 +49,31 @@ async def register_app(sdk: SDK, client: AsyncAidboxClient):
4749
async def init_client(settings: Settings):
4850
aidbox_client_cls = settings.AIDBOX_CLIENT_CLASS
4951
basic_auth = BasicAuth(
50-
login=settings.APP_INIT_CLIENT_ID,
51-
password=settings.APP_INIT_CLIENT_SECRET,
52+
login=cast(str, settings.APP_INIT_CLIENT_ID),
53+
password=cast(str, settings.APP_INIT_CLIENT_SECRET),
5254
)
5355

5456
return aidbox_client_cls(f"{settings.APP_INIT_URL}", authorization=basic_auth.encode())
5557

5658

57-
async def init(app):
58-
app["client"] = await init_client(app["settings"])
59-
app["db"] = DBProxy(app["settings"])
60-
await register_app(app["sdk"], app["client"])
61-
await app["db"].initialize()
59+
async def init(app: web.Application):
60+
app[ak.client] = await init_client(app[ak.settings])
61+
app["client"] = app[ak.client] # For backwards compatibility
62+
app[ak.db] = DBProxy(app[ak.settings])
63+
app["db"] = app[ak.db] # For backwards compatibility
64+
await register_app(app[ak.sdk], app[ak.client])
65+
await app[ak.db].initialize()
6266
yield
63-
await app["db"].deinitialize()
67+
await app[ak.db].deinitialize()
6468

6569

6670
def create_app(sdk: SDK):
6771
app = web.Application()
6872
app.cleanup_ctx.append(init)
69-
app.update(
70-
settings=sdk.settings,
71-
sdk=sdk,
72-
)
73+
app[ak.sdk] = sdk
74+
app["sdk"] = app[ak.sdk] # For backwards compatibility
75+
app[ak.settings] = sdk.settings
76+
app["settings"] = app[ak.settings] # For backwards compatibility
77+
7378
setup_routes(app)
7479
return app

aidbox_python_sdk/pytest_plugin.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import os
2+
from typing import cast
23

34
import pytest
45
import pytest_asyncio
5-
from aiohttp import BasicAuth, ClientSession
6+
from aiohttp import BasicAuth, ClientSession, web
67
from yarl import URL
78

89
from main import create_app as _create_app
910

11+
from . import app_keys as ak
12+
1013

1114
async def start_app(aiohttp_client):
1215
app = await aiohttp_client(_create_app(), server_kwargs={"host": "0.0.0.0", "port": 8081})
13-
sdk = app.server.app["sdk"]
16+
sdk = cast(web.Application, app.server.app)[ak.sdk]
1417
sdk._test_start_txid = -1
1518

1619
return app
@@ -40,20 +43,18 @@ async def _request(self, method, path, *args, **kwargs):
4043
@pytest_asyncio.fixture
4144
async def aidbox(client):
4245
"""HTTP client for making requests to Aidbox"""
43-
app = client.server.app
46+
app = cast(web.Application, client.server.app)
4447
basic_auth = BasicAuth(
45-
login=app["settings"].APP_INIT_CLIENT_ID,
46-
password=app["settings"].APP_INIT_CLIENT_SECRET,
48+
login=app[ak.settings].APP_INIT_CLIENT_ID,
49+
password=app[ak.settings].APP_INIT_CLIENT_SECRET,
4750
)
48-
session = AidboxSession(auth=basic_auth)
51+
session = AidboxSession(auth=basic_auth, base_url=app[ak.settings].APP_INIT_URL)
4952
yield session
5053
await session.close()
5154

5255

5356
@pytest_asyncio.fixture
54-
async def safe_db(aidbox, client):
55-
sdk = client.server.app["sdk"]
56-
57+
async def safe_db(aidbox, client, sdk):
5758
resp = await aidbox.post(
5859
"/$psql",
5960
json={"query": "SELECT last_value from transaction_id_seq;"},
@@ -76,14 +77,14 @@ async def safe_db(aidbox, client):
7677

7778
@pytest.fixture()
7879
def sdk(client):
79-
return client.server.app["sdk"]
80+
return cast(web.Application, client.server.app)[ak.sdk]
8081

8182

8283
@pytest.fixture()
8384
def aidbox_client(client):
84-
return client.server.app["client"]
85+
return cast(web.Application, client.server.app)[ak.client]
8586

8687

8788
@pytest.fixture()
8889
def aidbox_db(client):
89-
return client.server.app["db"]
90+
return cast(web.Application, client.server.app)[ak.db]

aidbox_python_sdk/types.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
from typing_extensions import TypedDict
2-
3-
from aidbox_python_sdk.aidboxpy import AsyncAidboxClient
4-
from aidbox_python_sdk.db import DBProxy
5-
from aidbox_python_sdk.sdk import SDK
6-
7-
8-
class SDKOperationRequestApp(TypedDict):
9-
client: AsyncAidboxClient
10-
db: DBProxy
11-
sdk: SDK
1+
from typing import Any
122

3+
from aiohttp import web
4+
from typing_extensions import TypedDict
135

146
SDKOperationRequest = TypedDict(
15-
"SDKOperationRequest", {"app": SDKOperationRequestApp, "route-params": dict, "resource": dict}
7+
"SDKOperationRequest",
8+
{"app": web.Application, "params": dict, "route-params": dict, "resource": Any},
169
)
10+
11+
12+
class SDKOperation(TypedDict):
13+
pass

0 commit comments

Comments
 (0)