smart-on-fhir / client-py

Python SMART on FHIR client
http://docs.smarthealthit.org
Other
591 stars 211 forks source link

404 Errors when using searching for resources #144

Closed rahulm577 closed 3 months ago

rahulm577 commented 10 months ago

Hi There,

New to the world of EHR work. I am trying to use client-py to be able to eventually extract admission information from an EHR. I have managed to retrieve basic patient information successfully using this code:

from fhirclient import client
settings = {
    'app_id': 'my_app',
    'api_base': 'https://fhir-open.cerner.com/r4/ec2458f2-1e24-41c8-b71b-0e701af7583d/'
}
smart = client.FHIRClient(settings=settings)

import fhirclient.models.patient as p
patient = p.Patient.read('12724067', smart.server)
patient.birthDate.isostring
smart.human_name(patient.name[0])

However, I am now trying to search for patient resources and keep getting 404 errors despite being seemingly connected to the server.

import fhirclient.models.medication as mo
search = mo.Medication.where(struct={'patient': patient.id, 'status': 'active'})
medOrders = search.perform_resources(smart.server)

which results in:

FHIRNotFoundException                     Traceback (most recent call last)
Cell In[49], line 5
      3 import fhirclient.models.medication as mo
      4 search = mo.Medication.where(struct={'patient': patient.id, 'status': 'active'})
----> 5 medOrders = search.perform_resources(smart.server)

File ~\anaconda3\envs\smart-on-fhir-client-py-demo\lib\site-packages\fhirclient\models\fhirsearch.py:135, in FHIRSearch.perform_resources(self, server)
    128 def perform_resources(self, server):
    129     """ Performs the search by calling `perform`, then extracts all Bundle
    130     entries and returns a list of Resource instances.
    131     
    132     :param server: The server against which to perform the search
    133     :returns: A list of Resource instances
    134     """
--> 135     bundle = self.perform(server)
    136     resources = []
    137     if bundle is not None and bundle.entry is not None:

File ~\anaconda3\envs\smart-on-fhir-client-py-demo\lib\site-packages\fhirclient\models\fhirsearch.py:123, in FHIRSearch.perform(self, server)
    120     raise Exception("Need a server to perform search")
    122 from . import bundle
--> 123 res = server.request_json(self.construct())
    124 bundle = bundle.Bundle(res)
    125 bundle.origin_server = server

File ~\anaconda3\envs\smart-on-fhir-client-py-demo\lib\site-packages\fhirclient\server.py:169, in FHIRServer.request_json(self, path, nosign)
    160 """ Perform a request for JSON data against the server's base with the
    161 given relative path.
    162 
   (...)
    166 :returns: Decoded JSON response
    167 """
    168 headers = {'Accept': 'application/json'}
--> 169 res = self._get(path, headers, nosign)
    171 return res.json()

File ~\anaconda3\envs\smart-on-fhir-client-py-demo\lib\site-packages\fhirclient\server.py:201, in FHIRServer._get(self, path, headers, nosign)
    199 # perform the request but intercept 401 responses, raising our own Exception
    200 res = self.session.get(url, headers=headers)
--> 201 self.raise_for_status(res)
    202 return res

File ~\anaconda3\envs\smart-on-fhir-client-py-demo\lib\site-packages\fhirclient\server.py:298, in FHIRServer.raise_for_status(self, response)
    296     raise FHIRPermissionDeniedException(response)
    297 elif 404 == response.status_code:
--> 298     raise FHIRNotFoundException(response)
    299 else:
    300     response.raise_for_status()

FHIRNotFoundException: <Response [404]>

Any help would be appreciated!

mikix commented 3 months ago

This is probably too late to help, but your problem was that you were looking at Medication, not MedicationRequest. Medication is not tied to patients, and some EHRs like Cerner don't let you search/bulk-export them, only request them by ID.

MedicationRequest is the patient-oriented version. So if you do:

from fhirclient.models.medicationrequest import MedicationRequest
search = MedicationRequest.where(struct={'patient': patient.id, 'status': 'active'})
medOrders = search.perform_resources(smart.server)

That should work for you.