For this exercise, we will walk you through the following steps:
📘 A link to a useful external reference related to the section the icon appears in
First, Let's string together a basic query to get the status of our FHIR server. We'll use the python requests library to submit a RESTful HTTP GET request formatted as a URL.
Generally we'll want to use the following python notation for all our HTTP requests:
r = s.get(url)
In this case, we'll start with a basic query to make sure the server is up and everything is working. We'll talk more about what this request is for in the next exercise.
# For making HTTP requests to the FHIR server
import requests
# Configure requests session with standard headers
s = requests.Session()
# Optional: Turn off SSL verification. Useful when dealing with a corporate proxy with self-signed certificates.
s.verify = False
requests.packages.urllib3.disable_warnings()
r = s.get("https://api.logicahealth.org/researchonfhir/open/metadata")
r
<Response [200]>
Hopefully you got a server status = <Response [200]> meaning the server is up and running!
We are now positioned to submit specific queries to our FHIR server and retrieve data.
Generally speaking the pattern for a RESTful GET query appended to a URL will take the form of:
VERB [url]/[Resource]/[id] {?parameter=[value]}
Let's submit a sample query to return the basic information from a single patient: smart-1032702
:
[URL] = https://api.logicahealth.org/researchonfhir/open/
[Resource] = Patient
ID = smart-1032702
patient = s.get(f"https://api.logicahealth.org/researchonfhir/open/Patient/smart-1032702", headers={'Accept':'application/fhir+json'}, verify=False)
patient
<Response [200]>
So if you output the resulting query we again (hopefully!) get a 200 status response telling us our query was successfully received. We now have to convert the response into a format we can parse locally, and to do that we'll need the JSON Library!
The Requests library provides a .json
method for decoding JSON HTTP response payloads into python.
patient_json = patient.json()
Let's now output the response data to confirm we successfully accessed the server and queried data.
patient_json
{'resourceType': 'Patient', 'id': 'smart-1032702', 'meta': {'versionId': '1', 'lastUpdated': '2020-07-15T02:51:25.000+00:00', 'source': '#KQSArAdbxORTtqVw'}, 'text': {'status': 'generated', 'div': '<div xmlns="http://www.w3.org/1999/xhtml">Amy Shaw</div>'}, 'identifier': [{'use': 'official', 'type': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/v2-0203', 'code': 'MR', 'display': 'Medical Record Number'}], 'text': 'Medical Record Number'}, 'system': 'http://hospital.smarthealthit.org', 'value': 'smart-1032702'}], 'active': True, 'name': [{'use': 'official', 'family': 'Shaw', 'given': ['Amy', 'V']}], 'telecom': [{'system': 'phone', 'value': '800-782-6765', 'use': 'mobile'}, {'system': 'email', 'value': 'amy.shaw@example.com'}], 'gender': 'female', 'birthDate': '2007-03-20', 'address': [{'use': 'home', 'line': ['49 Meadow St'], 'city': 'Mounds', 'state': 'OK', 'postalCode': '74047', 'country': 'USA'}], 'generalPractitioner': [{'reference': 'Practitioner/smart-Practitioner-72004454'}]}
We can now visualize the specific patient data stored in this FHIR resource.
In the next exercise, we'll look deeper into these and other requests and converting data into a Python Pandas Dataframe for analysis.