Dienstag, 16. Juni 2015

The Game of the APIs

Inspired by Ewout Kramer's speech  at the HL7 WGM in Paris, I decided to play the "Game Of The APIs" for a bit.

The result is an interface, that listens to a Twitter stream and translates the incoming Tweets tagged with #fhir into Fhir Resources: one containing the Tweet and one representing the Tweep (Twitter user).
The Resources are then bundled into a Transaction and sent to a public test server.

This interface is completely useless in a real world scenario, but it is a nice exercise :)



So this:



is translated into this:
http://fhir-dev.healthintersections.com.au/open/Patient/223084
and this:

http://fhir-dev.healthintersections.com.au/open/Communication/55

Sending both resources in an transaction type Bundle to the FHIR Server allows to pull a few nice tricks:
  • by assigning a temporal id to the Patient Entry

    <Bundle xmlns="http://hl7.org/fhir">
      <type value="transaction"/>
      <entry>
        <resource>
          <Patient xmlns="http://hl7.org/fhir">
            <id value="patient_temp_id"/>

    we can use this id to reference from the Communication resource to the Patient resource:

    <Communication xmlns="http://hl7.org/fhir">  ... 
      <sender>
        <reference value="Patient/patient_temp_id"/>  </sender>

    The temporal Id will then be replaced with the actual Id by the server.
  • By setting the BaseURL of the Patient resources to a conditional URL, thus performing a conditional update, we can make sure to reidentify out Tweep and perform an update instead of creating duplicates, if the same Tweep tweets multiple Tweets.
    <transaction>  
      <method value="PUT"/>  
      <url value="/Patient?identifier=http://twitter.com|2922960201"/>
    </transaction>
  • And finally, the transaction style processing makes sure, we do not end up with a Tweet without a Tweep or vice versa, in case one of the resources can not be created successfully.
And - just for the fun of it: 
  • the interface places all hashtags used in the Tweet into the Communication Resource's metadata:
    <Communication xmlns="http://hl7.org/fhir">
      <meta>
        <tag>
          <system value="http://twitter.com"/>
          <code value="http://twitter.com/hashtag/fhir"/>
          <display value="#fhir"/>
        </tag>
      </meta>
  • the Tweep's avatar is placed into the narrative part of the Patient resource, hence the pretty display above the code in the screenshot taken from the public test server


Here's the full Transaction Bundle: >link<

I consider this a proof-of-concept to show how other (health related) APIs can integrate with Fhir.
Think of Withings, Fitbit, Apple Health and all these toys out there, collecting relevant patient information!