feat(tea): EEG analysis before the silver load, and its verdict, quality checks and baselines through silver and FHIR
This commit is contained in:
+27
-3
@@ -1,7 +1,9 @@
|
||||
"""What every FHIR R4 answer of the WebApi shares: the media type, errors as `OperationOutcome`, search bundles with
|
||||
their paging links, the simulated-data tag and the project's canonical URLs."""
|
||||
their paging links, the simulated-data tag and the project's canonical URLs. Every answer is validated against FHIR's
|
||||
R4B models (`fhir.resources`, the models the EEG analysis validates its own report with) before it leaves."""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from urllib.parse import urlencode
|
||||
|
||||
@@ -17,10 +19,32 @@ SIMULATED = {"system": f"{CODE_SYSTEM}/etiqueta", "code": "simulado", "display":
|
||||
MAX_COUNT = 200
|
||||
DEFAULT_COUNT = 50
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def validated(resource: dict) -> dict:
|
||||
"""The resource as FHIR's R4B models read it and write it back (R4B keeps R4's shape for these resources).
|
||||
`ValueError` if it doesn't conform."""
|
||||
from fhir.resources.R4B import get_fhir_model_class
|
||||
|
||||
try:
|
||||
model = get_fhir_model_class(resource["resourceType"])
|
||||
except KeyError as exc:
|
||||
raise ValueError(f"no FHIR resource type {resource.get('resourceType')!r}") from exc
|
||||
return model.model_validate(resource).model_dump(mode="json", exclude_none=True)
|
||||
|
||||
|
||||
def fhir(resource: dict, status: int = 200) -> Response:
|
||||
"""A resource as the answer, in FHIR's JSON media type."""
|
||||
return Response(json.dumps(resource, ensure_ascii=False), status=status, mimetype=MEDIA_TYPE)
|
||||
"""A resource as the answer, in FHIR's JSON media type. One that doesn't conform to FHIR never leaves: the answer
|
||||
is then a 500 `OperationOutcome`, and the reason goes to the log."""
|
||||
try:
|
||||
body = validated(resource)
|
||||
except ValueError:
|
||||
if resource.get("resourceType") == "OperationOutcome":
|
||||
raise
|
||||
log.exception("a %s answer doesn't conform to FHIR R4B", resource.get("resourceType"))
|
||||
return outcome(500, "exception", "la respuesta no es un recurso FHIR válido")
|
||||
return Response(json.dumps(body, ensure_ascii=False), status=status, mimetype=MEDIA_TYPE)
|
||||
|
||||
|
||||
def outcome(status: int, code: str, diagnostics: str, severity: str = "error") -> Response:
|
||||
|
||||
Reference in New Issue
Block a user