Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save image as binary resource #169

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import logging
import logging.config
import backoff
import base64
import magic
from datetime import datetime
from oauthlib.oauth2 import LegacyApplicationClient
from requests_oauthlib import OAuth2Session
Expand Down Expand Up @@ -1102,6 +1104,62 @@ def export_resources_to_csv(resource_type, parameter, value, limit):
logging.error(f"Failed to retrieve resource. Status code: {response[1]} response: {response[0]}")


def encode_image(image_file):
with open(image_file, 'rb') as image:
image_b64_data = base64.b64encode(image.read())
return image_b64_data


# This function takes in the source url of an image, downloads it, encodes it,
# and saves it as a Binary resource. It returns the id of the Binary resource if
# successful and 0 if failed
def save_image(image_source_url):
headers = {"Authorization": "Bearer " + config.product_access_token}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assumption here is that these images are coming from a different source that requires different authorization from the fhir server

data = requests.get(url=image_source_url, headers=headers)
if data.status_code == 200:
with open('images/image_file', 'wb') as image_file:
image_file.write(data.content)

# get file type
mime = magic.Magic(mime=True)
file_type = mime.from_file('images/image_file')

encoded_image = encode_image('images/image_file')
resource_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, image_source_url))
payload = {
"resourceType": "Bundle",
"type": "transaction",
"entry": [{
"request": {
"method": "PUT",
"url": "Binary/" + resource_id,
"ifMatch": "1"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll make the version and id configurable her when we add the final piece of code that calls this function when adding products so that edits also work

},
"resource": {
"resourceType": "Binary",
"id": resource_id,
"contentType": file_type,
"data": str(encoded_image)
}
}]
}
payload_string = json.dumps(payload, indent=4)
response = handle_request("POST", payload_string, get_base_url())
if response.status_code == 200:
logging.info("Binary resource created successfully")
logging.info(response.text)
return resource_id
else:
logging.error("Error while creating Binary resource")
logging.error(response.text)
return 0
else:
logging.error("Error while attempting to retrieve image")
logging.error(data)
return 0



class ResponseFilter(logging.Filter):
def __init__(self, param=None):
self.param = param
Expand Down
3 changes: 2 additions & 1 deletion importer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ urllib3==2.0.3
backoff==2.2.1
pytest==7.4.2
jsonschema==4.21.1
mock==5.1.0
mock==5.1.0
python-magic==0.4.27
1 change: 1 addition & 0 deletions importer/sample_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
fhir_base_url = 'https://example.smartregister.org/fhir'
keycloak_url = 'https://keycloak.smartregister.org/auth/admin/realms/example-realm'
access_token = 'example-access-token'
product_access_token = 'example-product-access-token'
Loading