|
1 | 1 | import json |
2 | 2 |
|
| 3 | +import aiohttp |
3 | 4 | from aiohttp import web |
4 | 5 | from fhirpy.lib import AsyncFHIRClient |
5 | 6 |
|
|
24 | 25 | @routes.get("/Questionnaire/{id}/$assemble") |
25 | 26 | async def assemble_handler(request: web.BaseRequest): |
26 | 27 | client: AsyncFHIRClient = request.app["client"] |
27 | | - aidbox_client = request.aidbox_client |
| 28 | + fce_converter_url = getattr(request.app["settings"], "FCE_CONVERTER_URL", None) |
28 | 29 |
|
29 | 30 | questionnaire = ( |
30 | 31 | await client.resources("Questionnaire").search(_id=request.match_info["id"]).get() |
31 | 32 | ) |
32 | 33 |
|
| 34 | + if fce_converter_url: |
| 35 | + async with aiohttp.ClientSession() as session: |
| 36 | + |
| 37 | + async def to_fce(fhir_resource): |
| 38 | + async with session.post( |
| 39 | + f"{fce_converter_url}/to-fce", json=dict(fhir_resource) |
| 40 | + ) as resp: |
| 41 | + return await resp.json() |
| 42 | + |
| 43 | + fce_questionnaire = await to_fce(dict(questionnaire)) |
| 44 | + assembled_lazy = await assemble(client, fce_questionnaire, to_fce) |
| 45 | + assembled = json.loads(json.dumps(assembled_lazy, default=list)) |
| 46 | + |
| 47 | + async with session.post(f"{fce_converter_url}/to-fhir", json=assembled) as resp: |
| 48 | + return web.json_response(await resp.json()) |
| 49 | + |
33 | 50 | async def get_to_first_class_extension(fhir_resource): |
34 | | - return to_first_class_extension(fhir_resource, aidbox_client) |
| 51 | + return to_first_class_extension(fhir_resource) |
35 | 52 |
|
36 | 53 | assembled_questionnaire_lazy = await assemble( |
37 | 54 | client, to_first_class_extension(questionnaire), get_to_first_class_extension |
|
0 commit comments