felicity-lims/felicity/api/rest/api_v1/fhir/r4.py

75 lines
2.2 KiB
Python
Raw Normal View History

import json
2023-11-22 17:13:16 +08:00
from typing import Annotated
2024-02-16 23:48:19 +08:00
from fastapi import APIRouter, Depends, HTTPException, Request
2023-11-22 17:13:16 +08:00
from felicity.api.deps import get_current_user
2024-09-22 23:15:27 +08:00
from felicity.apps.iol.fhir.schema import (
BundleResource,
DiagnosticReportResource,
PatientResource,
ServiceRequestResource,
)
from felicity.apps.iol.fhir.utils import (
create_resource,
get_diagnostic_report_resource,
get_patient_resource,
)
from felicity.apps.user.schemas import User
2024-07-28 03:52:31 +08:00
from felicity.apps.user.services import UserService
2023-05-17 16:54:05 +08:00
2023-11-22 17:13:16 +08:00
fhir_v4 = APIRouter(tags=["fhir-v4"], prefix="/fhir")
2023-05-17 16:54:05 +08:00
2023-08-14 02:08:08 +08:00
@fhir_v4.post("/{resource_type}")
async def add_resource(
2024-07-28 03:52:31 +08:00
request: Request,
resource_type: str,
user_service: Annotated[UserService, Depends(UserService)],
current_user: Annotated[User, Depends(get_current_user)],
2023-09-11 13:02:05 +08:00
):
2023-05-17 16:54:05 +08:00
"""
Add a fhir resource
Supported Resources are Bundle, ServiceRequest and Patient
2023-05-17 16:54:05 +08:00
"""
user = await user_service.get_by_username(current_user.user_name)
2023-06-24 18:28:27 +08:00
data = json.loads(await request.json())
resources = {
"Bundle": BundleResource,
"DiagnosticReport": DiagnosticReportResource,
"ServiceRequest": ServiceRequestResource,
"Patient": PatientResource,
}
if resource_type not in resources:
2023-11-22 17:13:16 +08:00
raise HTTPException(417, f"{resource_type} Resource not supported")
2023-09-11 13:02:05 +08:00
mapped_data = resources[resource_type](**data)
2024-07-24 17:40:04 +08:00
return await create_resource(resource_type, mapped_data, request, user)
2023-05-17 16:54:05 +08:00
2023-08-14 02:08:08 +08:00
@fhir_v4.get("/{resource}/{resource_id}")
2023-09-11 13:02:05 +08:00
async def get_resource(
2024-07-28 03:52:31 +08:00
resource: str,
resource_id: str,
current_user: Annotated[User, Depends(get_current_user)],
2023-09-11 13:02:05 +08:00
):
2023-05-17 16:54:05 +08:00
"""
Supported Resources are DiagnosticReport and Patient
- **resource_id** A Fhir Resource ID
"""
if resource not in ["Patient", "DiagnosticReport"]:
2023-11-22 17:13:16 +08:00
raise HTTPException(417, f"{resource} Resource not supported")
2023-05-17 16:54:05 +08:00
item = None
if resource == "Patient":
item = await get_patient_resource(resource_id)
if resource == "DiagnosticReport":
item = await get_diagnostic_report_resource(resource_id)
if not item:
raise HTTPException(404, f"{resource} with id {resource_id} not found")
2023-05-17 16:54:05 +08:00
return item