Skip to main content

fhir_client

FHIR Client implementation.

Module

Functions

relative_path_if_same_server

def relative_path_if_same_server(url: str, base_url: str)> str | None:

Return url's path relative to base_url, if they're the same server.

"Same server" is determined by normalized components rather than a naive substring/prefix check: exact scheme match, case-insensitive host match, and the URL's path sitting under the base URL's path. The port is deliberately ignored, since some servers (e.g. ModMed) emit absolute URLs with an explicit default port (:443) against a base URL that omits it. Returns None for a non-absolute url (no scheme) or a genuinely different server.

Classes

FHIRClient

class FHIRClient(    url: str,    authorization: str | None = None,    extra_headers: dict[str, Any] | None = None,    requests_config: dict[str, Any] | None = None,    *,    dump_resource: Callable[[Any], dict[str, Any]] = <function FHIRClient.<lambda>>,    ehr_config_type: str | None = None,    token_provider: TokenProvider | None = None,):

Implementation of FHIR Client for error handling.

dump kwarg is a function that is called for all CRUD operations. It's needed for custom typing model like pydantic

Ancestors

Methods


ensure_authorized

def ensure_authorized(self, force: bool = False)> None:

Populate the bearer token from the token provider, if one is set.

A no-op when no token_provider was supplied, so externally-managed tokens (set_authorization) and genuinely unauthenticated FHIR servers both keep working unchanged.

Arguments

  • force: If True, ask the provider to discard its cached token and fetch a fresh one. Use after a 401, where the token may have been revoked server-side despite not having expired.

set_authorization

def set_authorization(self, token: str, expires: datetime | None = None)> None:

Set the bearer token (and optional expiry) for EHR requests.

Call this when using externally managed JWT so the client can log fingerprint and expiry on request failure. Also sets .authorization as required by the parent class.