|
1 | 1 | import uuid |
2 | 2 |
|
3 | | -from dynamodb import EventTable |
4 | | -import json |
5 | | -import re |
6 | | - |
7 | | - |
8 | | -def create_response(event_id, message, code): |
9 | | - return { |
10 | | - "resourceType": "OperationOutcome", |
11 | | - "id": event_id, |
12 | | - "meta": { |
13 | | - "profile": [ |
14 | | - "https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome" |
15 | | - ] |
16 | | - }, |
17 | | - "issue": [ |
18 | | - { |
19 | | - "severity": "error", |
20 | | - "code": code, |
21 | | - "details": { |
22 | | - "coding": [ |
23 | | - { |
24 | | - "system": "https://fhir.nhs.uk/Codesystem/http-error-codes", |
25 | | - "code": code.upper() |
26 | | - } |
27 | | - ] |
28 | | - }, |
29 | | - "diagnostics": message |
30 | | - } |
31 | | - ] |
32 | | - } |
| 3 | +from fhir_controller import FhirController |
| 4 | +from fhir_repository import ImmunisationRepository |
| 5 | +from fhir_service import FhirService |
| 6 | +from models.errors import Severity, Code, create_operation_outcome |
33 | 7 |
|
34 | 8 |
|
35 | 9 | def get_imms_handler(event, context): |
36 | | - dynamo_service = EventTable() |
37 | | - return get_imms(event, dynamo_service) |
38 | | - |
39 | | - |
40 | | -# create function which receives event and instance of dynamodb |
41 | | -def get_imms(event, dynamo_service): |
42 | | - event_id = event["pathParameters"]["id"] |
43 | | - |
44 | | - def is_valid_id(_event_id): |
45 | | - pattern = r'^[A-Za-z0-9\-.]{1,64}$' |
46 | | - return re.match(pattern, _event_id) is not None |
47 | | - |
48 | | - if not is_valid_id(event_id) or not event_id: |
49 | | - return { |
50 | | - "statusCode": 400, |
51 | | - "headers": { |
52 | | - "Content-Type": "application/fhir+json", |
53 | | - }, |
54 | | - "body": json.dumps( |
55 | | - create_response(str(uuid.uuid4()), |
56 | | - "he provided event ID is either missing or not in the expected format.", |
57 | | - "invalid")) |
58 | | - } |
| 10 | + imms_repo = ImmunisationRepository() |
| 11 | + service = FhirService(imms_repo=imms_repo) |
| 12 | + controller = FhirController(fhir_service=service) |
59 | 13 |
|
60 | | - message = dynamo_service.get_event_by_id(event_id) |
61 | | - if message is None: |
| 14 | + return get_immunisation_by_id(event, controller) |
62 | 15 |
|
63 | | - return { |
64 | | - "statusCode": 404, |
65 | | - "headers": { |
66 | | - "Content-Type": "application/fhir+json", |
67 | | - }, |
68 | | - "body": json.dumps(create_response(str(uuid.uuid4()), "The requested resource was not found.", "not-found")) |
69 | | - } |
70 | 16 |
|
71 | | - return { |
72 | | - 'statusCode': 200, |
73 | | - "headers": { |
74 | | - "Content-Type": "application/fhir+json", |
75 | | - }, |
76 | | - 'body': json.dumps(message) |
77 | | - } |
| 17 | +def get_immunisation_by_id(event, controller: FhirController): |
| 18 | + try: |
| 19 | + return controller.get_immunisation_by_id(event) |
| 20 | + except Exception as e: |
| 21 | + exp_error = create_operation_outcome(resource_id=str(uuid.uuid4()), severity=Severity.error, |
| 22 | + code=Code.not_found, |
| 23 | + diagnostics=str(e)) |
| 24 | + return FhirController.create_response(500, exp_error.json()) |
0 commit comments