我使用 FHIR REST API 已經有一段時間了,但對 Python 沒有任何經驗。作為我的第一個 python 專案,我正在嘗試創建一個可以讀取和寫入開放 API 的簡單 python 腳本。我能夠閱讀,但由于以下原因,我無法創建成功的 POST:錯誤 [TypeError("unhashable type: 'dict'")]。我不完全理解 python 字典是如何作業的,并試圖使用元組但得到同樣的錯誤。
import requests #REST Access to FHIR Server
print('Search patient by MRN to find existing appointment')
MRN = input("Enter patient's MRN -try CT12181 :")
url = 'http://hapi.fhir.org/baseR4/Patient?identifier=' MRN
print('Searching for Patient by MRN...@' url)
response = requests.get(url)
json_response = response.json()
try:
key='entry'
EntryArray=json_response[key]
FirstEntry=EntryArray[0]
key='resource'
resource=FirstEntry['resource']
id=resource['id']
PatientServerId= id
patientName = resource['name'][0]['given'][0] ' ' resource['name'][0]['family']
print('Patient Found')
print('Patient Id:' id)
#Searching for assertppointments
url='http://hapi.fhir.org/baseR4/Appointment?patient=' id #fhir server endpoint
#Print appointment data
print('Now Searching for Appointments...@' url)
appt_response = requests.get(url).json()
key='entry'
EntryArray=appt_response[key]
print (f'Appointment(s) found for the patient {patientName}')
for entry in EntryArray:
appt=entry['resource']
# print('-------------------------')
# Date=appt['start']
# Status=appt['status']
# print(appt_response)
#print ('AppointmentStartDate/Time: ' ,appt['start'])
print ('Status: ' ,appt['status'])
print ('ID: ' ,appt['id'])
print('Search for open general practice slot?')
option = input('Enter yes or no: ')
while not(option == 'yes'):
print('Please search a different paitent')
option = input('Enter yes or no: ')
url = 'http://hapi.fhir.org/baseR4/Slot?service-type=57' #fhir server endpoint
print('Searching for General Practice Slot...@' url)
slot_response = requests.get(url).json()
key='entry'
EntryArray=slot_response[key]
print ('Slot(s) found for the service type General Practice')
for entry in EntryArray:
slot=entry['resource']
#print('-------------------------')
#slotDate=slot['start']
#slotStatus=slot['status']
print (f'SlotID: ' slot['id'])
#print (f'Status: ' slot['status'])
print('Book a slot?')
option = input('Enter yes or no: ')
while not(option == 'yes'):
print('Please search a different paitent')
option = input('Enter yes or no: ')
#Book slot
slotID = input("Enter slot ID :")
url = 'http://hapi.fhir.org/baseR4/Appointment' #fhir server endpoint
print('Booking slot...@' url)
headers = {"Content-Type": "application/fhir json;charset=utf-8"}
data = {{"resourceType": "Appointment","status": "booked","slot": tuple({"reference":"Slot/104602"}),"participant": tuple({"actor": {"reference":"Patient/1229151","status": "accepted"}}),"reasonCode": tuple({"text": "I have a cramp"})}}
#fhir server json header content
# headers = {"Content-Type": "application/fhir json;charset=utf-8"}
response = requests.post(url=url,headers=headers,data=data)
print(response)
print(response.json())
except Exception as e:
print ('error' ,[e])
我期待 JSON 資料成功寫入 API。我可以在 Postman 中使用相同的 JSON 資料來撥打電話,但我對這在 Python 中的作業方式并不熟悉。
uj5u.com熱心網友回復:
看起來Appointment POST 端點接受了一個簡單的有效負載,例如:
{
"resourceType": "Appointment"
}
然后根據 API 檔案回傳相應的 ID。
這與您似乎在代碼中嘗試的不同,您嘗試將其他詳細資訊傳遞給此端點:
ata = {{"resourceType": "Appointment","status": "booked","slot": tuple({"reference":"Slot/104602"}),"participant": tuple({"actor": {"reference":"Patient/1229151","status": "accepted"}}),"reasonCode": tuple({"text": "I have a cramp"})}}
但是,要向檔案中記錄的端點發出 POST 請求,也許可以嘗試json使用requests.post. 類似于以下內容:
>>> import requests
>>> headers = {"Content-Type": "application/fhir json;charset=utf-8"}
>>> json_payload = {
... "resourceType": "Appointment"
... }
>>> url = 'http://hapi.fhir.org/baseR4/Appointment'
>>> r = requests.post(url, headers=headers, json=json_payload)
>>> r
<Response [201]>
>>> r.json()
{'resourceType': 'Appointment', 'id': '2261980', 'meta': {'versionId': '1', 'lastUpdated': '2022-03-25T23:40:42.621 00:00'}}
>>>
如果您已經熟悉此 API,那么也許這可能會有所幫助。我懷疑您隨后需要使用第一個請求中回傳的 ID 向另一個端點發送另一個 POST 或 PATCH 請求以輸入相關資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451734.html
上一篇:sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)回傳除空記錄之外的所有記錄
