Skip to content

Commit 86afdcc

Browse files
committed
Add async template lifecycle example
1 parent 51fdac5 commit 86afdcc

5 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ For copy/paste runnable examples, take a look at
6262
[`examples/`](https://github.com/transloadit/python-sdk/tree/HEAD/examples).
6363

6464
The examples cover sync uploads, async uploads, resumable uploads, Template usage,
65-
Template lifecycle management, and Smart CDN URL signing.
65+
sync and async Template lifecycle management, and Smart CDN URL signing.
6666

6767
## Documentation
6868

‎docs/source/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ Examples
9191
For copy/paste runnable examples, take a look at `examples/`_.
9292

9393
The examples cover sync uploads, async uploads, resumable uploads, Template usage,
94-
Template lifecycle management, and Smart CDN URL signing.
94+
sync and async Template lifecycle management, and Smart CDN URL signing.
9595

9696
.. _examples/: https://github.com/transloadit/python-sdk/tree/HEAD/examples

‎examples/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ poetry run python examples/async_image_resize.py
1616
poetry run python examples/resumable_upload.py
1717
poetry run python examples/assembly_with_template.py
1818
poetry run python examples/template_lifecycle.py
19+
poetry run python examples/async_template_lifecycle.py
1920
poetry run python examples/smart_cdn_url.py
2021
```
2122

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""Create, fetch, update, and delete a Template with the async client.
2+
3+
Run from the repository root:
4+
5+
TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy poetry run python examples/async_template_lifecycle.py
6+
"""
7+
8+
import asyncio
9+
import os
10+
from uuid import uuid4
11+
12+
from transloadit.async_client import AsyncTransloadit
13+
14+
15+
def get_credentials():
16+
key = os.getenv("TRANSLOADIT_KEY")
17+
secret = os.getenv("TRANSLOADIT_SECRET")
18+
if not key or not secret:
19+
raise RuntimeError("Please set TRANSLOADIT_KEY and TRANSLOADIT_SECRET.")
20+
return key, secret
21+
22+
23+
def extract_template_id(response_data):
24+
template_id = response_data.get("id") or response_data.get("template_id")
25+
if not template_id:
26+
raise RuntimeError(f"Template response did not contain an id: {response_data}")
27+
return template_id
28+
29+
30+
async def main():
31+
key, secret = get_credentials()
32+
template_id = None
33+
deleted = False
34+
template_name = f"python-sdk-async-example-{uuid4().hex[:12]}"
35+
36+
async with AsyncTransloadit(key, secret) as client:
37+
try:
38+
template = client.new_template(template_name)
39+
template.add_step(
40+
"resize",
41+
"/image/resize",
42+
{
43+
"use": ":original",
44+
"width": 120,
45+
"height": 120,
46+
"resize_strategy": "fit",
47+
"format": "png",
48+
},
49+
)
50+
created = await template.create()
51+
template_id = extract_template_id(created.data)
52+
print("Created template:", template_id)
53+
54+
fetched = await client.get_template(template_id)
55+
print("Fetched template:", fetched.data.get("name") or fetched.data.get("template_name"))
56+
57+
updated = await client.update_template(
58+
template_id,
59+
{
60+
"name": f"{template_name}-updated",
61+
"template": {
62+
"steps": {
63+
"resize": {
64+
"use": ":original",
65+
"robot": "/image/resize",
66+
"width": 96,
67+
"height": 96,
68+
"resize_strategy": "fit",
69+
"format": "jpg",
70+
},
71+
},
72+
},
73+
},
74+
)
75+
print("Updated template:", updated.data.get("ok"))
76+
77+
deleted_response = await client.delete_template(template_id)
78+
print("Deleted template:", deleted_response.data.get("ok"))
79+
deleted = True
80+
finally:
81+
if template_id and not deleted:
82+
await client.delete_template(template_id)
83+
84+
85+
if __name__ == "__main__":
86+
asyncio.run(main())

‎tests/test_examples.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"resumable_upload.py",
1616
"assembly_with_template.py",
1717
"template_lifecycle.py",
18+
"async_template_lifecycle.py",
1819
"smart_cdn_url.py",
1920
]
2021

0 commit comments

Comments
 (0)