0
# Core Functions
1
2
Essential utilities for dynamic FHIR resource creation and manipulation. These functions enable flexible resource handling without requiring hardcoded resource types, making them ideal for dynamic applications and generic FHIR processing.
3
4
## Capabilities
5
6
### Dynamic Model Class Retrieval
7
8
Get FHIR model classes dynamically by name, supporting all FHIR resource types and data types across all supported versions.
9
10
```python { .api }
11
def get_fhir_model_class(model_name: str) -> Type[FHIRAbstractModel]:
12
"""
13
Get FHIR model class by resource or element name.
14
15
Parameters:
16
- model_name: str - Name of FHIR resource or element type (e.g., "Patient", "Observation", "HumanName")
17
18
Returns:
19
Type[FHIRAbstractModel] - The corresponding FHIR model class
20
21
Raises:
22
KeyError - If model_name is not a valid FHIR element type
23
"""
24
```
25
26
**Usage Example:**
27
28
```python
29
from fhir.resources import get_fhir_model_class
30
31
# Get Patient class dynamically
32
PatientClass = get_fhir_model_class("Patient")
33
patient = PatientClass(active=True)
34
35
# Get complex data type class
36
HumanNameClass = get_fhir_model_class("HumanName")
37
name = HumanNameClass(family="Doe", given=["John"])
38
39
# Handle invalid types
40
try:
41
InvalidClass = get_fhir_model_class("InvalidType")
42
except KeyError as e:
43
print(f"Invalid FHIR type: {e}")
44
```
45
46
### Universal Element Construction
47
48
Construct FHIR elements from various data formats including dictionaries, JSON strings, raw bytes, or file paths.
49
50
```python { .api }
51
def construct_fhir_element(
52
element_type: str,
53
data: Union[Dict[str, Any], str, bytes, Path]
54
) -> FHIRAbstractModel:
55
"""
56
Construct FHIR element from various data formats.
57
58
Parameters:
59
- element_type: str - FHIR resource or element type name
60
- data: Union[Dict, str, bytes, Path] - Source data in various formats
61
62
Returns:
63
FHIRAbstractModel - Instantiated and validated FHIR element
64
65
Raises:
66
LookupError - If element_type is not a valid FHIR element type
67
ValidationError - If data doesn't conform to FHIR specification
68
"""
69
```
70
71
**Usage Examples:**
72
73
```python
74
from fhir.resources import construct_fhir_element
75
from pathlib import Path
76
77
# From dictionary
78
patient_dict = {
79
"resourceType": "Patient",
80
"id": "123",
81
"active": True,
82
"name": [{"family": "Doe", "given": ["John"]}]
83
}
84
patient = construct_fhir_element("Patient", patient_dict)
85
86
# From JSON string
87
json_data = '{"resourceType": "Patient", "active": true}'
88
patient = construct_fhir_element("Patient", json_data)
89
90
# From bytes
91
json_bytes = b'{"resourceType": "Patient", "active": true}'
92
patient = construct_fhir_element("Patient", json_bytes)
93
94
# From file path
95
file_path = Path("patient.json")
96
patient = construct_fhir_element("Patient", file_path)
97
98
# Handle invalid element types
99
try:
100
invalid = construct_fhir_element("InvalidType", {"test": "data"})
101
except LookupError as e:
102
print(f"Invalid element type: {e}")
103
```
104
105
## Types
106
107
```python { .api }
108
from typing import Any, Dict, Union, Type
109
from pathlib import Path
110
from fhir.resources.core.fhirabstractmodel import FHIRAbstractModel
111
112
# Type aliases used in function signatures
113
DataInput = Union[Dict[str, Any], str, bytes, Path]
114
ModelClass = Type[FHIRAbstractModel]
115
```