templr 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- templr-0.1.0/.gitignore +10 -0
- templr-0.1.0/CHANGELOG.md +21 -0
- templr-0.1.0/LICENSE +21 -0
- templr-0.1.0/PKG-INFO +286 -0
- templr-0.1.0/README.md +256 -0
- templr-0.1.0/pyproject.toml +89 -0
- templr-0.1.0/src/templr/__init__.py +82 -0
- templr-0.1.0/src/templr/_client.py +134 -0
- templr-0.1.0/src/templr/_transport.py +268 -0
- templr-0.1.0/src/templr/errors.py +183 -0
- templr-0.1.0/src/templr/models.py +190 -0
- templr-0.1.0/src/templr/py.typed +0 -0
- templr-0.1.0/src/templr/resources/__init__.py +6 -0
- templr-0.1.0/src/templr/resources/assets.py +137 -0
- templr-0.1.0/src/templr/resources/templates.py +234 -0
- templr-0.1.0/tests/__init__.py +0 -0
- templr-0.1.0/tests/conftest.py +44 -0
- templr-0.1.0/tests/openapi.json +1697 -0
- templr-0.1.0/tests/payloads.py +99 -0
- templr-0.1.0/tests/test_async_client.py +105 -0
- templr-0.1.0/tests/test_client.py +318 -0
- templr-0.1.0/tests/test_contract.py +178 -0
templr-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to the Templr Python SDK are documented here.
|
|
4
|
+
|
|
5
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the SDK follows
|
|
6
|
+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html). Releases are tagged `sdks/python/vX.Y.Z`.
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-12-03
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- `Templr` and `AsyncTemplr` clients for the Templr REST API v1, authenticated with a workspace API key.
|
|
15
|
+
- Templates: `list`, `list_drafts`, `get`, `get_draft`, `render`, `list_variables` and `update_variable`.
|
|
16
|
+
- Assets: `list`, `upload`, `create_upload`, `confirm` and `delete`. `upload` starts an upload, sends the file
|
|
17
|
+
to the storage bucket and confirms it in one call.
|
|
18
|
+
- `get_openapi_document` to read the OpenAPI document served by an instance.
|
|
19
|
+
- Typed, immutable Pydantic models for every response.
|
|
20
|
+
- Typed errors with a machine-readable `code`, one class per HTTP status family.
|
|
21
|
+
- `UploadError`, raised when the storage bucket rejects the file of an upload.
|
templr-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sebastian Marat Urdanegui Bisalaya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
templr-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: templr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python SDK for the Templr REST API.
|
|
5
|
+
Project-URL: Homepage, https://github.com/SebastianUrdaneguiBisalaya/templr
|
|
6
|
+
Project-URL: Documentation, https://github.com/SebastianUrdaneguiBisalaya/templr/tree/main/sdks/python#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/SebastianUrdaneguiBisalaya/templr/tree/main/sdks/python
|
|
8
|
+
Project-URL: Issues, https://github.com/SebastianUrdaneguiBisalaya/templr/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/SebastianUrdaneguiBisalaya/templr/blob/main/sdks/python/CHANGELOG.md
|
|
10
|
+
Author: Sebastian Marat Urdanegui Bisalaya
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: api-client,email,email-templates,react-email,sdk,templr
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Framework :: Pydantic :: 2
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Topic :: Communications :: Email
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Requires-Python: >=3.11
|
|
27
|
+
Requires-Dist: httpx<1,>=0.28.1
|
|
28
|
+
Requires-Dist: pydantic<3,>=2.11
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# Templr Python SDK
|
|
32
|
+
|
|
33
|
+
The official Python client for the [Templr](https://github.com/SebastianUrdaneguiBisalaya/templr) REST API.
|
|
34
|
+
Render the email templates your team designs in Templr from any Python backend, with typed models,
|
|
35
|
+
typed errors, and sync and async clients.
|
|
36
|
+
|
|
37
|
+
- Python 3.11+
|
|
38
|
+
- Fully typed (`py.typed`): your editor autocompletes every method and model
|
|
39
|
+
- Sync (`Templr`) and async (`AsyncTemplr`) clients, built on [httpx](https://www.python-httpx.org/)
|
|
40
|
+
- Immutable [Pydantic](https://docs.pydantic.dev/) models for every response
|
|
41
|
+
|
|
42
|
+
Templr does not send emails. Render a template with this SDK, then send the HTML and text with the email
|
|
43
|
+
provider you already use.
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install templr
|
|
49
|
+
# or
|
|
50
|
+
uv add templr
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Quick start
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from templr import Templr
|
|
57
|
+
|
|
58
|
+
with Templr(api_key="tmpl_...", base_url="https://templr.example.com") as client:
|
|
59
|
+
email = client.templates.render(
|
|
60
|
+
"2b0f3a0e-6a1c-4d43-9a57-1f1f6b7d5e01",
|
|
61
|
+
values={"name": "Ada"},
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
print(email.html) # The email HTML
|
|
65
|
+
print(email.text) # The plain text version
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Authentication
|
|
69
|
+
|
|
70
|
+
Every request is authenticated with a workspace API key and scoped to that workspace. Create keys in
|
|
71
|
+
Templr under the workspace settings.
|
|
72
|
+
|
|
73
|
+
- A `read` key can list, get and render templates, and list images.
|
|
74
|
+
- A `write` key can also upload and delete images.
|
|
75
|
+
|
|
76
|
+
Keys have a fixed expiration and are never renewed automatically. Keep them secret: use them only on
|
|
77
|
+
your server, never in a browser or mobile app. Read the key from the environment instead of hard-coding
|
|
78
|
+
it:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
import os
|
|
82
|
+
|
|
83
|
+
from templr import Templr
|
|
84
|
+
|
|
85
|
+
client = Templr(api_key=os.environ["TEMPLR_API_KEY"], base_url=os.environ["TEMPLR_BASE_URL"])
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`base_url` is the URL of your Templr instance, since Templr is self-hosted. If Templr is served under a
|
|
89
|
+
path, include it: `https://example.com/templr`.
|
|
90
|
+
|
|
91
|
+
## Usage
|
|
92
|
+
|
|
93
|
+
### Templates
|
|
94
|
+
|
|
95
|
+
By default the API serves the latest **published** version of each template. The `*_draft` methods and
|
|
96
|
+
`draft=True` serve the draft editors are working on.
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
# Published templates, most recently published first
|
|
100
|
+
page = client.templates.list(limit=50)
|
|
101
|
+
for version in page.items:
|
|
102
|
+
print(version.template_id, version.name, version.version, version.published_at)
|
|
103
|
+
|
|
104
|
+
# Every template with its draft, most recently updated first
|
|
105
|
+
drafts = client.templates.list_drafts()
|
|
106
|
+
|
|
107
|
+
# One template, with its document
|
|
108
|
+
version = client.templates.get(template_id) # latest published version
|
|
109
|
+
draft = client.templates.get_draft(template_id) # draft
|
|
110
|
+
|
|
111
|
+
# Render to HTML and plain text; missing values use the placeholder's fallback
|
|
112
|
+
email = client.templates.render(template_id, values={"name": "Ada", "plan": "Pro"})
|
|
113
|
+
preview = client.templates.render(template_id, values={"name": "Ada"}, draft=True)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Variables
|
|
117
|
+
|
|
118
|
+
The variables of a template are the values `render` takes. Each key appears once, in document order.
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
for variable in client.templates.list_variables(template_id): # draft=True for the draft
|
|
122
|
+
print(variable.key, variable.fallback) # fallback is None when nothing renders without a value
|
|
123
|
+
|
|
124
|
+
# Rename a variable of the draft, change its fallback (None for nothing) or both, at every occurrence.
|
|
125
|
+
# Requires a `write` key; published versions are not changed.
|
|
126
|
+
variable = client.templates.update_variable(template_id, "name", new_key="first_name", fallback="there")
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Pagination
|
|
130
|
+
|
|
131
|
+
Lists return a `Page` with `items`, `total` (the size of the whole list) and `next_cursor`, which is
|
|
132
|
+
`None` on the last page.
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
cursor = None
|
|
136
|
+
while True:
|
|
137
|
+
page = client.templates.list(cursor=cursor, limit=100)
|
|
138
|
+
for version in page.items:
|
|
139
|
+
...
|
|
140
|
+
if page.next_cursor is None:
|
|
141
|
+
break
|
|
142
|
+
cursor = page.next_cursor
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Images
|
|
146
|
+
|
|
147
|
+
`upload` starts the upload, sends the file straight to the storage bucket and confirms it. It requires a
|
|
148
|
+
`write` key; accepted types are gif, jpeg, png and webp, up to 5 MiB.
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from pathlib import Path
|
|
152
|
+
|
|
153
|
+
asset = client.assets.upload(Path("logo.png").read_bytes(), content_type="image/png")
|
|
154
|
+
print(asset.url)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
The same three steps can also be run one by one:
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
import httpx
|
|
161
|
+
|
|
162
|
+
content = Path("logo.png").read_bytes()
|
|
163
|
+
|
|
164
|
+
# 1. Start the upload
|
|
165
|
+
upload = client.assets.create_upload(content_type="image/png", size_bytes=len(content))
|
|
166
|
+
|
|
167
|
+
# 2. Send the fields, then the file as the last field, to the bucket before `upload.expires_at`
|
|
168
|
+
response = httpx.post(
|
|
169
|
+
upload.upload.url,
|
|
170
|
+
data=upload.upload.fields,
|
|
171
|
+
files={"file": ("logo.png", content, "image/png")},
|
|
172
|
+
)
|
|
173
|
+
response.raise_for_status()
|
|
174
|
+
|
|
175
|
+
# 3. Confirm it; the image is now listed, with its public URL
|
|
176
|
+
asset = client.assets.confirm(upload.asset_id)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
images = client.assets.list()
|
|
181
|
+
client.assets.delete(asset.id) # Templates that use it will show a broken image
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Async
|
|
185
|
+
|
|
186
|
+
`AsyncTemplr` has the same methods as `Templr`, as coroutines:
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
import asyncio
|
|
190
|
+
|
|
191
|
+
from templr import AsyncTemplr
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
async def main() -> None:
|
|
195
|
+
async with AsyncTemplr(api_key="tmpl_...", base_url="https://templr.example.com") as client:
|
|
196
|
+
email = await client.templates.render(template_id, values={"name": "Ada"})
|
|
197
|
+
print(email.html)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
asyncio.run(main())
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Error handling
|
|
204
|
+
|
|
205
|
+
Every error raised by the SDK inherits from `TemplrError` and has a machine-readable `code`. Errors
|
|
206
|
+
returned by the API are `APIError` subclasses chosen by HTTP status, with the `code`, `message` and
|
|
207
|
+
validation `issues` sent by the API.
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
from templr import APIConnectionError, APIError, NotFoundError, RateLimitError
|
|
211
|
+
|
|
212
|
+
try:
|
|
213
|
+
email = client.templates.render(template_id, values={"name": "Ada"})
|
|
214
|
+
except NotFoundError:
|
|
215
|
+
... # The template does not exist, belongs to another workspace, or was never published
|
|
216
|
+
except RateLimitError as error:
|
|
217
|
+
... # Retry after `error.retry_after` seconds
|
|
218
|
+
except APIError as error:
|
|
219
|
+
print(error.status_code, error.code, error.message, error.issues)
|
|
220
|
+
except APIConnectionError:
|
|
221
|
+
... # The server could not be reached, or the request timed out (`APITimeoutError`)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
| Error | Status | API codes |
|
|
225
|
+
| ------------------------- | ------ | ---------------------------------------------------------------------------------------------- |
|
|
226
|
+
| `BadRequestError` | 400 | `invalid_input`, `validation_error` |
|
|
227
|
+
| `AuthenticationError` | 401 | `unauthorized` |
|
|
228
|
+
| `PermissionDeniedError` | 403 | `forbidden` |
|
|
229
|
+
| `NotFoundError` | 404 | `not_found` |
|
|
230
|
+
| `ConflictError` | 409 | `upload_missing`, `conflict` |
|
|
231
|
+
| `PayloadTooLargeError` | 413 | `payload_too_large` |
|
|
232
|
+
| `RateLimitError` | 429 | `rate_limited` |
|
|
233
|
+
| `InternalServerError` | 5xx | `internal_error`, `database_error`, `render_failed`, `storage_error`, `storage_not_configured` |
|
|
234
|
+
| `UploadError` | bucket | `upload_failed`: the storage bucket rejected the file sent by `assets.upload` |
|
|
235
|
+
| `APIConnectionError` | none | `connection_error` |
|
|
236
|
+
| `APITimeoutError` | none | `timeout` |
|
|
237
|
+
| `UnexpectedResponseError` | 2xx | `unexpected_response`: the server answered with a body this SDK does not understand |
|
|
238
|
+
|
|
239
|
+
An error response without a Templr error body (for example, an HTML page from a proxy) raises the
|
|
240
|
+
class of its status with the code `unknown_error`.
|
|
241
|
+
|
|
242
|
+
## Configuration
|
|
243
|
+
|
|
244
|
+
```python
|
|
245
|
+
import httpx
|
|
246
|
+
|
|
247
|
+
from templr import Templr
|
|
248
|
+
|
|
249
|
+
client = Templr(
|
|
250
|
+
api_key="tmpl_...",
|
|
251
|
+
base_url="https://templr.example.com",
|
|
252
|
+
timeout=10.0, # Seconds per request (default: 30)
|
|
253
|
+
http_client=httpx.Client(proxy="http://proxy.internal:8080"), # Optional: your own httpx client
|
|
254
|
+
)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
The SDK closes the HTTP client it creates when you call `close()` or leave the `with` block. It never
|
|
258
|
+
closes an `http_client` you pass in.
|
|
259
|
+
|
|
260
|
+
## Versioning
|
|
261
|
+
|
|
262
|
+
The SDK follows [Semantic Versioning](https://semver.org/) and is versioned independently of the Templr
|
|
263
|
+
app. Each release is tagged `sdks/python/vX.Y.Z`; see the [changelog](CHANGELOG.md).
|
|
264
|
+
|
|
265
|
+
## Development
|
|
266
|
+
|
|
267
|
+
The SDK lives in `sdks/python` of the Templr repository and uses [uv](https://docs.astral.sh/uv/). The
|
|
268
|
+
virtual environment (`.venv`) and the package cache (`.uv-cache`) stay inside this folder: deleting
|
|
269
|
+
them removes everything the SDK installed.
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
cd sdks/python
|
|
273
|
+
uv sync --locked # Create .venv with the exact versions of uv.lock
|
|
274
|
+
uv run ruff check # Lint
|
|
275
|
+
uv run ruff format --check # Formatting
|
|
276
|
+
uv run mypy # Strict type checking
|
|
277
|
+
uv run pytest # Tests (local only, no network)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
From the repository root, `make sdk-python-check` runs all of them, and `make sdk-python-spec` refreshes
|
|
281
|
+
`tests/openapi.json`, the copy of the API's OpenAPI document that the contract tests check the SDK
|
|
282
|
+
against. See [RELEASING.md](RELEASING.md) to publish a new version.
|
|
283
|
+
|
|
284
|
+
## License
|
|
285
|
+
|
|
286
|
+
[MIT](LICENSE). The Templr app itself is licensed under the PolyForm Noncommercial License 1.0.0 (see the root LICENSE).
|
templr-0.1.0/README.md
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# Templr Python SDK
|
|
2
|
+
|
|
3
|
+
The official Python client for the [Templr](https://github.com/SebastianUrdaneguiBisalaya/templr) REST API.
|
|
4
|
+
Render the email templates your team designs in Templr from any Python backend, with typed models,
|
|
5
|
+
typed errors, and sync and async clients.
|
|
6
|
+
|
|
7
|
+
- Python 3.11+
|
|
8
|
+
- Fully typed (`py.typed`): your editor autocompletes every method and model
|
|
9
|
+
- Sync (`Templr`) and async (`AsyncTemplr`) clients, built on [httpx](https://www.python-httpx.org/)
|
|
10
|
+
- Immutable [Pydantic](https://docs.pydantic.dev/) models for every response
|
|
11
|
+
|
|
12
|
+
Templr does not send emails. Render a template with this SDK, then send the HTML and text with the email
|
|
13
|
+
provider you already use.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install templr
|
|
19
|
+
# or
|
|
20
|
+
uv add templr
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick start
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from templr import Templr
|
|
27
|
+
|
|
28
|
+
with Templr(api_key="tmpl_...", base_url="https://templr.example.com") as client:
|
|
29
|
+
email = client.templates.render(
|
|
30
|
+
"2b0f3a0e-6a1c-4d43-9a57-1f1f6b7d5e01",
|
|
31
|
+
values={"name": "Ada"},
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
print(email.html) # The email HTML
|
|
35
|
+
print(email.text) # The plain text version
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Authentication
|
|
39
|
+
|
|
40
|
+
Every request is authenticated with a workspace API key and scoped to that workspace. Create keys in
|
|
41
|
+
Templr under the workspace settings.
|
|
42
|
+
|
|
43
|
+
- A `read` key can list, get and render templates, and list images.
|
|
44
|
+
- A `write` key can also upload and delete images.
|
|
45
|
+
|
|
46
|
+
Keys have a fixed expiration and are never renewed automatically. Keep them secret: use them only on
|
|
47
|
+
your server, never in a browser or mobile app. Read the key from the environment instead of hard-coding
|
|
48
|
+
it:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import os
|
|
52
|
+
|
|
53
|
+
from templr import Templr
|
|
54
|
+
|
|
55
|
+
client = Templr(api_key=os.environ["TEMPLR_API_KEY"], base_url=os.environ["TEMPLR_BASE_URL"])
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`base_url` is the URL of your Templr instance, since Templr is self-hosted. If Templr is served under a
|
|
59
|
+
path, include it: `https://example.com/templr`.
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### Templates
|
|
64
|
+
|
|
65
|
+
By default the API serves the latest **published** version of each template. The `*_draft` methods and
|
|
66
|
+
`draft=True` serve the draft editors are working on.
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
# Published templates, most recently published first
|
|
70
|
+
page = client.templates.list(limit=50)
|
|
71
|
+
for version in page.items:
|
|
72
|
+
print(version.template_id, version.name, version.version, version.published_at)
|
|
73
|
+
|
|
74
|
+
# Every template with its draft, most recently updated first
|
|
75
|
+
drafts = client.templates.list_drafts()
|
|
76
|
+
|
|
77
|
+
# One template, with its document
|
|
78
|
+
version = client.templates.get(template_id) # latest published version
|
|
79
|
+
draft = client.templates.get_draft(template_id) # draft
|
|
80
|
+
|
|
81
|
+
# Render to HTML and plain text; missing values use the placeholder's fallback
|
|
82
|
+
email = client.templates.render(template_id, values={"name": "Ada", "plan": "Pro"})
|
|
83
|
+
preview = client.templates.render(template_id, values={"name": "Ada"}, draft=True)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Variables
|
|
87
|
+
|
|
88
|
+
The variables of a template are the values `render` takes. Each key appears once, in document order.
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
for variable in client.templates.list_variables(template_id): # draft=True for the draft
|
|
92
|
+
print(variable.key, variable.fallback) # fallback is None when nothing renders without a value
|
|
93
|
+
|
|
94
|
+
# Rename a variable of the draft, change its fallback (None for nothing) or both, at every occurrence.
|
|
95
|
+
# Requires a `write` key; published versions are not changed.
|
|
96
|
+
variable = client.templates.update_variable(template_id, "name", new_key="first_name", fallback="there")
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Pagination
|
|
100
|
+
|
|
101
|
+
Lists return a `Page` with `items`, `total` (the size of the whole list) and `next_cursor`, which is
|
|
102
|
+
`None` on the last page.
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
cursor = None
|
|
106
|
+
while True:
|
|
107
|
+
page = client.templates.list(cursor=cursor, limit=100)
|
|
108
|
+
for version in page.items:
|
|
109
|
+
...
|
|
110
|
+
if page.next_cursor is None:
|
|
111
|
+
break
|
|
112
|
+
cursor = page.next_cursor
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Images
|
|
116
|
+
|
|
117
|
+
`upload` starts the upload, sends the file straight to the storage bucket and confirms it. It requires a
|
|
118
|
+
`write` key; accepted types are gif, jpeg, png and webp, up to 5 MiB.
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from pathlib import Path
|
|
122
|
+
|
|
123
|
+
asset = client.assets.upload(Path("logo.png").read_bytes(), content_type="image/png")
|
|
124
|
+
print(asset.url)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The same three steps can also be run one by one:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
import httpx
|
|
131
|
+
|
|
132
|
+
content = Path("logo.png").read_bytes()
|
|
133
|
+
|
|
134
|
+
# 1. Start the upload
|
|
135
|
+
upload = client.assets.create_upload(content_type="image/png", size_bytes=len(content))
|
|
136
|
+
|
|
137
|
+
# 2. Send the fields, then the file as the last field, to the bucket before `upload.expires_at`
|
|
138
|
+
response = httpx.post(
|
|
139
|
+
upload.upload.url,
|
|
140
|
+
data=upload.upload.fields,
|
|
141
|
+
files={"file": ("logo.png", content, "image/png")},
|
|
142
|
+
)
|
|
143
|
+
response.raise_for_status()
|
|
144
|
+
|
|
145
|
+
# 3. Confirm it; the image is now listed, with its public URL
|
|
146
|
+
asset = client.assets.confirm(upload.asset_id)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
images = client.assets.list()
|
|
151
|
+
client.assets.delete(asset.id) # Templates that use it will show a broken image
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Async
|
|
155
|
+
|
|
156
|
+
`AsyncTemplr` has the same methods as `Templr`, as coroutines:
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
import asyncio
|
|
160
|
+
|
|
161
|
+
from templr import AsyncTemplr
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
async def main() -> None:
|
|
165
|
+
async with AsyncTemplr(api_key="tmpl_...", base_url="https://templr.example.com") as client:
|
|
166
|
+
email = await client.templates.render(template_id, values={"name": "Ada"})
|
|
167
|
+
print(email.html)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
asyncio.run(main())
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Error handling
|
|
174
|
+
|
|
175
|
+
Every error raised by the SDK inherits from `TemplrError` and has a machine-readable `code`. Errors
|
|
176
|
+
returned by the API are `APIError` subclasses chosen by HTTP status, with the `code`, `message` and
|
|
177
|
+
validation `issues` sent by the API.
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
from templr import APIConnectionError, APIError, NotFoundError, RateLimitError
|
|
181
|
+
|
|
182
|
+
try:
|
|
183
|
+
email = client.templates.render(template_id, values={"name": "Ada"})
|
|
184
|
+
except NotFoundError:
|
|
185
|
+
... # The template does not exist, belongs to another workspace, or was never published
|
|
186
|
+
except RateLimitError as error:
|
|
187
|
+
... # Retry after `error.retry_after` seconds
|
|
188
|
+
except APIError as error:
|
|
189
|
+
print(error.status_code, error.code, error.message, error.issues)
|
|
190
|
+
except APIConnectionError:
|
|
191
|
+
... # The server could not be reached, or the request timed out (`APITimeoutError`)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
| Error | Status | API codes |
|
|
195
|
+
| ------------------------- | ------ | ---------------------------------------------------------------------------------------------- |
|
|
196
|
+
| `BadRequestError` | 400 | `invalid_input`, `validation_error` |
|
|
197
|
+
| `AuthenticationError` | 401 | `unauthorized` |
|
|
198
|
+
| `PermissionDeniedError` | 403 | `forbidden` |
|
|
199
|
+
| `NotFoundError` | 404 | `not_found` |
|
|
200
|
+
| `ConflictError` | 409 | `upload_missing`, `conflict` |
|
|
201
|
+
| `PayloadTooLargeError` | 413 | `payload_too_large` |
|
|
202
|
+
| `RateLimitError` | 429 | `rate_limited` |
|
|
203
|
+
| `InternalServerError` | 5xx | `internal_error`, `database_error`, `render_failed`, `storage_error`, `storage_not_configured` |
|
|
204
|
+
| `UploadError` | bucket | `upload_failed`: the storage bucket rejected the file sent by `assets.upload` |
|
|
205
|
+
| `APIConnectionError` | none | `connection_error` |
|
|
206
|
+
| `APITimeoutError` | none | `timeout` |
|
|
207
|
+
| `UnexpectedResponseError` | 2xx | `unexpected_response`: the server answered with a body this SDK does not understand |
|
|
208
|
+
|
|
209
|
+
An error response without a Templr error body (for example, an HTML page from a proxy) raises the
|
|
210
|
+
class of its status with the code `unknown_error`.
|
|
211
|
+
|
|
212
|
+
## Configuration
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
import httpx
|
|
216
|
+
|
|
217
|
+
from templr import Templr
|
|
218
|
+
|
|
219
|
+
client = Templr(
|
|
220
|
+
api_key="tmpl_...",
|
|
221
|
+
base_url="https://templr.example.com",
|
|
222
|
+
timeout=10.0, # Seconds per request (default: 30)
|
|
223
|
+
http_client=httpx.Client(proxy="http://proxy.internal:8080"), # Optional: your own httpx client
|
|
224
|
+
)
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
The SDK closes the HTTP client it creates when you call `close()` or leave the `with` block. It never
|
|
228
|
+
closes an `http_client` you pass in.
|
|
229
|
+
|
|
230
|
+
## Versioning
|
|
231
|
+
|
|
232
|
+
The SDK follows [Semantic Versioning](https://semver.org/) and is versioned independently of the Templr
|
|
233
|
+
app. Each release is tagged `sdks/python/vX.Y.Z`; see the [changelog](CHANGELOG.md).
|
|
234
|
+
|
|
235
|
+
## Development
|
|
236
|
+
|
|
237
|
+
The SDK lives in `sdks/python` of the Templr repository and uses [uv](https://docs.astral.sh/uv/). The
|
|
238
|
+
virtual environment (`.venv`) and the package cache (`.uv-cache`) stay inside this folder: deleting
|
|
239
|
+
them removes everything the SDK installed.
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
cd sdks/python
|
|
243
|
+
uv sync --locked # Create .venv with the exact versions of uv.lock
|
|
244
|
+
uv run ruff check # Lint
|
|
245
|
+
uv run ruff format --check # Formatting
|
|
246
|
+
uv run mypy # Strict type checking
|
|
247
|
+
uv run pytest # Tests (local only, no network)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
From the repository root, `make sdk-python-check` runs all of them, and `make sdk-python-spec` refreshes
|
|
251
|
+
`tests/openapi.json`, the copy of the API's OpenAPI document that the contract tests check the SDK
|
|
252
|
+
against. See [RELEASING.md](RELEASING.md) to publish a new version.
|
|
253
|
+
|
|
254
|
+
## License
|
|
255
|
+
|
|
256
|
+
[MIT](LICENSE). The Templr app itself is licensed under the PolyForm Noncommercial License 1.0.0 (see the root LICENSE).
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "templr"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Official Python SDK for the Templr REST API."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
authors = [{ name = "Sebastian Marat Urdanegui Bisalaya" }]
|
|
10
|
+
keywords = ["templr", "email", "email-templates", "react-email", "sdk", "api-client"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 3 - Alpha",
|
|
13
|
+
"Framework :: Pydantic :: 2",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Topic :: Communications :: Email",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
# Ranges, not exact pins: a library must not force its users onto one exact
|
|
26
|
+
# version. The exact versions used for development live in uv.lock.
|
|
27
|
+
dependencies = ["httpx>=0.28.1,<1", "pydantic>=2.11,<3"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/SebastianUrdaneguiBisalaya/templr"
|
|
31
|
+
Documentation = "https://github.com/SebastianUrdaneguiBisalaya/templr/tree/main/sdks/python#readme"
|
|
32
|
+
Repository = "https://github.com/SebastianUrdaneguiBisalaya/templr/tree/main/sdks/python"
|
|
33
|
+
Issues = "https://github.com/SebastianUrdaneguiBisalaya/templr/issues"
|
|
34
|
+
Changelog = "https://github.com/SebastianUrdaneguiBisalaya/templr/blob/main/sdks/python/CHANGELOG.md"
|
|
35
|
+
|
|
36
|
+
[dependency-groups]
|
|
37
|
+
dev = ["jsonschema==4.26.0", "mypy==2.4.1", "pytest==9.2.0", "respx==0.23.1", "ruff==0.17.6"]
|
|
38
|
+
|
|
39
|
+
[build-system]
|
|
40
|
+
requires = ["hatchling==1.33.0"]
|
|
41
|
+
build-backend = "hatchling.build"
|
|
42
|
+
|
|
43
|
+
[tool.hatch.build.targets.wheel]
|
|
44
|
+
packages = ["src/templr"]
|
|
45
|
+
|
|
46
|
+
[tool.hatch.build.targets.sdist]
|
|
47
|
+
include = ["src", "tests", "README.md", "CHANGELOG.md", "LICENSE"]
|
|
48
|
+
|
|
49
|
+
[tool.uv]
|
|
50
|
+
# Keep the package cache inside this folder: deleting sdks/python removes everything.
|
|
51
|
+
cache-dir = ".uv-cache"
|
|
52
|
+
# Use the Python already installed on the machine; never download interpreters into the user's home.
|
|
53
|
+
python-preference = "only-system"
|
|
54
|
+
# httpx 0.29.0 was released days before this SDK; stay on the proven 0.28.1 for development.
|
|
55
|
+
constraint-dependencies = ["httpx==0.28.1"]
|
|
56
|
+
|
|
57
|
+
[tool.ruff]
|
|
58
|
+
line-length = 120
|
|
59
|
+
target-version = "py311"
|
|
60
|
+
|
|
61
|
+
[tool.ruff.lint]
|
|
62
|
+
select = ["E", "W", "F", "I", "B", "UP", "SIM", "RUF", "N", "D", "ANN", "PT", "RET", "PL"]
|
|
63
|
+
ignore = ["PLR0913"]
|
|
64
|
+
|
|
65
|
+
[tool.ruff.lint.pydocstyle]
|
|
66
|
+
convention = "google"
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint.per-file-ignores]
|
|
69
|
+
"tests/**" = ["D", "PLR2004"]
|
|
70
|
+
|
|
71
|
+
[tool.mypy]
|
|
72
|
+
strict = true
|
|
73
|
+
python_version = "3.11"
|
|
74
|
+
plugins = ["pydantic.mypy"]
|
|
75
|
+
files = ["src", "tests"]
|
|
76
|
+
|
|
77
|
+
[[tool.mypy.overrides]]
|
|
78
|
+
# jsonschema ships no type hints; it is only used by the contract tests.
|
|
79
|
+
module = ["jsonschema", "jsonschema.*"]
|
|
80
|
+
ignore_missing_imports = true
|
|
81
|
+
|
|
82
|
+
[tool.pydantic-mypy]
|
|
83
|
+
init_forbid_extra = true
|
|
84
|
+
init_typed = true
|
|
85
|
+
warn_required_dynamic_aliases = true
|
|
86
|
+
|
|
87
|
+
[tool.pytest.ini_options]
|
|
88
|
+
testpaths = ["tests"]
|
|
89
|
+
addopts = ["--strict-markers", "--strict-config"]
|