-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Reverted atomic extract changes"
This reverts commit 2ce7145.
- Loading branch information
Showing
2 changed files
with
230 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,68 @@ | ||
from aiohttp import ClientSession, web | ||
from funcy.seqs import flatten | ||
|
||
|
||
async def external_service_extraction(client, service, template, context): | ||
async with ClientSession() as session: | ||
async with session.post( | ||
service, | ||
json={ | ||
"template": template, | ||
"context": context, | ||
}, | ||
) as result: | ||
if 200 <= result.status <= 299: | ||
bundle = await result.json() | ||
return await client.execute("/", data=bundle) | ||
else: | ||
raise web.HTTPBadRequest(body=await result.text()) | ||
async def get_external_service_bundle(session, service, template, context): | ||
async with session.post( | ||
service, | ||
json={ | ||
"template": template, | ||
"context": context, | ||
}, | ||
) as result: | ||
if 200 <= result.status <= 299: | ||
return await result.json() | ||
else: | ||
raise web.HTTPBadRequest(body=await result.text()) | ||
|
||
|
||
async def execute_mappers_bundles(client, mappers_bundles): | ||
not_transaction = any(bundle.get("type") != "transaction" for bundle in mappers_bundles) | ||
|
||
result_bundle = { | ||
"resourceType": "Bundle", | ||
"type": "batch" if not_transaction else "transaction", | ||
"entry": list(flatten(bundle["entry"] for bundle in mappers_bundles)), | ||
} | ||
|
||
return await client.execute("/", data=result_bundle) | ||
|
||
|
||
async def extract(client, mappings, context, extract_services): | ||
""" | ||
mappings could be a list of Aidbox Mapping resources | ||
or plain jute templates | ||
""" | ||
resp = [] | ||
|
||
for mapper in mappings: | ||
if "resourceType" in mapper and "body" in mapper: | ||
# It is custome mapper resource | ||
mapper_type = mapper.get("type", "JUTE") | ||
if mapper_type == "JUTE" and extract_services["JUTE"] == "aidbox": | ||
# Aidbox native extraction | ||
resp.append(await mapper.execute("$apply", data=context)) | ||
async with ClientSession() as session: | ||
resp = [] | ||
mappers_bundles = [] | ||
|
||
for mapper in mappings: | ||
if "resourceType" in mapper and "body" in mapper: | ||
# It is custome mapper resource | ||
mapper_type = mapper.get("type", "JUTE") | ||
if mapper_type == "JUTE" and extract_services["JUTE"] == "aidbox": | ||
mapper_bundle = await mapper.execute("$debug", data=context) | ||
if "entry" not in mapper_bundle: | ||
continue | ||
|
||
mappers_bundles.append(mapper_bundle) | ||
else: | ||
# Use 3rd party service FHIRPathMapping or JUTE | ||
mappers_bundles.append( | ||
await get_external_service_bundle( | ||
session, extract_services[mapper_type], mapper["body"], context | ||
) | ||
) | ||
else: | ||
# Use 3rd party service FHIRPathMapping or JUTE | ||
resp.append( | ||
await external_service_extraction( | ||
client, extract_services[mapper_type], mapper["body"], context | ||
# legacy extraction | ||
mappers_bundles.append( | ||
await get_external_service_bundle( | ||
session, extract_services["JUTE"], mapper, context | ||
) | ||
) | ||
else: | ||
# legacy extraction | ||
resp.append( | ||
await external_service_extraction(client, extract_services["JUTE"], mapper, context) | ||
) | ||
|
||
return resp | ||
if len(mappers_bundles) > 0: | ||
resp.append(await execute_mappers_bundles(client, mappers_bundles)) | ||
|
||
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters