-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempfile_wrapper.py
More file actions
45 lines (35 loc) · 1.27 KB
/
Copy pathtempfile_wrapper.py
File metadata and controls
45 lines (35 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from json import dumps as stringify
from requests import get, post, Response
def put_blob(filename, file_bytes) -> dict:
try:
response: Response = post(
"https://tempfile.org/api/upload/local",
files={"files": (filename, file_bytes)},
data={"expiryHours": "1"}
)
if not response.ok:
raise Exception(f"HTTP {response.status}: {response.statusText}")
result: dict = response.json()
if not result.get("success", False):
raise Exception("No success!")
return result
except Exception as error:
print('Upload failed:', error.message)
return {"success": False}
def get_blob(url) -> dict:
response: Response = get(url)
if not response.ok:
print("Response not OK")
return {"success": False}
body: dict = response.json()
if not body.get("success", False):
print("No success!")
return {"success": False}
return body
def get_bytes(body: dict):
if not body.get("success", False):
raise Exception("Get bytes fail")
response = get(f"https://tempfile.org{body["file"]["downloadUrl"]}")
if not response.ok:
raise Exception("Get bytes fail, response not ok")
return response.content