helix_fhir_client_sdk.fhir_client

Module Contents

Classes

FhirClient

Class used to call FHIR server (uses async and parallel execution to speed up)

Attributes

HandleBatchFunction

HandleErrorFunction

helix_fhir_client_sdk.fhir_client.HandleBatchFunction
helix_fhir_client_sdk.fhir_client.HandleErrorFunction
class helix_fhir_client_sdk.fhir_client.FhirClient

Class used to call FHIR server (uses async and parallel execution to speed up)

Class used to call FHIR server (uses async and parallel execution to speed up)

action(self, action)

Set the action

Parameters

action (str) – (Optional) do an action e.g., $everything

Return type

FhirClient

action_payload(self, action_payload)

Set action payload

Parameters

action_payload (Dict[str, Any]) – (Optional) if action such as $graph needs a http payload

Return type

FhirClient

resource(self, resource)

set resource to query

Parameters

resource (str) – what FHIR resource to retrieve

Return type

FhirClient

id_(self, id_)
Parameters

id_ (Union[List[str], str]) –

Return type

FhirClient

url(self, url)

set url

Parameters

url (str) – server to call for FHIR

Return type

FhirClient

validation_server_url(self, validation_server_url)

set url to validate

Parameters

validation_server_url (str) – server to call for FHIR validation

Return type

FhirClient

additional_parameters(self, additional_parameters)

set additional parameters

Parameters

additional_parameters (List[str]) – Any additional parameters to send with request

Return type

FhirClient

filter_by_resource(self, filter_by_resource)

filter

Parameters

filter_by_resource (str) – filter the resource by this. e.g., /Condition?Patient=1 (resource=Condition, filter_by_resource=Patient)

Return type

FhirClient

filter_parameter(self, filter_parameter)

filter

Parameters

filter_parameter (str) – Instead of requesting ?patient=1, do ?subject:Patient=1 (if filter_parameter is subject)

Return type

FhirClient

include_only_properties(self, include_only_properties)

include only these properties

Parameters

include_only_properties (List[str]) – includes only these properties

Return type

FhirClient

page_number(self, page_number)

page number to load

Parameters

page_number (int) – page number to load

Return type

FhirClient

page_size(self, page_size)

page size

Parameters

page_size (int) – (Optional) use paging and get this many items in each page

Return type

FhirClient

last_updated_after(self, last_updated_after)

get records updated after this datetime

Parameters

last_updated_after (datetime.datetime) – (Optional) Only get records newer than this

Return type

FhirClient

last_updated_before(self, last_updated_before)

get records updated before this datetime

Parameters

last_updated_before (datetime.datetime) – (Optional) Only get records older than this

Return type

FhirClient

sort_fields(self, sort_fields)

sort

Parameters

sort_fields (List[helix_fhir_client_sdk.filters.sort_field.SortField]) – sort by fields in the resource

Return type

FhirClient

auth_server_url(self, auth_server_url)

auth server url

Parameters

auth_server_url (str) – server url to call to get the authentication token

Return type

FhirClient

auth_scopes(self, auth_scopes)

auth scopes

Parameters

auth_scopes (List[str]) – list of scopes to request permission for e.g., system/AllergyIntolerance.read

Return type

FhirClient

login_token(self, login_token)

login token

Parameters

login_token (str) – login token to use

Return type

FhirClient

client_credentials(self, client_id, client_secret)

Sets client credentials to use when calling the FHIR server

Parameters
  • client_id (str) – client_id

  • client_secret (str) – client_secret

Returns

self

Return type

FhirClient

logger(self, logger)

Logger to use for logging calls to the FHIR server

Parameters

logger (helix_fhir_client_sdk.loggers.fhir_logger.FhirLogger) – logger

Return type

FhirClient

adapter(self, adapter)

Http Adapter to use for calling the FHIR server

Parameters

adapter (requests.adapters.BaseAdapter) – adapter

Return type

FhirClient

limit(self, limit)

Limit the results

Parameters

limit (int) – Limit results to this count

Return type

FhirClient

async property access_token_async(self)

Gets current access token

Returns

access token if any

Return type

Optional[str]

set_access_token(self, value)

Sets access token

Parameters

value (str) – access token

Return type

FhirClient

async delete_async(self)

Delete the resources

Return type

helix_fhir_client_sdk.responses.fhir_delete_response.FhirDeleteResponse

delete(self)

Delete the resources

Return type

helix_fhir_client_sdk.responses.fhir_delete_response.FhirDeleteResponse

separate_bundle_resources(self, separate_bundle_resources)

Set flag to separate bundle resources

Parameters

separate_bundle_resources (bool) –

Return type

FhirClient

expand_fhir_bundle(self, expand_fhir_bundle)

Set flag to expand the FHIR bundle into a list of resources. If false then we don’t un bundle the response

Parameters

expand_fhir_bundle (bool) – whether to just return the result as a FHIR bundle

Return type

FhirClient

async get_async(self)

Issues a GET call

Returns

response

Return type

helix_fhir_client_sdk.responses.fhir_get_response.FhirGetResponse

get(self)

Issues a GET call

Returns

response

Return type

helix_fhir_client_sdk.responses.fhir_get_response.FhirGetResponse

static create_http_session()

Creates an HTTP Session

Return type

aiohttp.ClientSession

async get_with_handler_async(self, session, page_number, ids, fn_handle_batch, fn_handle_error, id_above=None)

gets data and calls the handlers as data is received

Parameters
  • session (Optional[aiohttp.ClientSession]) –

  • page_number (Optional[int]) –

  • ids (Optional[List[str]]) – ids to retrieve

  • fn_handle_batch (Optional[HandleBatchFunction]) – function to call when data is received

  • fn_handle_error (Optional[HandleErrorFunction]) – function to call when there is an error

  • id_above (Optional[str]) –

Returns

list of resources

Return type

List[Dict[str, Any]]

async get_page_by_query_async(self, session, start_page, increment, output_queue, fn_handle_batch, fn_handle_error)

Gets the specified page for query

Parameters
  • session (Optional[aiohttp.ClientSession]) –

  • start_page (int) –

  • increment (int) –

  • output_queue (asyncio.Queue[helix_fhir_client_sdk.responses.paging_result.PagingResult]) – queue to use

  • fn_handle_batch (Optional[HandleBatchFunction]) – function to call when data is received

  • fn_handle_error (Optional[HandleErrorFunction]) – function to call when there is an error

Returns

list of paging results

Return type

List[helix_fhir_client_sdk.responses.paging_result.PagingResult]

async get_by_query_in_pages_async(self, concurrent_requests, output_queue, fn_handle_batch, fn_handle_error)

Retrieves the data in batches (using paging) to reduce load on the FHIR server and to reduce network traffic

Parameters
  • output_queue

  • fn_handle_error (Optional[HandleErrorFunction]) –

  • concurrent_requests (int) –

  • fn_handle_batch (Optional[HandleBatchFunction]) – function to call for each batch. Receives a list of resources where each resource is a dictionary. If this is specified then we don’t return the resources anymore. If this function returns False then we stop processing batches.

Return type

helix_fhir_client_sdk.responses.fhir_get_response.FhirGetResponse

:return response containing all the resources received

async authenticate_async(self, http, auth_server_url, auth_scopes, login_token)

Authenticates with an OAuth Provider

Parameters
  • http (aiohttp.ClientSession) – http session

  • auth_server_url (str) – url to auth server /token endpoint

  • auth_scopes (Optional[List[str]]) – list of scopes to request

  • login_token (str) – login token to use for authenticating

Returns

access token

Return type

Optional[str]

async merge_async(self, json_data_list)

Calls $merge function on FHIR server

Parameters

json_data_list (List[str]) – list of resources to send

Return type

helix_fhir_client_sdk.responses.fhir_merge_response.FhirMergeResponse

merge(self, json_data_list)

Calls $merge function on FHIR server

Parameters

json_data_list (List[str]) – list of resources to send

Return type

helix_fhir_client_sdk.responses.fhir_merge_response.FhirMergeResponse

async graph_async(self, *, graph_definition, contained, process_in_batches=None, fn_handle_batch=None, fn_handle_error=None, concurrent_requests=1)

Executes the $graph query on the FHIR server

Parameters
  • concurrent_requests (int) –

  • graph_definition (helix_fhir_client_sdk.graph.graph_definition.GraphDefinition) – definition of a graph to execute

  • contained (bool) – whether we should return the related resources as top level list or nest them inside their parent resources in a contained property

  • process_in_batches (Optional[bool]) – whether to process in batches of size page_size

  • fn_handle_batch (Optional[HandleBatchFunction]) – Optional function to execute on each page of data. Note that if this is passed we don’t return the resources in the response anymore. If this function returns false then we stop processing any further batches.

  • fn_handle_error (Optional[HandleErrorFunction]) – function that is called when there is an error

Return type

helix_fhir_client_sdk.responses.fhir_get_response.FhirGetResponse

graph(self, *, graph_definition, contained, process_in_batches=None, concurrent_requests=1)
Parameters
Return type

helix_fhir_client_sdk.responses.fhir_get_response.FhirGetResponse

include_total(self, include_total)

Whether to ask the server to include the total count in the result

Parameters

include_total (bool) – whether to include total count

Return type

FhirClient

filter(self, filter_)

Allows adding in a custom filters that derives from BaseFilter

Parameters
Return type

FhirClient

async update_async(self, json_data)
Update the resource. This will completely overwrite the resource. We recommend using merge()

instead since that does proper merging.

Parameters

json_data (str) – data to update the resource with

Return type

helix_fhir_client_sdk.responses.fhir_update_response.FhirUpdateResponse

update(self, json_data)
Update the resource. This will completely overwrite the resource. We recommend using merge()

instead since that does proper merging.

Parameters

json_data (str) – data to update the resource with

Return type

helix_fhir_client_sdk.responses.fhir_update_response.FhirUpdateResponse

async get_resources_by_id_in_parallel_batches_async(self, concurrent_requests, chunks, fn_handle_batch, fn_handle_error)

Given a list of ids, this function loads them in parallel batches

Parameters
  • concurrent_requests (int) –

  • chunks (Generator[List[str], None, None]) – a generator that returns a list of ids to load in one batch

  • fn_handle_batch (Optional[HandleBatchFunction]) – Optional function to execute on each page of data. Note that if this is passed we don’t return the resources in the response anymore. If this function returns false then we stop processing any further batches.

  • fn_handle_error (Optional[HandleErrorFunction]) – function that is called when there is an error

Returns

list of resources

Return type

List[Dict[str, Any]]

async get_resources_by_id_from_queue_async(self, session, queue, task_number, fn_handle_batch, fn_handle_error)

Gets resources given a queue

Parameters
  • session (aiohttp.ClientSession) –

  • queue (asyncio.Queue[List[str]]) – queue to use

  • task_number (int) –

  • fn_handle_batch (Optional[HandleBatchFunction]) – Optional function to execute on each page of data. Note that if this is passed we don’t return the resources in the response anymore. If this function returns false then we stop processing any further batches.

  • fn_handle_error (Optional[HandleErrorFunction]) – function that is called when there is an error

Returns

list of resources

Return type

List[Dict[str, Any]]

handle_error(self, error, response, page_number)

Default handler for errors. Can be replaced by passing in fnError to functions

Parameters
  • error (str) – error text

  • response (str) – response text

  • page_number (Optional[int]) –

Returns

whether to continue processing

Return type

bool

async get_resources_by_query_and_last_updated_async(self, last_updated_start_date, last_updated_end_date, concurrent_requests=10, page_size_for_retrieving_resources=100, page_size_for_retrieving_ids=10000, fn_handle_batch=None, fn_handle_error=None)
Gets results for a query by paging through one day at a time,

first downloading all the ids and then retrieving resources for each id in parallel

Parameters
  • fn_handle_batch (Optional[HandleBatchFunction]) – Optional function to execute on each page of data. Note that if this is passed we don’t return the resources in the response anymore. If this function returns false then we stop processing any further batches.

  • fn_handle_error (Optional[HandleErrorFunction]) – Optional function that is called when there is an error

  • last_updated_start_date (datetime.datetime) – find resources updated after this datetime

  • last_updated_end_date (datetime.datetime) – find resources updated before this datetime

  • concurrent_requests (int) – number of concurrent requests to make to the server

  • page_size_for_retrieving_resources (int) – number of resources to download in one batch

  • page_size_for_retrieving_ids: – number of ids to download in one batch

  • page_size_for_retrieving_ids (int) –

Return type

List[Dict[str, Any]]

get_resources_by_query_and_last_updated(self, last_updated_start_date, last_updated_end_date, concurrent_requests=10, page_size_for_retrieving_resources=100, page_size_for_retrieving_ids=10000, fn_handle_batch=None, fn_handle_error=None)
Gets results for a query by paging through one day at a time,

first downloading all the ids and then retrieving resources for each id in parallel

Parameters
  • fn_handle_batch (Optional[HandleBatchFunction]) – Optional function to execute on each page of data. Note that if this is passed we don’t return the resources in the response anymore. If this function returns false then we stop processing any further batches.

  • fn_handle_error (Optional[HandleErrorFunction]) – Optional function that is called when there is an error

  • last_updated_start_date (datetime.datetime) – find resources updated after this datetime

  • last_updated_end_date (datetime.datetime) – find resources updated before this datetime

  • concurrent_requests (int) – number of concurrent requests to make to the server

  • page_size_for_retrieving_resources (int) – number of resources to download in one batch

  • page_size_for_retrieving_ids: – number of ids to download in one batch

  • page_size_for_retrieving_ids (int) –

Return type

List[Dict[str, Any]]

async get_resources_by_query_async(self, last_updated_start_date=None, last_updated_end_date=None, concurrent_requests=10, page_size_for_retrieving_resources=100, page_size_for_retrieving_ids=10000, fn_handle_batch=None, fn_handle_error=None)

Gets results for a query by first downloading all the ids and then retrieving resources for each id in parallel

Parameters
  • fn_handle_batch (Optional[HandleBatchFunction]) – Optional function to execute on each page of data. Note that if this is passed we don’t return the resources in the response anymore. If this function returns false then we stop processing any further batches.

  • fn_handle_error (Optional[HandleErrorFunction]) – function that is called when there is an error

  • last_updated_start_date (Optional[datetime.datetime]) – find resources updated after this datetime

  • last_updated_end_date (Optional[datetime.datetime]) – find resources updated before this datetime

  • concurrent_requests (int) – number of concurrent requests to make to the server

  • page_size_for_retrieving_resources (int) – number of resources to download in one batch

  • page_size_for_retrieving_ids: – number of ids to download in one batch

  • page_size_for_retrieving_ids (int) –

Return type

List[Dict[str, Any]]

async get_ids_for_query_async(self, *, last_updated_start_date=None, last_updated_end_date=None, concurrent_requests=10, page_size_for_retrieving_ids=10000)

Gets just the ids of the resources matching the query

Parameters
  • last_updated_start_date (Optional[datetime.datetime]) – (Optional) get ids updated after this date

  • last_updated_end_date (Optional[datetime.datetime]) – (Optional) get ids updated before this date

  • concurrent_requests (int) – number of concurrent requests

  • page_size_for_retrieving_ids (int) –

Returns

list of ids

Return type

List[str]

get_ids_for_query(self, *, last_updated_start_date=None, last_updated_end_date=None, concurrent_requests=10, page_size_for_retrieving_ids=10000)

Gets just the ids of the resources matching the query

Parameters
  • last_updated_start_date (Optional[datetime.datetime]) – (Optional) get ids updated after this date

  • last_updated_end_date (Optional[datetime.datetime]) – (Optional) get ids updated before this date

  • concurrent_requests (int) –

  • page_size_for_retrieving_ids (int) –

Returns

list of ids

Return type

List[str]

get_resources_by_query(self, last_updated_start_date=None, last_updated_end_date=None, concurrent_requests=10, page_size_for_retrieving_resources=100, page_size_for_retrieving_ids=10000, fn_handle_batch=None, fn_handle_error=None)

Gets results for a query by first downloading all the ids and then retrieving resources for each id in parallel

Parameters
  • fn_handle_batch (Optional[HandleBatchFunction]) – Optional function to execute on each page of data. Note that if this is passed we don’t return the resources in the response anymore. If this function returns false then we stop processing any further batches.

  • fn_handle_error (Optional[HandleErrorFunction]) – function that is called when there is an error

  • last_updated_start_date (Optional[datetime.datetime]) – (Optional) get ids updated after this date

  • last_updated_end_date (Optional[datetime.datetime]) – (Optional) get ids updated before this date

  • concurrent_requests (int) – number of concurrent requests to make to the server

  • page_size_for_retrieving_resources (int) – number of resources to download in one batch

  • page_size_for_retrieving_ids: – number of ids to download in one batch

  • page_size_for_retrieving_ids (int) –

Return type

List[Dict[str, Any]]