Skip to main content

service

One FHIR R4 EHR connection, behind the backend-neutral EHRDataService.

Classes​

FHIRR4EHRService​

class FHIRR4EHRService(    ehr_config: EHRConfig,    *,    ehr_secrets: RefreshableJWT | None = None,    knowledge_store: KnowledgeStore | None = None,):

One FHIR R4 EHR connection, through which its patients' records are read.

Built once per connection. It builds one token source, one auth failure budget, one FHIRClient and one SearchExecutor, all for the config's provider. Every record and lister it builds gets that executor, that client and that provider, so they share what is known about the server, the response cache's scope and the budget, and a record's own profile agrees with the executor its searches run on.

The client counts each request's authentication outcome against the budget. The budget is acted on here: before each lookup and each lister, and when a lookup is rejected as unauthenticated. A run whose credentials keep being rejected therefore stops at a lookup with EHRConnectionError.

Arguments

  • ehr_config: The EHR's config; its provider must be a FHIR R4 one.
  • ehr_secrets: JWT secrets for the EHR, from the pod's secrets. Not needed for a SMART Backend config, which carries its own auth.
  • knowledge_store: Where findings about the server are kept; in memory for this connection when not given.

Raises

  • ValueError: If the config's provider is not a FHIR R4 one (is_fhir_r4_provider), if it has no base_url, or if it has no usable credentials and allow_no_ehr_secrets is not set.

Variables​

  • supports_mrn_lookup : bool - Whether find_by_mrn can find patients: a FHIR R4 server always can.

Methods​


availability​

def availability(self) ‑> EHRPreflightResult:

Whether this EHR can be reached, before any patient is asked for.

Obtains a token from the connection's token provider, then asks the server for its CapabilityStatement (capability_statement_attempt), as often as the preflight policy allows (run_preflight). A rejected token is refetched for the next attempt.

find_by_mrn​

def find_by_mrn(    self, mrn: str | Sequence[str], *, system_qualified: bool = False,) ‑> FHIRR4PatientQuerier:

The record of the one patient holding this medical record number.

Several values are tried in turn, as EHRDataService.find_by_mrn says, one search each. Each value is stripped of surrounding whitespace (mrn_values), then escaped (escape_fhir_search_value), so it is searched as one literal value, or, with system_qualified, read as system|value when it holds exactly one | (fhir_identifier_token).

Arguments

  • mrn: The identifier, or several to try in turn.
  • system_qualified: Whether a value with one | is system|value.

Raises

  • ValueError: If no identifier is given, or one is blank (empty or whitespace only).
  • NoMatchingFHIRR4PatientError: If no patient holds any of them.
  • NonSpecificFHIRR4PatientError: If more than one patient holds the value being tried.
  • NoFHIRR4PatientIDError: If the patient holding the value being tried has no ID.
  • FHIRR4AuthenticationError: If the EHR rejected the search for the value being tried.
  • EHRConnectionError: If authentication has failed too often in a row for the run to go on.
  • FHIRR4APIError: If the search for the value being tried failed for any other API failure, such as an HTTP error (a 400 for an identifier the server will not accept, say) or an error OperationOutcome, or its answer could not be read (not a Bundle, not JSON, or not a valid Patient). An EHRRequestError; chained to the error it was raised from, where there was one.
  • requests.RequestException: If the server could not be reached or did not answer in time for the value being tried: the connection or timeout error left after the client's retries.

find_patient​

def find_patient(    self,    *,    given: str | None,    family: str | None,    dob: str | date,    narrows_last_search: bool = False,) ‑> FHIRR4PatientQuerier:

The record of the one patient with these names and date of birth.

Arguments

  • given: The given name searched on.
  • family: The family name searched on.
  • dob: The date of birth, as a date or a YYYY-MM-DD string.
  • narrows_last_search: Ignored: a FHIR R4 server is searched by name, so a narrower search is a search of its own.

Raises

  • NoMatchingFHIRR4PatientError: If no patient matches.
  • NonSpecificFHIRR4PatientError: If more than one patient matches.
  • NoFHIRR4PatientIDError: If the patient found has no ID.
  • FHIRR4AuthenticationError: If the EHR rejected the search.
  • EHRConnectionError: If authentication has failed too often in a row for the run to go on.
  • FHIRR4APIError: If the search failed for any other API failure, such as an HTTP error or an error OperationOutcome, or its answer could not be read (not a Bundle, not JSON, or not a valid Patient). An EHRRequestError; chained to the error it was raised from, where there was one.
  • requests.RequestException: If the server could not be reached or did not answer in time: the connection or timeout error left after the client's retries.

lister​

def lister(self) ‑> FHIRR4PatientLister:

A lister paging through this EHR's patients' IDs.

The budget is checked here, not per page; the client still counts each page's authentication outcome against it.

Raises

  • EHRConnectionError: If authentication has failed too often in a row for the run to go on.

patient_by_id​

def patient_by_id(    self, patient_id: str,) ‑> FHIRR4PatientQuerier:

The record of the patient with this FHIR Patient.id.

Arguments

  • patient_id: The patient's resource ID.

Raises

  • ValueError: If the ID is blank (empty or whitespace only).
  • NoMatchingFHIRR4PatientError: If no patient has that ID.
  • FHIRR4AuthenticationError: If the EHR rejected the lookup.
  • EHRConnectionError: If authentication has failed too often in a row for the run to go on.
  • FHIRR4APIError: If more than one patient has that ID, or the lookup failed for any other API failure, such as an HTTP error or an error OperationOutcome, or its answer could not be read (not a Bundle, not JSON, or not a valid Patient). An EHRRequestError; chained to the error it was raised from, where there was one.
  • requests.RequestException: If the server could not be reached or did not answer in time: the connection or timeout error left after the client's retries.

record_for_id​

def record_for_id(    self, patient_id: str,) ‑> FHIRR4PatientQuerier:

A record for this FHIR Patient.id, taken on trust.

The Patient resource is read best effort (get_patient_response_by_id): found, it is the record's raw_patient. Missing, or not read because the read failed (an API error or an error OperationOutcome, an answer that is not a Bundle or not JSON, or a connection or timeout error), the record has none, and the failure is logged without the ID. Only a rejection, a second patient with the ID, or the connection's budget stop raises. Its demographics are None, and the Patient is kept as read, not validated.

Arguments

  • patient_id: The patient's resource ID.

Raises

  • ValueError: If the ID is blank (empty or whitespace only).
  • FHIRR4AuthenticationError: If the EHR rejected the read.
  • EHRConnectionError: If authentication has failed too often in a row for the run to go on.
  • FHIRR4APIError: If more than one patient has that ID. An EHRRequestError, chained to fhirpy's MultipleResourcesFound.

reset_auth_budget​

def reset_auth_budget(self) ‑> None:

Forget the authentication failures counted so far.

The next lookup is sent whatever went before, and the budget counts from zero again.

stats​

def stats(self) ‑> EHRServiceStats:

What this connection has counted so far, as it stands now.

A snapshot: the counts are copied, so a later request does not change stats already read.