def get_meds()

in LambdaFunction/FHIRClient.py [0:0]


    def get_meds(self, patient_id):
        res_token = self.get_access_token(self.client_id, self.endpoint_token)
        
        if res_token['status'] == 200:
            headers = {'Authorization': 'Bearer {}'.format(res_token['data']['access_token']), 'Content-Type': 'application/json'}
            logger.debug(headers)
            
            r = self.http.request('GET', self.endpoint_stu3+'MedicationStatement', fields={'patient': patient_id}, headers=headers)
            if r.status == 200:
                dat = json.loads(r.data.decode())
                if 'total' in  dat and dat['total']>0:
                    medications = []
                    for entry in dat['entry']:
                        ms = entry['resource']
                        if 'issue' not in ms:
                            medication = {}
                            medication['status'] = ms['status']
                            medication['category'] = ms['category']['text']
                            medication['dateAsserted'] = ms['dateAsserted']
                            medication['subject'] = ms['subject']['display']
                            if 'dosage' in ms:
                                medication['dosage'] =[]
                                for d in ms['dosage']:
                                    dosage = {}
                                    if 'text' in d:
                                        dosage['patientInstruction'] = d['text']
                                    if 'patientInstruction' in d:
                                        dosage['patientInstruction'] = dosage['patientInstruction']
                                    if 'route' in d:
                                        dosage['route'] = d['route']['text']
                                    if 'timing' in d:
                                        dosage['timing'] = d['timing']
                                    medication['dosage'].append(dosage)
                            if 'medicationReference' in ms:
                                medication['medicationReference'] = ms['medicationReference']['display']
                            medications.append(medication)
                    response = medications
                else:
                    response = 'No medication has been found.'
            else:
                response = r.data.decode()
                logger.info(r)
            return {
                'status': r.status,
                'response': response
            }
        else: 
            return {
                'status': 400,
                'response': 'JWT token not found'
            }