diff --git a/importer/main.py b/importer/main.py index ed92c021..acf8e5e9 100644 --- a/importer/main.py +++ b/importer/main.py @@ -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 @@ -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} + 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" + }, + "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 diff --git a/importer/requirements.txt b/importer/requirements.txt index ac3b213f..4c748647 100644 --- a/importer/requirements.txt +++ b/importer/requirements.txt @@ -6,4 +6,5 @@ urllib3==2.0.3 backoff==2.2.1 pytest==7.4.2 jsonschema==4.21.1 -mock==5.1.0 \ No newline at end of file +mock==5.1.0 +python-magic==0.4.27 \ No newline at end of file diff --git a/importer/sample_config.py b/importer/sample_config.py index d46cc9ac..b8432523 100644 --- a/importer/sample_config.py +++ b/importer/sample_config.py @@ -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'