Repository
Main class and utilities for managing FHIR structure definitions.
AbstractRepository
Path: fhircraft.fhir.resources.repository.AbstractRepository
Abstract base class for generic repositories.
Methods:
| Name | Description |
|---|---|
get |
Retrieve a resource by canonical URL and optional version. |
add |
Add a resource to the repository. |
has |
Check if a resource exists in the repository. |
get_versions |
Get all available versions for a canonical URL. |
get_latest_version |
Get the latest version for a canonical URL. |
set_internet_enabled |
Enable or disable internet access for this repository. |
parse_canonical_url |
Parse a canonical URL to extract base URL and version. |
format_canonical_url |
Format a canonical URL with optional version. |
get
abstractmethod
add
abstractmethod
add(resource: T) -> None
has
abstractmethod
get_versions
abstractmethod
get_latest_version
abstractmethod
set_internet_enabled
abstractmethod
set_internet_enabled(enabled: bool) -> None
parse_canonical_url
staticmethod
Parse a canonical URL to extract base URL and version.
Source code in fhircraft/fhir/resources/repository.py
format_canonical_url
staticmethod
Format a canonical URL with optional version.
CompositeStructureDefinitionRepository
Path: fhircraft.fhir.resources.repository.CompositeStructureDefinitionRepository
CompositeStructureDefinitionRepository(internet_enabled: bool = True, enable_packages: bool = True, registry_base_url: Optional[str] = None, timeout: float = 30.0)
Bases: AbstractRepository[StructureDefinition]
CompositeStructureDefinitionRepository provides a unified interface for managing, retrieving, and caching FHIR StructureDefinition resources from multiple sources, including local storage, FHIR packages, and online repositories.
This repository supports
- Local caching of StructureDefinitions, with version tracking and duplicate prevention.
- Optional integration with FHIR package repositories for bulk loading and management of definitions.
- Online retrieval of StructureDefinitions when enabled, with automatic local caching of downloaded resources.
- Utilities for loading definitions from files, directories, or pre-loaded dictionaries.
- Management of loaded packages, including loading, checking, and removal.
- Version management, including retrieval of all available versions, the latest version, and removal of specific versions.
Methods:
| Name | Description |
|---|---|
get |
Retrieve a StructureDefinition resource by its canonical URL and optional version. |
add |
Adds a StructureDefinition to the local repository. |
has |
Check if a resource with the given canonical URL and optional version exists in the repository. |
get_versions |
Retrieve all available versions for a given canonical URL. |
get_latest_version |
Retrieve the latest version string for a given FHIR resource canonical URL. |
load_from_directory |
Loads all JSON structure definitions from the specified directory and adds them to the repository. |
load_from_files |
Loads FHIR StructureDefinition resources from one or more JSON files and adds them to the repository. |
load_from_definitions |
Loads FHIR structure definitions from one or more pre-loaded dictionaries. |
set_internet_enabled |
Enables or disables internet access for the repository and its underlying HTTP repository. |
get_loaded_urls |
Returns a list of URLs for all FHIR definitions currently loaded in the repository. |
get_all_loaded_urls_with_versions |
Returns a dictionary mapping each loaded base URL to a list of its available versions. |
clear_local_cache |
Clears the local cache of definitions and latest versions. |
remove_version |
Remove a specific version or all versions of a resource identified by its canonical URL. |
load_package |
Loads a FHIR package into the repository, making its structure definitions available. |
get_loaded_packages |
Returns a dictionary of loaded FHIR packages. |
has_package |
Check if a package with the specified name and optional version exists in the package repository. |
remove_package |
Remove a package from the package repository. |
set_registry_base_url |
Sets the base URL for the package registry. |
Source code in fhircraft/fhir/resources/repository.py
get
Retrieve a StructureDefinition resource by its canonical URL and optional version.
This method attempts to find the requested StructureDefinition in the following order: 1. Local repository: Checks for the resource in the local cache, optionally by version. 2. Package repository: If available, attempts to retrieve the resource from a package repository. 3. Internet: If enabled, tries to fetch the resource from an online repository.
If the resource is found in the package or internet repository, it is cached locally for future use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
canonical_url
|
str
|
The canonical URL of the StructureDefinition to retrieve. |
required |
version
|
Optional[str]
|
The specific version of the StructureDefinition to retrieve. If not provided, the latest version is used if available. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
StructureDefinition |
StructureDefinition
|
The requested StructureDefinition resource. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the StructureDefinition cannot be found in any repository. |
Source code in fhircraft/fhir/resources/repository.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | |
add
add(resource: StructureDefinition, fail_if_exists: bool = False) -> None
Adds a StructureDefinition to the local repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource
|
StructureDefinition
|
The StructureDefinition instance to add. Must have a 'url' field, and a version either in the URL or in the 'version' field. |
required |
fail_if_exists
|
bool
|
If True, raises a ValueError if a StructureDefinition with the same base URL and version already exists in the repository. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the StructureDefinition does not have a 'url' field. |
ValueError
|
If the StructureDefinition does not have a version (in the URL or 'version' field). |
ValueError
|
If a duplicate StructureDefinition is added and fail_if_exists is True. |
Source code in fhircraft/fhir/resources/repository.py
has
Check if a resource with the given canonical URL and optional version exists in the repository. This method searches for the resource in the following order: 1. Local storage: Checks if the resource is available locally, optionally matching the specified version. 2. Package repository: If configured, checks if the resource exists in the package repository. 3. HTTP repository: If internet access is enabled, checks if the resource can be found via the HTTP repository. Args: canonical_url (str): The canonical URL of the resource to check. version (Optional[str], optional): The specific version of the resource to check for. Defaults to None. Returns: bool: True if the resource exists in any of the repositories or can be downloaded; False otherwise.
Source code in fhircraft/fhir/resources/repository.py
get_versions
Retrieve all available versions for a given canonical URL. Args: canonical_url (str): The canonical URL of the resource, possibly including a version. Returns: List[str]: A sorted list of version strings available for the specified canonical URL. Versions are sorted using semantic versioning if possible; otherwise, they are sorted lexicographically.
Source code in fhircraft/fhir/resources/repository.py
get_latest_version
Retrieve the latest version string for a given FHIR resource canonical URL. Args: canonical_url (str): The canonical URL of the FHIR resource, possibly including a version. Returns: Optional[str]: The latest version string associated with the base canonical URL, or None if no version is found.
Source code in fhircraft/fhir/resources/repository.py
load_from_directory
Loads all JSON structure definitions from the specified directory and adds them to the repository. Args: directory_path (Union[str, Path]): The path to the directory containing JSON structure definition files. Raises: FileNotFoundError: If the specified directory does not exist. RuntimeError: If an error occurs while loading or adding a structure definition from a file.
Source code in fhircraft/fhir/resources/repository.py
load_from_files
Loads FHIR StructureDefinition resources from one or more JSON files and adds them to the repository. Args: *file_paths (Union[str, Path]): One or more file paths to JSON files containing FHIR StructureDefinition resources. Raises: RuntimeError: If loading or parsing any of the files fails, a RuntimeError is raised with details about the file and the error.
Source code in fhircraft/fhir/resources/repository.py
load_from_definitions
Loads FHIR structure definitions from one or more pre-loaded dictionaries.
Each dictionary in definitions should represent a FHIR StructureDefinition resource.
The method validates each dictionary and adds the resulting StructureDefinition
object to the repository.
Args:
*definitions (Dict[str, Any]): One or more dictionaries representing FHIR StructureDefinition resources.
Raises:
ValidationError: If any of the provided dictionaries do not conform to the StructureDefinition model.
Source code in fhircraft/fhir/resources/repository.py
set_internet_enabled
set_internet_enabled(enabled: bool) -> None
Enables or disables internet access for the repository and its underlying HTTP repository. Args: enabled (bool): If True, internet access is enabled; if False, it is disabled.
Source code in fhircraft/fhir/resources/repository.py
get_loaded_urls
get_all_loaded_urls_with_versions
Returns a dictionary mapping each loaded base URL to a list of its available versions.
Returns:
| Type | Description |
|---|---|
Dict[str, List[str]]
|
Dict[str, List[str]]: A dictionary where the keys are base URLs (str) and the values are lists of version strings (List[str]) associated with each URL. |
Source code in fhircraft/fhir/resources/repository.py
clear_local_cache
Clears the local cache of definitions and latest versions. This method removes all entries from the internal caches used to store local definitions and their latest versions, effectively resetting the local state.
Source code in fhircraft/fhir/resources/repository.py
remove_version
Remove a specific version or all versions of a resource identified by its canonical URL. Args: canonical_url (str): The canonical URL of the resource, optionally including a version. version (Optional[str], optional): The specific version to remove. If not provided, the version is parsed from the canonical URL if present. If neither is provided, all versions for the base URL are removed. Returns: None Behavior: - If a version is specified (either as an argument or in the canonical URL), removes only that version. - If the removed version was the latest, updates the latest version to the next available one. - If no versions remain for the base URL, cleans up internal data structures. - If no version is specified, removes all versions associated with the base URL.
Source code in fhircraft/fhir/resources/repository.py
load_package
Loads a FHIR package into the repository, making its structure definitions available. Args: package_name (str): The name of the FHIR package to load. version (Optional[str], optional): The specific version of the package to load. If None, the latest version is used. Raises: RuntimeError: If package support is not enabled for this repository. Side Effects: - Loads the specified package into the internal package repository. - Adds any new structure definitions from the package to the local cache, avoiding duplicates.
Source code in fhircraft/fhir/resources/repository.py
get_loaded_packages
Returns a dictionary of loaded FHIR packages.
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dict[str, str]: A dictionary where the keys are package names and the values are their corresponding versions. |
Source code in fhircraft/fhir/resources/repository.py
has_package
Check if a package with the specified name and optional version exists in the package repository. Args: package_name (str): The name of the package to check for. version (Optional[str], optional): The specific version of the package to check. Defaults to None. Returns: bool: True if the package (and version, if specified) exists in the repository, False otherwise.
Source code in fhircraft/fhir/resources/repository.py
remove_package
Remove a package from the package repository. Args: package_name (str): The name of the package to remove. version (Optional[str], optional): The specific version of the package to remove. If None, all versions may be removed. Defaults to None. Returns: None
Source code in fhircraft/fhir/resources/repository.py
set_registry_base_url
set_registry_base_url(base_url: str) -> None
Sets the base URL for the package registry. Args: base_url (str): The base URL to be used for the package registry. Raises: RuntimeError: If package support is not enabled for this repository.
Source code in fhircraft/fhir/resources/repository.py
HttpStructureDefinitionRepository
Path: fhircraft.fhir.resources.repository.HttpStructureDefinitionRepository
Bases: AbstractRepository[StructureDefinition]
Repository that downloads structure definitions from the internet.
Methods:
| Name | Description |
|---|---|
get |
Download structure definition from the internet. |
add |
HTTP repository doesn't support adding definitions. |
has |
Check if URL can potentially be resolved. |
get_versions |
HTTP repository can't list versions without downloading. |
get_latest_version |
HTTP repository can't determine latest version without downloading. |
set_internet_enabled |
Enable or disable internet access. |
Source code in fhircraft/fhir/resources/repository.py
get
Download structure definition from the internet.
Source code in fhircraft/fhir/resources/repository.py
add
HTTP repository doesn't support adding definitions.
has
Check if URL can potentially be resolved.
Source code in fhircraft/fhir/resources/repository.py
get_versions
HTTP repository can't list versions without downloading.
get_latest_version
HTTP repository can't determine latest version without downloading.
Source code in fhircraft/fhir/resources/repository.py
PackageStructureDefinitionRepository
Path: fhircraft.fhir.resources.repository.PackageStructureDefinitionRepository
PackageStructureDefinitionRepository(internet_enabled: bool = True, registry_base_url: Optional[str] = None, timeout: float = 30.0)
Bases: AbstractRepository[StructureDefinition]
Repository that can load FHIR packages from package registries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
internet_enabled
|
bool
|
Whether to enable internet access |
True
|
registry_base_url
|
Optional[str]
|
Base URL for package registry (defaults to packages.fhir.org) |
None
|
timeout
|
float
|
Request timeout in seconds |
30.0
|
Methods:
| Name | Description |
|---|---|
get |
Get structure definition from loaded packages. |
add |
Add a structure definition to the repository. |
has |
Check if structure definition exists in loaded packages. |
get_versions |
Get all available versions for a canonical URL. |
get_latest_version |
Get the latest version for a canonical URL. |
set_internet_enabled |
Enable or disable internet access. |
load_package |
Load a FHIR package from the registry and add all structure definitions. |
get_loaded_packages |
Get a dictionary of loaded packages and their versions. |
has_package |
Check if a package is loaded. |
remove_package |
Remove a loaded package and all its structure definitions. |
set_registry_base_url |
Change the package registry base URL. |
clear_local_cache |
Clear all locally cached structure definitions. |
Source code in fhircraft/fhir/resources/repository.py
get
Get structure definition from loaded packages.
Source code in fhircraft/fhir/resources/repository.py
add
Add a structure definition to the repository.
Source code in fhircraft/fhir/resources/repository.py
has
Check if structure definition exists in loaded packages.
Source code in fhircraft/fhir/resources/repository.py
get_versions
Get all available versions for a canonical URL.
Source code in fhircraft/fhir/resources/repository.py
get_latest_version
Get the latest version for a canonical URL.
set_internet_enabled
set_internet_enabled(enabled: bool) -> None
load_package
load_package(package_name: str, package_version: Optional[str] = None, install_dependencies: bool = True, fail_if_exists: bool = False) -> None
Load a FHIR package from the registry and add all structure definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
Name of the package (e.g., "hl7.fhir.us.core") |
required |
package_version
|
Optional[str]
|
Version of the package (defaults to latest) |
None
|
install_dependencies
|
bool
|
If True, checks and installs any dependencies of the package |
True
|
fail_if_exists
|
bool
|
If True, raise error if package already loaded |
False
|
Raises:
| Type | Description |
|---|---|
PackageNotFoundError
|
If package or version not found |
FHIRPackageRegistryError
|
If download fails |
RuntimeError
|
If package processing fails |
Source code in fhircraft/fhir/resources/repository.py
get_loaded_packages
has_package
Check if a package is loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
Name of the package |
required |
package_version
|
Optional[str]
|
Version of the package (if None, checks any version) |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if package is loaded |
Source code in fhircraft/fhir/resources/repository.py
remove_package
Remove a loaded package and all its structure definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
Name of the package |
required |
package_version
|
Optional[str]
|
Version of the package (if None, removes all versions) |
None
|
Source code in fhircraft/fhir/resources/repository.py
set_registry_base_url
set_registry_base_url(base_url: str) -> None
clear_local_cache
Clear all locally cached structure definitions.
StructureDefinitionNotFoundError
Path: fhircraft.fhir.resources.repository.StructureDefinitionNotFoundError
Bases: FileNotFoundError
Raised when a required structure definition cannot be resolved.
configure_repository
configure_repository(directory: Optional[Union[str, Path]] = None, files: Optional[List[Union[str, Path]]] = None, definitions: Optional[List[Dict[str, Any]]] = None, internet_enabled: bool = True) -> CompositeStructureDefinitionRepository
Configures and returns a CompositeStructureDefinitionRepository by loading structure definitions from a directory, a list of files, or a list of definition dictionaries. Args: directory (Optional[Union[str, Path]]): Path to a directory containing structure definition files to load. files (Optional[List[Union[str, Path]]]): List of file paths to structure definition files to load. definitions (Optional[List[Dict[str, Any]]]): List of structure definition dictionaries to load directly. internet_enabled (bool): Whether to enable internet access for the repository (default is True). Returns: CompositeStructureDefinitionRepository: The configured repository with the loaded structure definitions.