Skip to content

Commit

Permalink
Only require path when uploading media
Browse files Browse the repository at this point in the history
  • Loading branch information
bakeable committed Jun 4, 2024
1 parent 604c7e6 commit 20fc404
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
21 changes: 14 additions & 7 deletions akeneo_connector/akeneo_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,34 @@ def get_attribute(self, attributecode: str):
data = response.text

return data

def upload_media(self, product_dict: dict, filename: str, file_type: str, base64_file: str):
def upload_media(self, product_dict: dict, file_path: str):
"""
Uploads media to Akeneo.
Args:
product_dict (dict): The product info to send in the request.
media_file (str): The base64 encoded the media file.
file_path (str): The path to the local media file.
"""

# Infer filename and file type from path
binary = open(file_path, 'rb').read()
filename = os.path.basename(file_path)
file_type = 'image/jpeg' if filename.endswith('.jpg') else 'application/pdf'

# Create fields
fields = {
"product": product_dict,
"file": (filename, base64_file, file_type)
"product": json.dumps(product_dict),
"file": (filename, binary, file_type)
}

# Encode body and header
encoded_body, headers = encode_multipart_formdata(fields, None)
encoded_body, content_type = encode_multipart_formdata(fields)

# Add authorization
headers['Authorization'] = 'Bearer ' + self.access_token
headers = {
'Content-Type': content_type,
'Authorization': 'Bearer ' + self.access_token,
}

# Send the request to the Akeneo API
print(f"POST {self.products_media_url}")
Expand Down
8 changes: 3 additions & 5 deletions akeneo_connector/akeneo_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,8 @@ def get_media(self, media_attribute: str, locale: str | None = None, scope: str

def set_media(
self,
attribute: str,
filename: str,
file_type: str,
base64_file: str,
attribute: str,
file_path: str,
locale: str | None = None,
scope: str | None = None
):
Expand All @@ -376,4 +374,4 @@ def set_media(
'attribute': attribute,
'locale': locale,
'scope': scope
}, filename, file_type, base64_file)
}, file_path=file_path)

0 comments on commit 20fc404

Please sign in to comment.