Skip to main content

Partner Client for CommonHealth Cloud Storage Service

Project description

This is a lightweight Python client to interact with CommonHealth Cloud Storage Service. It requires Python 3.6+.

Installation

You can use pip to install it like so: pip install commonhealth-cloud-storage-client

It has dependencies on the following packages:

cryptography==42.0.4
requests==2.31.0
tink==1.9.0

Configuration

The client (represented by the class CHClient) requires parameters to be passed on initialization. They are:

  • Partner Client ID -- a UUID issued by TCP to your organization. This is not secret.
  • Partner Client Secret -- a secret value issued by TCP to your organization. Store alongside any other secrets in your system.
  • Partner ID -- a UUID issued by TCP to your organization. This is not secret.
  • Delegate -- the client library requires a persistence layer. An object implementing an expected interface (a subclass of the CHStorageDelegate type) must be passed to the client. A default, SQlite-based implementation is included and provided through the SQLiteDelegate class.
  • CH Authorization Deeplink -- this is a CommonHealth URL. For production, this will be: https://app.commonhealth.org/m/phr/cloud-sharing/authorize; for dev it will be: https://appdev.tcpdev.org/m/phr/cloud-sharing/authorize.
  • Storage Service Host -- the URL to the TCP Storage Service. For dev, this is chcs.tcpdev.org
  • Storage Service Port -- the port over which to connect to the TCP Storage Service. Set to 443.
  • Storage Service Scheme -- the connection scheme for HTTP connections to the TCP Storage Service. Set to "https".
  • Logging Enabled -- boolean indicating if logging is enabled

If you're using the provided SQLite-based delegate, through the SQLiteDelegate class, it requires some parameters as well:

  • Path to DB file -- the path to where the DB file should be written to, including the name. The class will automatically create the DB file if it doesn't exist.
  • DB passphrase -- a secret value, ideally with high entropy, that's used to generate an encryption key to encrypt database contents under.
  • DB passphrase salt -- a secret value that's used to generate an encryption key using the passphrase.
  • (Optional) Logger -- an optional logger instance to use. If none provided, no logs will be emitted.

After initializing the SQLiteDelegate, call initialize() for it assemble the db file and generate the encryption key before calling any of its other methods.

Configuration example - using the sample SQLiteDelegate

from commonhealth_cloud_storage_client import (
    SQLiteDelegate,
    CHClient
)

# create delegate
delegate = SQLiteDelegate(
    path_to_db_file="ch-delegate.db",
    db_passphrase="thisssasecretvaluewithhighentropy",
    db_passphrase_salt="thisisanothersecretvalue",
    logger=logging.getLogger("CommonHealthClientSQLiteDelegate")
)
# call initialize()
delegate.initialize()

# create CHClient
ch_client = CHClient(
    "5c1eeddd-81ca-45b9-901f-791184a6f57a", # Client ID, provided by TCP. Non-secret.
    "anothersecretvalue", # Client Secret, provided by TCP. Secret!
    "00e05764-bd84-4f11-9a4b-6f5c27f4519f", # Partner ID, provided by TCP. Non-secret.
    delegate,
    "https://appdev.tcpdev.org/m/phr/cloud-sharing/authorize",
    "chcs.tcpdev.org",
    443,
    "https"
)

# you can now use the client...

Registering signing keys

After initializing the objects, the client will need to generate and register a signing key with TCP's servers. This is also a chance to configure branding elements -- name and logo URL -- that will appear in the CommonHealth app to identify the partner to users.

This call looks like this, and requires network connectivity:

ch_client.perform_initialization(
    "Partner Name",
    "https://yourwebsite.com/logo.png"
)

Generating a deeplink to share with a specific user

Each deeplink is specific to a given user and should not be re-used with other users. To generate a deeplink, call construct_authorization_request_deeplink with the following parameters.

  • Patient ID -- A stable identifier for a given patient. This can be a hash of an actual patient ID.
  • Scope -- The scope of data being requested as a string. Recommend the following to READ OpenMHealth BP and HR data: OMHealthResource.HeartRate.Read OMHealthResource.BloodPressure.Read.
  • Sharing duration seconds -- The number of seconds that the data is shared for. For now, put 2592000, or 30 days.
  • (Optional) Rotate encryption key -- Useful for testing, but recommend setting as false.

A sample call:

deeplink = ch_client.construct_authorization_request_deeplink(
    "patient123",
    "OMHealthResource.HeartRate.Read OMHealthResource.BloodPressure.Read",
    2592000
)

The resulting deeplink will be a fully-formed string URL looking something like:

https://appdev.tcpdev.org/m/phr/cloud-sharing/authorize?authorization_request=eyJhbGciOiAiVGlua0VTMjU2IiwgInR5cCI6ICJKV1QifQ.eyJzY29wZSI6ICJPTUhlYWx0aFJlc291cmNlLkhlYXJ0UmF0ZS5SZWFkIE9NSGVhbHRoUmVzb3VyY2UuQmxvb2RQcmVzc3VyZS5SZWFkIiwgImV4cCI6IDE2NDc0NzUzMzAsICJpYXQiOiAxNjQ2ODcwNT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iwgImFsZ29yaXRobSI6ICJFQ0lFU19QMjU2X0hLREZfSE1BQ19TSEEyNTZfQUVTMTI4X0dDTSIsICJsaWJyYXJ5IjogIlRJTksiLCAic2VyaWFsaXphdGlvbl9tb2RlIjogIkpTT04ifX0.ARtUTJowRQIhAMr1rFNsYVs04HCqiMf5E4BTZmKmn_8wE8Nv_aqplewZAiBieJdZSTkVZNEn8lza82BzKbuOhdNMnj-RjKRJMLO4mQ

Fetching data for a given patient

The call to retrieve data is very simple - requiring just the same unique patient identifier used before:

from commonhealth_storage_service.errors import UserNotConsented, HTTPError, DelegateStateError

try:
    patient_data = ch_client.fetch_data("patient123")
except UserNotConsented as e:
    logger.info("Patient not consented")
except DelegateStateError as e:
    logger.warn("Something went wrong")
except Exception as e:
    logger.exception("Something else went wrong")

The patient_data returned is a list of ResourceHolder objects. The raw, plaintext JSON can be accessed through the json_content property on each ResourceHolder.

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

commonhealth_cloud_storage_client-1.0.1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file commonhealth_cloud_storage_client-1.0.1.tar.gz.

File metadata

File hashes

Hashes for commonhealth_cloud_storage_client-1.0.1.tar.gz
Algorithm Hash digest
SHA256 fb5692cc21c7d712a61f92dc0d07ba1d5a958d54ef593af4b4ee9f2bd5fc1a4f
MD5 7d41f071348914c0f2ea887388cc98a5
BLAKE2b-256 d4a431a80593f360ed8b948fee5d8c21e9d42f5d3981a35994725a16e398524f

See more details on using hashes here.

File details

Details for the file commonhealth_cloud_storage_client-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for commonhealth_cloud_storage_client-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b25f37053ebaa05746b2ec40a2332f3cbced478ae40cc133a06dcd66462b401d
MD5 d645661cbfc255281f7ad757336e6856
BLAKE2b-256 1fd083315a36bf2a51a983fca0291f4e175edcbb21374990a970c31c62909079

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