-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (58 loc) · 1.96 KB
/
Copy pathmain.py
File metadata and controls
71 lines (58 loc) · 1.96 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Create a Transloadit devdock TUS upload with bytes in the creation request."""
import sys
from io import BytesIO
from pathlib import Path
from tusclient import client as tus
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from api2devdock import (
fail,
load_scenario,
scenario_bytes,
scenario_id,
tus_url,
upload_metadata,
write_result,
)
def upload_with_creation_body(scenario, create_response):
upload_config = scenario["upload"]
content = scenario_bytes(upload_config)
if upload_config["chunkSize"] != "full-file":
fail("unsupported chunk size policy {!r}".format(upload_config["chunkSize"]))
if not upload_config["uploadDataDuringCreation"]:
fail("scenario does not enable uploadDataDuringCreation")
uploader = tus.TusClient(
tus_url(upload_config, scenario, create_response)
).create_upload_with_data(
len(content),
file_stream=BytesIO(content),
chunk_size=len(content),
metadata=upload_metadata(upload_config, scenario, create_response),
retries=upload_config["retries"],
)
uploader.upload()
if not uploader.url:
fail("creation-with-upload did not expose an upload URL")
if uploader.offset != len(content):
fail(
"creation-with-upload accepted {} bytes, expected {}".format(
uploader.offset,
len(content),
)
)
return {
"acceptedBytes": uploader.offset,
"uploadUrl": uploader.url,
}
def main():
scenario = load_scenario(Path(__file__).with_name("api2-scenario.json"))
create_response = scenario["prepared"]["createResponse"]
result = upload_with_creation_body(scenario, create_response)
write_result(result)
print(
"Python TUS SDK devdock scenario {} uploaded during creation to {}".format(
scenario_id(scenario),
result["uploadUrl"],
)
)
if __name__ == "__main__":
main()