2import requests
3from typing import Optional
70a7a71code formattingJeremy Magland 5
6def create_signed_upload_url(
7 url: str, size: int, user_id: str, memobin_api_key: str
8) -> str:
11 Args:
12 url: The target URL for the file
13 size: Size of the file in bytes
14 user_id: User ID for memobin
15 memobin_api_key: API key for memobin authentication
17 Returns:
18 The signed upload URL
20 Raises:
21 ValueError: If the URL prefix is invalid
22 requests.RequestException: If the API request fails
23 """
24 prefix = "https://tempory.net/f/memobin/"
25 if not url.startswith(prefix):
26 raise ValueError("Invalid url. Does not have proper prefix")
31 response = requests.post(
32 tempory_api_url,
33 headers={
34 "Content-Type": "application/json",
37 json={
38 "appName": "memobin",
39 "filePath": file_path,
40 "size": size,
42 },
45 if not response.ok:
46 raise requests.RequestException("Failed to get signed url")
48 result = response.json()
49 upload_url = result["uploadUrl"]
50 download_url = result["downloadUrl"]
52 if download_url != url:
53 raise ValueError("Mismatch between download url and url")
55 return upload_url
70a7a71code formattingJeremy Magland 57
58def construct_memobin_url(
59 alg_name: str,
60 dataset_name: str,
61 alg_version: str,
62 dataset_version: str,
63 system_version: str,
64) -> str:
67 Args:
68 alg_name: Name of the algorithm
69 dataset_name: Name of the dataset
70 alg_version: Version of the algorithm
71 dataset_version: Version of the dataset
72 system_version: Version of the system
74 Returns:
75 The constructed memobin URL
76 """
77 path = f"{alg_name}/{dataset_name}/{alg_version}/{dataset_version}/{system_version}/metadata.json"
78 return f"https://tempory.net/f/memobin/{path}"
70a7a71code formattingJeremy Magland 80
81def upload_to_memobin(
82 metadata: dict, url: str, user_id: str, memobin_api_key: str
83) -> None:
86 Args:
87 metadata: The metadata to upload
88 url: The target URL for the file
89 user_id: User ID for memobin
90 memobin_api_key: API key for memobin authentication
92 Raises:
93 requests.RequestException: If the upload fails
94 """
98 upload_url = create_signed_upload_url(url, size, user_id, memobin_api_key)
100 response = requests.put(
70a7a71code formattingJeremy Magland 101 upload_url, data=metadata_bytes, headers={"Content-Type": "application/json"}
104 if not response.ok:
105 raise requests.RequestException("Failed to upload metadata to memobin")
70a7a71code formattingJeremy Magland 107
109 """Download metadata from memobin.
111 Args:
112 url: The URL to download from
114 Returns:
115 The downloaded metadata as a dictionary, or None if not found
117 Raises:
118 requests.RequestException: If the download fails for a reason other than 404
119 """
120 response = None
121 try:
122 response = requests.get(url)
123 if response.status_code == 404:
124 return None
125 response.raise_for_status()
126 return response.json()
127 except requests.RequestException as e:
128 if response and response.status_code == 404:
129 return None
130 raise e