def read_tracking_id_headers(headers: HTTPHeaders, raise_error=True): if mdc.correlation_id.get(): return correlation_id = headers.get(HttpHeaders.X_CORRELATION_ID, None) if not correlation_id: correlation_id = message_utilities.get_uuid() logger.info( f"Request is missing {HttpHeaders.X_CORRELATION_ID} header. Assigning new value: {correlation_id}" ) else: if len(re.findall(UUID_PATTERN, correlation_id)) != 1: if raise_error: raise tornado.web.HTTPError( status_code=400, log_message= f"Invalid {HttpHeaders.X_CORRELATION_ID} header. Should be an UUIDv4 matching regex '{UUID_PATTERN}'" ) else: correlation_id = message_utilities.get_uuid() logger.info( f"Invalid {HttpHeaders.X_CORRELATION_ID} header. Assigning new value: {correlation_id}" ) mdc.correlation_id.set(correlation_id)
def serialize(self) -> Tuple[str, Dict[str, str], str]: """Produce a serialised representation of this SOAP message by populating a Mustache template with this object's properties. :return: A tuple string containing the message ID, HTTP headers to be sent with the message and the message itself. """ soap_message_dictionary = copy.deepcopy(self.message_dictionary) message_id = soap_message_dictionary.get(MESSAGE_ID) if not message_id: message_id = message_utilities.get_uuid() soap_message_dictionary[MESSAGE_ID] = message_id timestamp = message_utilities.get_timestamp() soap_message_dictionary[TIMESTAMP] = timestamp logger.info('Creating SOAP message with {MessageId} and {Timestamp}', fparams={ 'MessageId': message_id, 'Timestamp': timestamp }) message = self.message_builder.build_message(soap_message_dictionary) http_headers = { 'charset': 'UTF-8', 'SOAPAction': soap_message_dictionary[ACTION], HttpHeaders.CONTENT_TYPE: SOAP_CONTENT_TYPE_VALUE, 'type': SOAP_CONTENT_TYPE_VALUE } return message_id, http_headers, message
def serialize( self, _message_dictionary: Dict[str, Any] = None ) -> Tuple[str, Dict[str, str], str]: """Produce a serialised representation of this ebXML message by populating a Mustache template with this object's properties. :type _message_dictionary: An optional `message_dictionary` to use instead of the one supplied when this instance was constructed. For use by subclasses. :return: A tuple string containing the message ID, HTTP headers to be sent with the message and the message itself. """ ebxml_message_dictionary = copy.deepcopy(_message_dictionary or self.message_dictionary) message_id = ebxml_message_dictionary.get(MESSAGE_ID) if not message_id: message_id = message_utilities.get_uuid() ebxml_message_dictionary[MESSAGE_ID] = message_id timestamp = message_utilities.get_timestamp() ebxml_message_dictionary[TIMESTAMP] = timestamp logger.info('Creating ebXML message with {MessageId} and {Timestamp}', fparams={ 'MessageId': message_id, 'Timestamp': timestamp }) message = self.message_builder.build_message(ebxml_message_dictionary) http_headers = { 'charset': 'UTF-8', 'SOAPAction': f'{ebxml_message_dictionary[SERVICE]}/{ebxml_message_dictionary[ACTION]}' } return message_id, http_headers, message
def initialize(self, sds_client: SDSClient) -> None: """Initialise this request handler with the provided configuration values. :param sds_client: The sds client component to use to look up values in SDS. """ mdc.trace_id.set(message_utilities.get_uuid()) self.sds_client = sds_client
def _extract_correlation_id(headers: HTTPHeaders): correlation_id = headers.get(HttpHeaders.CORRELATION_ID, None) if not correlation_id: correlation_id = message_utilities.get_uuid() logger.info( "Didn't receive correlation id in incoming request from supplier, so have generated a new one." ) mdc.correlation_id.set(correlation_id)
def __construct_message(self, message: dict, properties: Dict[str, Any] = None) -> proton.Message: """ Build a message with a generated uuid, and specified message body. :param message: The message body to be wrapped. :param properties: Optional application properties to send with the message. :return: The Message in the correct format with generated uuid. """ message_id = message_utilities.get_uuid() logger.info('Constructing message with {id} and {applicationProperties}', fparams={'id': message_id, 'applicationProperties': properties}) return proton.Message(id=message_id, content_type='application/json', body=json.dumps(message), properties=properties, ttl=self.ttl_in_seconds)
def build_device_resource(ldap_attributes: Dict) -> Dict: device = {"resourceType": "Device", "id": message_utilities.get_uuid()} identifiers = [] unique_identifier = ldap_attributes.get('uniqueIdentifier') if len(unique_identifier) > 1: raise ValueError( "LDAP returned more than 1 'uniqueIdentifier' attribute") unique_identifier = (unique_identifier or [None])[0] if unique_identifier: identifiers.append( build_identifier(Url.NHS_SPINE_ASID, unique_identifier)) party_key = ldap_attributes.get('nhsMhsPartyKey') if party_key: identifiers.append( build_identifier(Url.NHS_MHS_PARTYKEY_URL, party_key)) if identifiers: device['identifier'] = identifiers extension = [] managing_organization = ldap_attributes.get('nhsIdCode') if managing_organization: extension.append( _build_value_reference_extension( Url.MANAGING_ORGANIZATION_EXTENSION_URL, Url.MANAGING_ORGANIZATION_URL, managing_organization)) service_id_extensions = list( map( lambda v: _build_value_reference_extension( Url.SDS_SERVICE_INTERACTION_ID_URL, SERVICE_ID_FHIR_IDENTIFIER, v), ldap_attributes.get('nhsAsSvcIA'))) if service_id_extensions: extension += service_id_extensions if extension: device['extension'] = extension client_id = (ldap_attributes.get('nhsAsClient') or [None])[0] if client_id: device["owner"] = { "identifier": { "system": Url.MANAGING_ORGANIZATION_URL, "value": client_id } } return device
def build_bundle_resource(resources: List[Dict], base_url: str, full_url: str): return { "resourceType": "Bundle", "id": message_utilities.get_uuid(), "type": "searchset", "total": len(resources), "link": [{ "relation": "self", "url": full_url }], "entry": list( map( lambda resource: _map_resource_to_bundle_entry( resource, base_url), resources)) }
def build_endpoint(address): result = { "resourceType": "Endpoint", "id": message_utilities.get_uuid(), "status": "active", "connectionType": build_connection_type(), "payloadType": _build_payload_type(), "address": address } managing_organization = _build_managing_organization( ldap_attributes.get("nhsIDCode")) if managing_organization: result["managingOrganization"] = managing_organization identifiers = _build_identifier_array(ldap_attributes) identifiers = list(filter(lambda item: item, identifiers)) if identifiers: result["identifier"] = identifiers extensions = [] reliability_configuration_extensions = _build_extension_array( ldap_attributes) reliability_configuration_extensions = list( filter(lambda item: item, reliability_configuration_extensions)) if reliability_configuration_extensions: extensions.append({ "url": Url.EXTENSION_URL, "extension": reliability_configuration_extensions }) interaction_id = ldap_attributes.get("nhsMhsSvcIA") if interaction_id: extensions.append( _build_value_reference_extension( Url.SDS_SERVICE_INTERACTION_ID_URL, SERVICE_ID_FHIR_IDENTIFIER, interaction_id)) if extensions: result["extension"] = extensions return result
def _extract_correlation(self): correlation_id = self.request.headers.get(HttpHeaders.CORRELATION_ID, None) if correlation_id is None: return message_utilities.get_uuid() return correlation_id
def initialize(self) -> None: mdc.trace_id.set(message_utilities.get_uuid())