{ "cells": [ { "cell_type": "markdown", "id": "e0759183", "metadata": {}, "source": [ "# FHIR for Research Workshop\n", "## Exercise 0 - Getting Started\n", "#### Exercise Objective: Introduce you to the basic mechanisms for working with FHIR\n", "\n", "For this exercise, we will walk you through the following steps:\n", "
    \n", "
  1. Establish a connection to the client server
  2. \n", "
  3. Format and submit a query to the server
  4. \n", "
  5. Process response data from the FHIR server as a JSON document.
  6. \n", "
  7. View the resulting JSON to confirm that we successfully pulled data from the remote server.
  8. \n", "
\n", "\n", "### Icons in this Guide\n", " 📘 A link to a useful external reference related to the section the icon appears in " ] }, { "cell_type": "markdown", "id": "e1b59f60", "metadata": {}, "source": [ "## Step 1: Establish a Connection to the FHIR Server\n", "\n", "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.\n", "\n", "Generally we'll want to use the following python notation for all our HTTP requests:\n", "\n", "```\n", "r = s.get(url)\n", "```\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 1, "id": "e490f7e1", "metadata": {}, "outputs": [], "source": [ "# For making HTTP requests to the FHIR server\n", "import requests\n", "\n", "# Configure requests session with standard headers\n", "s = requests.Session()\n", "\n", "# Optional: Turn off SSL verification. Useful when dealing with a corporate proxy with self-signed certificates.\n", "s.verify = False\n", "requests.packages.urllib3.disable_warnings()" ] }, { "cell_type": "code", "execution_count": 2, "id": "e1ee864a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = s.get(\"https://api.logicahealth.org/researchonfhir/open/metadata\")\n", "r" ] }, { "cell_type": "markdown", "id": "5932601d", "metadata": {}, "source": [ "Hopefully you got a server status = meaning the server is up and running!" ] }, { "cell_type": "markdown", "id": "16b58166", "metadata": {}, "source": [ "## Step 2: Format and submit a query to the server\n", "\n", "We are now positioned to submit specific queries to our FHIR server and retrieve data.\n", "\n", "Generally speaking the pattern for a RESTful GET query appended to a URL will take the form of: \n", "\n", "```\n", "VERB [url]/[Resource]/[id] {?parameter=[value]}\n", "```\n", "\n", "Let's submit a sample query to return the basic information from a single patient: `smart-1032702`:\n", "\n", "- `[URL] = https://api.logicahealth.org/researchonfhir/open/`\n", "- `[Resource] = Patient`\n", "- `ID = smart-1032702`\n", "\n", "📘[Read more about FHIR HTTP Interactions](http://hl7.org/fhir/R4/http.html)" ] }, { "cell_type": "code", "execution_count": 3, "id": "38a18304", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "patient = s.get(f\"https://api.logicahealth.org/researchonfhir/open/Patient/smart-1032702\", headers={'Accept':'application/fhir+json'}, verify=False)\n", "patient" ] }, { "cell_type": "markdown", "id": "e870fe6a", "metadata": {}, "source": [ "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!" ] }, { "cell_type": "markdown", "id": "7c350ecf", "metadata": {}, "source": [ "## Step 3: Process response data from the FHIR server as a JSON document\n", "\n", "The Requests library provides a `.json` method for decoding JSON HTTP response payloads into python." ] }, { "cell_type": "code", "execution_count": 4, "id": "84da38bd", "metadata": {}, "outputs": [], "source": [ "patient_json = patient.json()" ] }, { "cell_type": "markdown", "id": "5c8ea70b", "metadata": {}, "source": [ "## Step 4: View the resulting JSON to confirm that we successfully pulled data from our remote server\n", "\n", "Let's now output the response data to confirm we successfully accessed the server and queried data." ] }, { "cell_type": "code", "execution_count": 5, "id": "139f9c4a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'resourceType': 'Patient',\n", " 'id': 'smart-1032702',\n", " 'meta': {'versionId': '1',\n", " 'lastUpdated': '2020-07-15T02:51:25.000+00:00',\n", " 'source': '#KQSArAdbxORTtqVw'},\n", " 'text': {'status': 'generated',\n", " 'div': '
Amy Shaw
'},\n", " 'identifier': [{'use': 'official',\n", " 'type': {'coding': [{'system': 'http://terminology.hl7.org/CodeSystem/v2-0203',\n", " 'code': 'MR',\n", " 'display': 'Medical Record Number'}],\n", " 'text': 'Medical Record Number'},\n", " 'system': 'http://hospital.smarthealthit.org',\n", " 'value': 'smart-1032702'}],\n", " 'active': True,\n", " 'name': [{'use': 'official', 'family': 'Shaw', 'given': ['Amy', 'V']}],\n", " 'telecom': [{'system': 'phone', 'value': '800-782-6765', 'use': 'mobile'},\n", " {'system': 'email', 'value': 'amy.shaw@example.com'}],\n", " 'gender': 'female',\n", " 'birthDate': '2007-03-20',\n", " 'address': [{'use': 'home',\n", " 'line': ['49 Meadow St'],\n", " 'city': 'Mounds',\n", " 'state': 'OK',\n", " 'postalCode': '74047',\n", " 'country': 'USA'}],\n", " 'generalPractitioner': [{'reference': 'Practitioner/smart-Practitioner-72004454'}]}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "patient_json" ] }, { "cell_type": "markdown", "id": "cc332e18", "metadata": {}, "source": [ "We can now visualize the specific patient data stored in this FHIR resource.\n", "\n", "In the next exercise, we'll look deeper into these and other requests and converting data into a Python Pandas Dataframe for analysis." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.5" } }, "nbformat": 4, "nbformat_minor": 5 }