lifeomic / phc-sdk-py

The phc-sdk-py is a developer kit for interfacing with the PHC API on Python 3.8 and above.
https://lifeomic.github.io/phc-sdk-py/index.html
MIT License
1 stars 2 forks source link

Add DSTU3 CRUD operations for all FHIR classes 🎉 #139

Closed rcdilorenzo closed 3 years ago

rcdilorenzo commented 3 years ago

For many reasons, we need to actually be able to easily consume the FHIR DSTU3 APIs provided. This single class along with a clever class property brings create, read, update, and delete operations to every FSS entity in the phc.easy module including but not limited to Patient, Observation, DocumentReference, Condition, Procedure, Composition, etc... 🎉

Here's how to use it:

# ========================
# ======== Create ========
# ========================

# Create a patient resource
# (Note that it only returns a boolean--not any data.)
phc.Patient.DSTU3.create({
 'meta': {'tag': [{'system': 'http://lifeomic.com/fhir/dataset',
    'code': '637475e1-3b26-4d78-87eb-5df66ab9ef59'}],
  'lastUpdated': '2021-03-05T01:52:33.539Z'},
 'resourceType': 'Patient',
 'identifier': [{'system': 'lrn',
   'type': {'coding': [{'code': 'ANON',
      'system': 'http://hl7.org/fhir/v2/0203'}]}}],
 'name': [{'use': 'official', 'given': ['Jane'], 'family': 'Doe'}],
 'address': [{'use': 'home'}],
 'telecom': [{'system': 'email'}, {'system': 'phone'}],
})

# Get the last patient (assumes very few records)
patient_id = phc.Patient.get_data_frame().iloc[-1].id

# ========================
# ======== Update ========
# ========================

# Update the address->use to be "work" instead of "home"
from funcy import set_in
phc.Patient.DSTU3.update(patient_id, lambda p: set_in(p, ["address", 0, "use"], "work"))

# ========================
# ========= Read =========
# ========================

# Get the updated record
phc.Patient.DSTU3.get(patient_id)
# {'meta': {'tag': [{'system': 'http://lifeomic.com/fhir/dataset',
#     'code': '637475e1-3b26-4d78-87eb-5df66ab9ef59'}],
#   'lastUpdated': '2021-03-05T02:24:18.414Z'},
#  'resourceType': 'Patient',
#  'identifier': [{'system': 'lrn',
#    'type': {'coding': [{'code': 'ANON',
#       'system': 'http://hl7.org/fhir/v2/0203'}]}}],
#  'name': [{'use': 'official', 'given': ['Jane'], 'family': 'Doe'}],
#  'address': [{'use': 'work'}],
#  'telecom': [{'system': 'email'}, {'system': 'phone'}],
#  'id': 'b5a77efe-9e1f-4f2f-8d5f-57c5a6c8f839'}

# ========================
# ======== Delete ========
# ========================

# Delete the record
phc.Patient.DSTU3.delete(patient_id)