smart-on-fhir / client-py

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

attaching binary data in DocumentReference - Attachment expects data to be <class 'str'>? #92

Closed themantalope closed 3 months ago

themantalope commented 3 years ago

Hi all,

I'm very new to FHIR and I'm learning. Any help is much appreciated. I'm trying to attach a PDF file to a document reference for a patient. I'm using the following code:

from fhirclient.models.patient import Patient
from fhirclient.models.documentreference import DocumentReference
from fhirclient.models.attachment import Attachment
from fhirclient.models.binary import Binary
from fhirclient import client

# client to local HAPI FHIR server
smart = client.FHIRClient({'api_base':'http://localhost:8080/baseDtsu3', 'app_id':''})

smart.prepare()

# get a patient
patient = Patient.read('1', smart.server)

# make a document reference
dr = DocumentReference()
dr.subject = patient

# make attachment
a = Attachment()
a.contentType = 'application/pdf'
# read in pdf file
pdf_file = ....
pdf_data = None
with open(pdf_file, 'rb') as f:
  pdf_data = f.read()

a.data = pdf_data
dr.attachment = a

# now try to post
dr.create(smart.server)

When I try running this code, I get an error stating that Expecting property "data" on <class 'fhirclient.models.attachment.Attachment'> to be <class 'str'>, but is <class 'bytes'>. How should the binary data be stored?

I could do something like:

pdf_bin = None
import base64
with open(pdf, 'rb') as f:
 pdf_bin = f.read()
 pdf_b64 = base64.b64encode(pdf_bin)
 pdf_b64_m = pdf_b64.decode('utf-8')

Where now pdf_b64_m is a UTF-8 encoded string. Is this the correct way to store the data? I would then be able to re-encode it back to binary, but I want to make sure that this is correct so other programs that download the data from the server can interpret the PDF correctly.

mikix commented 3 months ago

I know this is very late, but yes - that's the strategy for embedding data in a DocumentReference.

See the spec and how it marks data as a base64Binary field.

It is lightly annoying that you have to then encode that as a unicode string for fhirclient's benefit, but that's the current workflow.