Skip to main content

An ASGI FHIR API framework built on top of FastAPI and FHIR Resources

Project description

fhirstarter

tests

An ASGI FHIR API framework built on top of FastAPI and FHIR Resources.

Supports FHIR sequences:

Installation

pip install fhirstarter

Features

  • Automatic, standardized API route creation
  • Automatic validation of inputs and outputs through the use of FHIR Resources Pydantic models
  • Automatically-generated capability statement that can be customized, and a capability statement API route
  • An exception-handling framework that produces FHIR-friendly responses (i.e. OperationOutcomes)
  • Automatically-generated, integrated documentation generated from the FHIR specification
  • Custom search parameters for search endpoints

Disclaimer

FHIRStarter was built based on the business needs of Canvas Medical. At any point in time, it may not be broadly applicable to the industry at large. Canvas Medical open-sourced the project so that it can be used by healthcare software developers whose needs it might also meet. Ongoing support and development will be based on the business needs of Canvas Medical.

Background

FHIRStarter uses a provider-decorator pattern. Developers can write functions, or handlers, that implement FHIR interactions and plug them into the framework. FHIRStarter then automatically creates FHIR-compatible API routes from these developer-provided functions. Handlers that are supplied must use the resource classes defined by the FHIR Resources Python package, which is a collection of Pydantic models for FHIR resources.

In order to stand up a FHIR server, all that is required is to create a FHIRStarter and a FHIRProvider instance, register a FHIR interaction with the provider, add the provider to the FHIRStarter instance, and pass the FHIRStarter instance to an ASGI server. An example is provided below.

Usage

Currently-supported functionality

FHIRStarter will automatically generate the capability statement endpoint at /metadata. In addition, it supports the following instance- and type-level interactions:

Interaction handlers can be written as coroutines with async/await syntax, or as plain functions. FastAPI supports both, as does FHIRStarter.

Using uvloop can improve performance of the underlying event loop on supported platforms. FHIRStarter does not mandate the use of uvloop, but it may be enabled by importing uvloop and adding a snippet like this to your application startup script:

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

Configuration for specific FHIR sequences

FHIRStarter will work out of the box as an R5 server. If a different sequence is desired, it must be specified with an environment variable:

FHIR_SEQUENCE=R4B

Model imports are affected by which version of fhir.resources is installed. For STU3 and R4B, model imports will look like this:

from fhir.resources.STU3.patient import Patient
from fhir.resources.R4B.patient import Patient

For R5, model imports will look like this:

from fhir.resources.patient import Patient

Example

A detailed example is available here: example.py.

import uvicorn
from fhir.resources.patient import Patient

from fhirstarter import FHIRProvider, FHIRStarter, InteractionContext
from fhirstarter.exceptions import FHIRResourceNotFoundError

# Create the app
app = FHIRStarter()

# Create a provider
provider = FHIRProvider()


# Register the patient read FHIR interaction with the provider
@provider.read(Patient)
async def patient_read(context: InteractionContext, id_: str) -> Patient:
    # Get the patient from the database
    patient = ...

    if not patient:
        raise FHIRResourceNotFoundError

    return Patient(
        **{
            # Map patient from database to FHIR Patient structure
        }
    )


# Add the provider to the app
app.add_providers(provider)


if __name__ == "__main__":
    # Start the server
    uvicorn.run(app)

Custom search parameters

Custom search parameters can be defined in a configuration file that can be passed to the app on creation.

[search-parameters.Patient.nickname]
type = "string"
description = "Nickname"
uri = "https://hostname/nickname"
include-in-capability-statement = true

Adding a custom search parameter via configuration allows this name to be used as an argument when defining a search-type interaction handler and also adds this search parameter to the API documentation for the search endpoint.

Capability statement

It is possible to customize the capability statement by setting a capability statement modifier:

def amend_capability_statement(
    capability_statement: MutableMapping[str, Any], request: Request, response: Response
) -> MutableMapping[str, Any]:
    capability_statement["publisher"] = "Canvas Medical"
    return capability_statement

app.set_capability_statement_modifier(amend_capability_statement)

FastAPI dependency injection

FastAPI's dependency injection system is exposed at various levels:

  • application: the __init__ method on the FHIRStarter class
  • provider: the __init__ method on the FHIRProvider class
  • handler: the read, update, create, or search_type decorator used to add a handler to a provider

Dependencies specified at the application level will be injected into all routes in the application.

Dependencies specified at the provider level will be injected into all routes that are added to the application from that specific provider.

Dependencies specified at the handler level only apply to that specific FHIR interaction.

Download files

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

Source Distribution

fhirstarter-3.0.0.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fhirstarter-3.0.0-py3-none-any.whl (1.6 MB view details)

Uploaded Python 3

File details

Details for the file fhirstarter-3.0.0.tar.gz.

File metadata

  • Download URL: fhirstarter-3.0.0.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.3 Linux/6.11.0-1012-azure

File hashes

Hashes for fhirstarter-3.0.0.tar.gz
Algorithm Hash digest
SHA256 60167668f8547a9f570a0ecf91fac530d23a082a87720ccd68eb8d5d624a91e4
MD5 f0c2d33ed0fdcf8318fab82e65ae7fd2
BLAKE2b-256 392ffe6d3cad3769afa6a1c440bc88561bf01301983f462ce77801665b9138b6

See more details on using hashes here.

File details

Details for the file fhirstarter-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: fhirstarter-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.3 Linux/6.11.0-1012-azure

File hashes

Hashes for fhirstarter-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a98c9ce99a48699034723b1824e5a83c95a9b590daa19971230b27d123a24e1
MD5 8d7f13b0b89fcbd12d83b5db57d072e7
BLAKE2b-256 d20dd92c73dda54b611726e088d26510bed2c8e9392615b3b959bab1a3687f11

See more details on using hashes here.

Supported by

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