Skip to content

Commit

Permalink
feat: adds fetch-bundle.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tuddman committed Apr 2, 2024
1 parent c3f2609 commit d849075
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
43 changes: 43 additions & 0 deletions examples/fetch-bundle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import requests
import os

def download_messages_bundle(bundle_id, hmac_value):
"""
Downloads a file from the specified endpoint using a GET request with an X-HMAC header.
Parameters:
- bundle_id (str): The unique identifier for the file.
- hmac_value (str): The HMAC signature value of the file with `bundle_id`.
"""

# Construct the URL with the file's BUNDLE_ID
url = f"http://0.0.0.0:5558/files/{bundle_id}"

# Send the GET request with the X-HMAC header
headers = {'X-HMAC': hmac_value}
response = requests.get(url, headers=headers)

if response.status_code == 200:
print("File downloaded successfully.")
# Here, you will want to save the file content to a file, for example:
with open(f"messages_bundle_{bundle_id}", 'wb') as file:
file.write(response.content)
else:
print(f"Failed to download file. Status code: {response.status_code} Response: {response.text}")


if __name__ == "__main__":
# The assigned bundle_id returned from calling `python upload-bundle.py`
bundle_id = os.environ.get("BUNDLE_ID", "").encode()
# The value from calculating the hmac signature for the uploaded messages bundle file
hmac_value = os.environ.get("HMAC_VALUE", "").encode()

if not bundle_id:
print("BUNDLE_ID environment variable is not set.")
exit(1)

if not hmac_value:
print("HMAC_VALUE environment variable is not set.")
exit(1)

download_messages_bundle(bundle_id, hmac_value)
6 changes: 3 additions & 3 deletions examples/upload-bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
import os

file_path = "test_bundle.txt"
file_path = "test_bundle.aes"

secret_key = os.environ.get("SECRET_KEY", "").encode()

Expand All @@ -15,12 +15,12 @@
# The request payload consisting of a message history bundle
with open(file_path, 'rb') as file:
file_content = file.read()
print(file_content)
# print(file_content)

# Compute the HMAC
hmac_instance = hmac.new(secret_key, file_content, hashlib.sha256)
hmac_hex = hmac_instance.hexdigest()
print(hmac_hex)
print(f"HMAC: {hmac_hex}")

# Send the request with the HMAC header
headers = {'X-HMAC': hmac_hex}
Expand Down

0 comments on commit d849075

Please sign in to comment.