Skip to main content

CRUD Operations against FHIR servers, resource validation and synthetic FHIR resource generation.

Project description

CI codecov PyPI version

:fire: FHIR Kindling

CRUD library for fhir servers, with resource validation and parsing powered by the pydantic models created by fhir.resources. More details in the Documentation.

Features

  • Create, Read, Update, Delete using a server's REST API
  • Bundle creation, validation and data management on a FHIR server via the REST API
  • Supports Hapi, Blaze and IBM FHIR servers

Installation

Install the package using pip:

pip install fhir-kindling --user

Usage

Connecting to a FHIR server

from fhir_kindling import FhirServer

# Connect with basic auth 
basic_auth_server = FhirServer("https://fhir.server/fhir", username="admin", password="admin")
# Connect with static token
token_server = FhirServer("https://fhir.server/fhir", token="your_token")

# Connect using oauth2/oidc
oidc_server = FhirServer("https://fhir.server/fhir", client_id="client_id", client_secret="secret",
                         oidc_provider_url="url")

# Print the server's capability statement
print(basic_auth_server.capabilities)

Query resources from the server

Basic resource query

from fhir_kindling import FhirServer
from fhir.resources.patient import Patient

# Connect using oauth2/oidc
oidc_server = FhirServer("https://fhir.server/fhir", client_id="client_id", client_secret="secret",
                         oidc_provider_url="url")

# query all patients on the server
query = oidc_server.query(Patient, output_format="json").all()
print(query.response)

# Query resources based on name of resource
query = oidc_server.query("Patient", output_format="json").all()
print(query.response)

Query with filters

Filtering the targeted resource is done using the where method on the query object. The filter is created by defining the target field, the comparison operator and the value to compare.

from fhir_kindling import FhirServer

server = FhirServer(api_address="https://fhir.server/fhir")

query = server.query("Patient").where(field="birthDate", operator="gt", value="1980").all()

Including related resources in the query

Resources that reference or are referenced by resources targeted by the query can be included in the response using the include method on the query object.

# server initialization omitted
# get the patients along with the queried conditions
query_patient_condition = server.query("Condition").include(resource="Condition", reference_param="subject").all()

# get the conditions for a patient
query_patient_condition = server.query("Patient")
query_patient_condition = query_patient_condition.include(resource="Condition", reference_param="subject", reverse=True)
response = query_patient_condition.all()

Add resources to the server

Resources can be added to the server using the add method on the server object. Lists of resources can be added using 'add_all'.

from fhir_kindling import FhirServer
from fhir.resources.patient import Patient

# Connect to the server
server = FhirServer(api_address="https://fhir.server/fhir")

# add a single resource
patient = Patient(name=[{"family": "Smith", "given": ["John"]}])
response = server.add(patient)

# add multiple resources
patients = [Patient(name=[{"family": f"Smith_{i}", "given": ["John"]}]) for i in range(10)]
response = server.add_all(patients)

Deleting/Updating resources

Resources can be deleted from the server using the delete method on the server object, it takes as input either references to the resources or the resources itself.
Similarly the update method can be used to update the resources on the server, by passing a list of updated resources.

from fhir_kindling import FhirServer
from fhir.resources.patient import Patient

# Connect to the server
server = FhirServer(api_address="https://fhir.server/fhir")

# add some patients
patients = [Patient(name=[{"family": f"Smith_{i}", "given": ["John"]}]) for i in range(10)]
response = server.add_all(patients)

# change the name of the patients
for patient in response.resources:
    patient.name[0].given[0] = "Jane"

# update the patients on the server
updated_patients = server.update(resources=response.resources)

# delete based on reference
server.delete(references=response.references[:5])
# delete based on resources
server.delete(resources=response.resources[5:])

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Change Log

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[0.7.0] - 2022-01-31

Update a list of resources on the server. CSV/Pandas serialization of resources and query responses.

Added

  • server.update(resources) which updates a list of resources stored on the server
  • Recursive resource flattening for csv/tabular serialization
  • query_response.save(path, format="csv) to save the results of a query to csv

Changed

  • server.query(params) the query method now directly accepts query parameters

[0.6.0] - 2022-01-19

Query Response with included resources. Reworked Generators

Added

  • Query response now stores and parses included resources
  • Generator parameters for Resources and Fields
  • Field generators for generating resource fields based on probabilistic choices or a generator function
  • Resource generator field values based on static value or list
  • Patient based data set generator

Changed

  • Query interface where, include, has now can add query parameters based on method arguments or parameter objects.

[0.5.0] - 2021-11-12

Query Parameters, include/revinclude, reverse chaining.

Added

  • Query Parameters classes, for regular queries, including resources and reverse chaining
  • Support for _include and _revinclude via query.include()
  • Reverse chaining support via query.has()
  • Parsing parameters from given URL/ coverting parameters to query url

Changed

  • Query interface where, include, has now can add query parameters based on method arguments or parameter objects.

[0.4.0] - 2021-11-12

Server summary, deleting resources, removed initial CLI.

Added

  • Getting a list of all resources on the server based on capabilities
  • Plots for server and resource summary
  • server.delete() method to delete resources based on ids

[0.3.0] - 2021-10-30

Response classes, reference parsing and basic xml support

Added

XML output format and resolving xml pagination. Response objects containig resources and references

Changed

Outsourced resolving of response pagination into response classes

Fixed

[0.2.0] - 2021-09-18

FHIR server and query API

Added

Classes for fhir servers and queries. Oauth2/OIDC support.

Changed

Moved location of cli

Fixed

[0.1.0] - 2021-08-24

Initial cli

Added

Changed

Fixed

Hashing order guarantees the right index of the query.json file in the hash

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fhir_kindling-0.7.3.tar.gz (43.6 kB view details)

Uploaded Source

Built Distribution

fhir_kindling-0.7.3-py2.py3-none-any.whl (32.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fhir_kindling-0.7.3.tar.gz.

File metadata

  • Download URL: fhir_kindling-0.7.3.tar.gz
  • Upload date:
  • Size: 43.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for fhir_kindling-0.7.3.tar.gz
Algorithm Hash digest
SHA256 777b701979f6a7ddb6cc5f3cc9d9b01688318a800f2fe53f9a321181b63c53af
MD5 beb7100287a41dc115e463136de27a01
BLAKE2b-256 f3516a12808ad2174f06ef603d6ea4db7b6ee3a3e6ac65afa73462103c2bc866

See more details on using hashes here.

File details

Details for the file fhir_kindling-0.7.3-py2.py3-none-any.whl.

File metadata

  • Download URL: fhir_kindling-0.7.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 32.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for fhir_kindling-0.7.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 54b3b6252b496ade5517b30d7fcf8dcbdb3ab8eab626ce9cf40c077a9620c771
MD5 c19a03d8d80138ce9f430eed19ebf9e8
BLAKE2b-256 f93962fcd3531f1926433fded7ac26a7b25837d96b882dcb90ee249a553d8ac1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page