22import datetime
33import logging
44import os
5- import urllib .parse
65import uuid
76from typing import Any , Optional , cast
87from uuid import uuid4
4342 validate_identifiers_match ,
4443 validate_resource_versions_match ,
4544)
46- from controller .constants import IMMUNIZATION_TARGET_LEGACY_KEY_NAME , ImmunizationSearchParameterName
47- from controller .parameter_parser import PATIENT_IDENTIFIER_SYSTEM
4845from filter import Filter
4946from models .errors import UnauthorizedVaxError
5047from repository .fhir_repository import ImmunizationRepository
48+ from service .search_url_helper import create_url_for_bundle_link , get_service_url
5149
5250logging .basicConfig (level = "INFO" )
5351logger = logging .getLogger ()
5957IMMUNIZATION_VALIDATOR = ImmunizationValidator ()
6058
6159
62- def get_service_url (
63- service_env : str = IMMUNIZATION_ENV ,
64- service_base_path : str = IMMUNIZATION_BASE_PATH ,
65- ) -> str :
66- if not service_base_path :
67- service_base_path = "immunisation-fhir-api/FHIR/R4"
68-
69- non_prod = ["internal-dev" , "int" , "sandbox" ]
70- if service_env in non_prod :
71- subdomain = f"{ service_env } ."
72- elif service_env == "prod" :
73- subdomain = ""
74- else :
75- subdomain = "internal-dev."
76-
77- return f"https://{ subdomain } api.service.nhs.uk/{ service_base_path } "
78-
79-
8060class FhirService :
8161 _DATA_MISSING_DATE_TIME_ERROR_MSG = (
8262 "Data quality issue - immunisation with ID %s was found containing no occurrenceDateTime"
@@ -98,7 +78,7 @@ def get_immunization_by_identifier(
9878 """
9979 Get an Immunization by its ID. Returns a FHIR Bundle containing the search results.
10080 """
101- base_url = f"{ get_service_url ()} /Immunization"
81+ base_url = f"{ get_service_url (IMMUNIZATION_ENV , IMMUNIZATION_BASE_PATH )} /Immunization"
10282 resource , resource_metadata = self .immunization_repo .get_immunization_by_identifier (identifier )
10383
10484 if not resource :
@@ -251,7 +231,7 @@ def search_immunizations(
251231 BundleEntry (
252232 resource = Immunization .parse_obj (imms ),
253233 search = BundleEntrySearch (mode = "match" ),
254- fullUrl = f"{ get_service_url ()} /Immunization/{ imms ['id' ]} " ,
234+ fullUrl = f"{ get_service_url (IMMUNIZATION_ENV , IMMUNIZATION_BASE_PATH )} /Immunization/{ imms ['id' ]} " ,
255235 )
256236 for imms in processed_resources
257237 ]
@@ -288,7 +268,15 @@ def search_immunizations(
288268 link = [
289269 BundleLink (
290270 relation = "self" ,
291- url = self .create_url_for_bundle_link (permitted_vacc_types , nhs_number , date_from , date_to , include ),
271+ url = create_url_for_bundle_link (
272+ permitted_vacc_types ,
273+ nhs_number ,
274+ date_from ,
275+ date_to ,
276+ include ,
277+ IMMUNIZATION_ENV ,
278+ IMMUNIZATION_BASE_PATH ,
279+ ),
292280 )
293281 ],
294282 total = len (processed_resources ),
@@ -408,29 +396,3 @@ def process_patient_for_bundle(patient: dict):
408396 new_patient ["id" ] = new_patient ["identifier" ][0 ].get ("value" )
409397
410398 return new_patient
411-
412- @staticmethod
413- def create_url_for_bundle_link (
414- immunization_targets : set [str ],
415- patient_nhs_number : str ,
416- date_from : Optional [datetime .date ],
417- date_to : Optional [datetime .date ],
418- include : Optional [str ],
419- ) -> str :
420- """Creates url for the searchset Bundle Link."""
421- params = {
422- # Temporarily maintaining this for backwards compatibility with imms history, but we should remove it
423- IMMUNIZATION_TARGET_LEGACY_KEY_NAME : "," .join (immunization_targets ),
424- ImmunizationSearchParameterName .IMMUNIZATION_TARGET : "," .join (immunization_targets ),
425- ImmunizationSearchParameterName .PATIENT_IDENTIFIER : f"{ PATIENT_IDENTIFIER_SYSTEM } |{ patient_nhs_number } " ,
426- }
427-
428- if date_from :
429- params [ImmunizationSearchParameterName .DATE_FROM ] = date_from .isoformat ()
430- if date_to :
431- params [ImmunizationSearchParameterName .DATE_TO ] = date_to .isoformat ()
432- if include :
433- params [ImmunizationSearchParameterName .INCLUDE ] = include
434-
435- query = urllib .parse .urlencode (params )
436- return f"{ get_service_url ()} /Immunization?{ query } "
0 commit comments