Skip to content

smart-on-fhir-client 🔥

Package allowing to request a fhir server with the smart-on-fhir protocol.

ℹ Warning

It is not a webserver providing a callback url usually involved in the smart-on-fhir procedure

Tutorial

First, we will need to create a partner providing a fhir server we want to query. We can do this easily subclassing the Partner class and setting basic information about the partner fhir server (such as fhir url and authorize endpoint).

import os
from smart_on_fhir_client.partner import Partner
from typing import Set
from smart_on_fhir_client.strategy import Strategy

class Lifen(Partner):
    name = 'LIFEN'
    supported_strategies: Set[Strategy] = {Strategy.M2M}
    client_id: str = os.getenv("LIFEN_CLIENT_ID")
    client_secret: str = os.getenv("LIFEN_CLIENT_SECRET")
    token_url: str = ... # set the token url
    fhir_url: str = ... # set the fhir url

    # additional information
    audience: str = ... # audience
    database_reference: str = ... # optional 
    grant_type: str = "client_credentials" # set the credentials

LIFEN = Lifen()

If we want, we can customize the fhir patient class for example adapted to our use case:

from typing import Any
from smart_on_fhir_client.requester.fhir_resource import CustomFHIRResource

class LifenPatientResource(CustomFHIRResource):

    async def pipe_to_target_fhir_server(
        self, *, target_identifier_url: str = None, **kwargs: Any
    ) -> "CustomFHIRResource":
        # do your own custom logic here !!!
        pass

We can now perform some basic queries !

from smart_on_fhir_client.client import smart_client_factory
from smart_on_fhir_client.requester.fhir_requester import fhir_client_manager
from smart_on_fhir_client.strategy import Strategy

# set up your own fhir server url
fhir_client_manager.set_own_fhir_url("http://localhost:8080/fhir")


async def register():
    async with smart_client_factory:
        await fhir_client_manager.register_fhir_client_async(
            smart_client_factory.builder()
            .for_partner(LIFEN)
            .for_strategy(Strategy.M2M)
            # you can register special classes for specific fhir resources
            .register_cls_for('Patient', LifenPatientResource)
        )
        first_patient = await fhir_client_manager.LIFEN.patient.search().limit(10).first()
        await first_patient.pipe_to_target_fhir_server()

Features

Allow to send some fetched fhir resources to another fhir server via the pipe_to_target_fhir_server, making data transfer between two fhir servers easier.

Notes

Work based heavily on fhir-py and fhir-resources python packages