Skip to content

Commit

Permalink
Get rid of aidbox-python-sdk. Change architecture. Return ref and tim…
Browse files Browse the repository at this point in the history
…estamp intead of resource
  • Loading branch information
ruscoder committed Oct 1, 2024
1 parent bf881ee commit 023b717
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 277 deletions.
111 changes: 19 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
# aidbox-python-sdk-tbs
# fhir-tbs-py

Topic-based subscription extension for Aidbox SDK for python
Topic-based subscription extension for python aiohttp web applications

## Install

Install `aidbox-python-sdk-tbs[r4b]` or `aidbox-python-sdk-tbs[r5]` using poetry/pipenv/pip.
Install `fhir-tbs-py[r4b]` or `fhir-tbs-py[r5]` using poetry/pipenv/pip.

## Usage

Create `app/app_keys.py` with the following context:

```python
from aidbox_python_sdk.app_keys import client, sdk, settings
from aiohttp import web
from fhirpy import AsyncFHIRClient

fhir_client: web.AppKey[AsyncFHIRClient] = web.AppKey("fhir_client", AsyncFHIRClient)

__all__ = ["fhir_client", "client", "sdk", "settings"]
```

Create `app/subscriptions.py` with the following content:
Create `subscriptions.py` with the following content:

```python
import logging
from collections.abc import AsyncGenerator

from aidbox_python_sdk.types import SDKOperation, SDKOperationRequest
from aidbox_python_sdk_tbs import SubscriptionDefinition
from aidbox_python_sdk_tbs.r4b impotr r4b_tbs_ctx_factory
from fhirpy import AsyncFHIRClient
import fhirpy_types_r4b as r4b
from fhir_tbs import SubscriptionDefinition
from fhir_tbs.r4b impotr r4b_tbs_ctx_factory
from aiohttp import web

from app import app_keys as ak
from app import config
from app.sdk import sdk
# Make sure that app has fhir_client_key
fhir_client_key = web.AppKey("fhir_client_key", AsyncFHIRClient)

async def new_appointment_sub(app: web.Application, appointment: r4b.Appointment) -> None:
fhir_client = app[ak.fhir_client]

async def new_appointment_sub(app: web.Application, appointment_ref: str, _timestamp: str) -> None:
fhir_client = app[fhir_client_key]
appointment = r4b.Appointment(**(await fhir_client.get(appointment_ref)))
logging.error("New appointment %s", appointment.model_dump())


Expand All @@ -52,78 +41,16 @@ subscriptions: list[SubscriptionDefinition] = [
]


def aidbox_tbs_ctx(app: web.Application) -> AsyncGenerator[None, None]:
def tbs_ctx(app: web.Application) -> AsyncGenerator[None, None]:
return r4b_tbs_ctx_factory(
app[ak.sdk],
app[ak.settings].APP_INIT_URL,
app[ak.fhir_client],
app,
"http://app:8080",
"webhook",
app[fhir_client_key],
subscriptions,
)
```

New we need to build `app` composing different contexts.
Pay attention, that app_ctx should be the last one, because `aidbox-python-sdk-tbs` adds endpoints to sdk.

Change `main.py` to look like the following example:

```python
from collections.abc import AsyncGenerator

from aidbox_python_sdk.main import init_client, register_app, setup_routes
from aidbox_python_sdk.settings import Settings
from aiohttp import BasicAuth, web
from fhirpy import AsyncFHIRClient

from app import app_keys as ak
from app.sdk import sdk
from app.subscriptions import aidbox_tbs_ctx


async def init_fhir_client(settings: Settings, prefix: str = "") -> AsyncFHIRClient:
basic_auth = BasicAuth(
login=settings.APP_INIT_CLIENT_ID,
password=settings.APP_INIT_CLIENT_SECRET,
)

return AsyncFHIRClient(
f"{settings.APP_INIT_URL}{prefix}",
authorization=basic_auth.encode(),
dump_resource=lambda x: x.model_dump(),
)


async def fhir_client_ctx(app: web.Application) -> AsyncGenerator[None, None]:
app[ak.fhir_client] = await init_fhir_client(app[ak.settings], "/fhir")
yield


async def aidbox_client_ctx(app: web.Application) -> AsyncGenerator[None, None]:
app[ak.client] = await init_client(app[ak.settings])
yield


async def app_ctx(app: web.Application) -> AsyncGenerator[None, None]:
await register_app(app[ak.sdk], app[ak.client])
yield


def create_app() -> web.Application:
app = web.Application()
app[ak.sdk] = sdk
app[ak.settings] = sdk.settings
app.cleanup_ctx.append(aidbox_client_ctx)
app.cleanup_ctx.append(fhir_client_ctx)
app.cleanup_ctx.append(aidbox_tbs_ctx)
# NOTE: Pay attention, app_ctx should be after aidbox_tbs_ctx !!!
app.cleanup_ctx.append(app_ctx)

setup_routes(app)

return app


async def create_gunicorn_app() -> web.Application:
return create_app()
```

Add `tbs_ctx` to `app.cleanup_ctx`.

120 changes: 0 additions & 120 deletions aidbox_python_sdk_tbs/implementation.py

This file was deleted.

45 changes: 0 additions & 45 deletions aidbox_python_sdk_tbs/r5.py

This file was deleted.

4 changes: 2 additions & 2 deletions aidbox_python_sdk_tbs/__init__.py → fhir_tbs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .implementation import tbs_ctx_factory
from .types import FilterBy, SubscriptionDefinition

__title__ = "aidbox-python-sdk-tbs"
__version__ = "0.0.1a5"
__title__ = "fhir-tbs-py"
__version__ = "0.0.1b0"
__author__ = "beda.software"
__license__ = "MIT"
__copyright__ = "Copyright 2024 beda.software"
Expand Down
Loading

0 comments on commit 023b717

Please sign in to comment.