duta-sdk 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.
- duta_sdk-0.1.0/.gitignore +6 -0
- duta_sdk-0.1.0/LICENSE +21 -0
- duta_sdk-0.1.0/PKG-INFO +78 -0
- duta_sdk-0.1.0/README.md +62 -0
- duta_sdk-0.1.0/pyproject.toml +26 -0
- duta_sdk-0.1.0/src/duta/__init__.py +7 -0
- duta_sdk-0.1.0/src/duta/client.py +108 -0
- duta_sdk-0.1.0/src/duta/errors.py +47 -0
duta_sdk-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Duta
|
|
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.
|
duta_sdk-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: duta-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python SDK for Duta — transactional email for developers.
|
|
5
|
+
Project-URL: Homepage, https://duta.indra.sh
|
|
6
|
+
Project-URL: Repository, https://github.com/Fawwazs17/duta-python
|
|
7
|
+
Author: Duta
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: duta,email,resend-alternative,ses,transactional-email
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# Duta Python SDK
|
|
18
|
+
|
|
19
|
+
Official Python client for [Duta](https://duta.indra.sh). Zero dependencies, standard library only.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install duta-sdk
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quickstart
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from duta import Duta
|
|
31
|
+
|
|
32
|
+
duta = Duta("duta_live_xxx")
|
|
33
|
+
|
|
34
|
+
result = duta.emails.send({
|
|
35
|
+
"from": "hello@yourdomain.com",
|
|
36
|
+
"to": "user@example.com",
|
|
37
|
+
"subject": "Welcome to Duta",
|
|
38
|
+
"html": "<p>Thanks for signing up!</p>",
|
|
39
|
+
})
|
|
40
|
+
print("Sent:", result["id"])
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Get an API key from the [dashboard](https://app.duta.indra.sh). The sender domain must be verified first.
|
|
44
|
+
|
|
45
|
+
## Error handling
|
|
46
|
+
|
|
47
|
+
Methods raise `DutaError` on failure:
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from duta import Duta, DutaError
|
|
51
|
+
|
|
52
|
+
duta = Duta("duta_live_xxx")
|
|
53
|
+
try:
|
|
54
|
+
duta.emails.send({ "from": "...", "to": "...", "subject": "Hi", "text": "Hello" })
|
|
55
|
+
except DutaError as e:
|
|
56
|
+
print(e.status_code, e.name, e.message)
|
|
57
|
+
# e.name: authentication_error | permission_denied | rate_limit_exceeded | ...
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## API
|
|
61
|
+
|
|
62
|
+
### `Duta(api_key, base_url=..., timeout=30.0)`
|
|
63
|
+
|
|
64
|
+
### `duta.emails.send(params)`
|
|
65
|
+
|
|
66
|
+
`params` keys: `from`, `to` (str or list), `subject`, `html`, `text`, `reply_to`, `tags` (dict). Returns a dict with `id` and `status`.
|
|
67
|
+
|
|
68
|
+
### `duta.emails.get(email_id)`
|
|
69
|
+
|
|
70
|
+
Retrieve one email. Requires a full-access API key.
|
|
71
|
+
|
|
72
|
+
### `duta.emails.list(page=1, limit=20)`
|
|
73
|
+
|
|
74
|
+
List emails, newest first. Requires a full-access API key.
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
MIT
|
duta_sdk-0.1.0/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Duta Python SDK
|
|
2
|
+
|
|
3
|
+
Official Python client for [Duta](https://duta.indra.sh). Zero dependencies, standard library only.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install duta-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quickstart
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from duta import Duta
|
|
15
|
+
|
|
16
|
+
duta = Duta("duta_live_xxx")
|
|
17
|
+
|
|
18
|
+
result = duta.emails.send({
|
|
19
|
+
"from": "hello@yourdomain.com",
|
|
20
|
+
"to": "user@example.com",
|
|
21
|
+
"subject": "Welcome to Duta",
|
|
22
|
+
"html": "<p>Thanks for signing up!</p>",
|
|
23
|
+
})
|
|
24
|
+
print("Sent:", result["id"])
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Get an API key from the [dashboard](https://app.duta.indra.sh). The sender domain must be verified first.
|
|
28
|
+
|
|
29
|
+
## Error handling
|
|
30
|
+
|
|
31
|
+
Methods raise `DutaError` on failure:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from duta import Duta, DutaError
|
|
35
|
+
|
|
36
|
+
duta = Duta("duta_live_xxx")
|
|
37
|
+
try:
|
|
38
|
+
duta.emails.send({ "from": "...", "to": "...", "subject": "Hi", "text": "Hello" })
|
|
39
|
+
except DutaError as e:
|
|
40
|
+
print(e.status_code, e.name, e.message)
|
|
41
|
+
# e.name: authentication_error | permission_denied | rate_limit_exceeded | ...
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
### `Duta(api_key, base_url=..., timeout=30.0)`
|
|
47
|
+
|
|
48
|
+
### `duta.emails.send(params)`
|
|
49
|
+
|
|
50
|
+
`params` keys: `from`, `to` (str or list), `subject`, `html`, `text`, `reply_to`, `tags` (dict). Returns a dict with `id` and `status`.
|
|
51
|
+
|
|
52
|
+
### `duta.emails.get(email_id)`
|
|
53
|
+
|
|
54
|
+
Retrieve one email. Requires a full-access API key.
|
|
55
|
+
|
|
56
|
+
### `duta.emails.list(page=1, limit=20)`
|
|
57
|
+
|
|
58
|
+
List emails, newest first. Requires a full-access API key.
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
MIT
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "duta-sdk"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Official Python SDK for Duta — transactional email for developers."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Duta" }]
|
|
13
|
+
keywords = ["duta", "email", "transactional-email", "ses", "resend-alternative"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
dependencies = []
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://duta.indra.sh"
|
|
23
|
+
Repository = "https://github.com/Fawwazs17/duta-python"
|
|
24
|
+
|
|
25
|
+
[tool.hatch.build.targets.wheel]
|
|
26
|
+
packages = ["src/duta"]
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""The Duta client."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import urllib.error
|
|
7
|
+
import urllib.request
|
|
8
|
+
from typing import Any, Dict, List, Optional, Union
|
|
9
|
+
|
|
10
|
+
from .errors import DutaError
|
|
11
|
+
|
|
12
|
+
DEFAULT_BASE_URL = "https://api.duta.indra.sh"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Emails:
|
|
16
|
+
"""The ``emails`` resource: send, retrieve, and list emails."""
|
|
17
|
+
|
|
18
|
+
def __init__(self, client: "Duta") -> None:
|
|
19
|
+
self._client = client
|
|
20
|
+
|
|
21
|
+
def send(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
|
22
|
+
"""Send a transactional email.
|
|
23
|
+
|
|
24
|
+
Example::
|
|
25
|
+
|
|
26
|
+
duta.emails.send({
|
|
27
|
+
"from": "hello@yourdomain.com",
|
|
28
|
+
"to": "user@example.com",
|
|
29
|
+
"subject": "Welcome",
|
|
30
|
+
"html": "<p>Thanks for signing up!</p>",
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
Accepts ``from``, ``to``, ``subject``, ``html``, ``text``, ``reply_to``,
|
|
34
|
+
and ``tags``. Returns a dict with ``id`` and ``status``.
|
|
35
|
+
Raises :class:`DutaError` on failure.
|
|
36
|
+
"""
|
|
37
|
+
body: Dict[str, Any] = {
|
|
38
|
+
"from": params["from"],
|
|
39
|
+
"to": params["to"],
|
|
40
|
+
"subject": params["subject"],
|
|
41
|
+
}
|
|
42
|
+
if params.get("html") is not None:
|
|
43
|
+
body["html"] = params["html"]
|
|
44
|
+
if params.get("text") is not None:
|
|
45
|
+
body["text"] = params["text"]
|
|
46
|
+
if params.get("reply_to") is not None:
|
|
47
|
+
body["replyTo"] = params["reply_to"]
|
|
48
|
+
if params.get("tags") is not None:
|
|
49
|
+
body["tags"] = params["tags"]
|
|
50
|
+
return self._client._request("POST", "/v1/email/send", body)
|
|
51
|
+
|
|
52
|
+
def get(self, email_id: str) -> Dict[str, Any]:
|
|
53
|
+
"""Retrieve a single email by ID. Requires a full-access API key."""
|
|
54
|
+
return self._client._request("GET", f"/v1/email/{email_id}")
|
|
55
|
+
|
|
56
|
+
def list(self, page: int = 1, limit: int = 20) -> Dict[str, Any]:
|
|
57
|
+
"""List emails, most recent first. Requires a full-access API key."""
|
|
58
|
+
return self._client._request("GET", f"/v1/email?page={page}&limit={limit}")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class Duta:
|
|
62
|
+
"""Duta API client.
|
|
63
|
+
|
|
64
|
+
Example::
|
|
65
|
+
|
|
66
|
+
from duta import Duta
|
|
67
|
+
|
|
68
|
+
duta = Duta("duta_live_xxx")
|
|
69
|
+
duta.emails.send({
|
|
70
|
+
"from": "hello@yourdomain.com",
|
|
71
|
+
"to": "user@example.com",
|
|
72
|
+
"subject": "Hello",
|
|
73
|
+
"text": "It works!",
|
|
74
|
+
})
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
def __init__(self, api_key: str, base_url: str = DEFAULT_BASE_URL, timeout: float = 30.0) -> None:
|
|
78
|
+
if not api_key:
|
|
79
|
+
raise ValueError("A Duta API key is required. Create one at https://app.duta.indra.sh.")
|
|
80
|
+
self._api_key = api_key
|
|
81
|
+
self._base_url = base_url.rstrip("/")
|
|
82
|
+
self._timeout = timeout
|
|
83
|
+
self.emails = Emails(self)
|
|
84
|
+
|
|
85
|
+
def _request(self, method: str, path: str, body: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
|
86
|
+
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
87
|
+
req = urllib.request.Request(
|
|
88
|
+
f"{self._base_url}{path}",
|
|
89
|
+
data=data,
|
|
90
|
+
method=method,
|
|
91
|
+
headers={
|
|
92
|
+
"Authorization": f"Bearer {self._api_key}",
|
|
93
|
+
"Content-Type": "application/json",
|
|
94
|
+
},
|
|
95
|
+
)
|
|
96
|
+
try:
|
|
97
|
+
with urllib.request.urlopen(req, timeout=self._timeout) as resp:
|
|
98
|
+
raw = resp.read().decode("utf-8")
|
|
99
|
+
return json.loads(raw) if raw else {}
|
|
100
|
+
except urllib.error.HTTPError as exc:
|
|
101
|
+
raw = exc.read().decode("utf-8")
|
|
102
|
+
try:
|
|
103
|
+
parsed = json.loads(raw) if raw else None
|
|
104
|
+
except json.JSONDecodeError:
|
|
105
|
+
parsed = None
|
|
106
|
+
raise DutaError.from_response(exc.code, parsed) from None
|
|
107
|
+
except urllib.error.URLError as exc:
|
|
108
|
+
raise DutaError("network_error", str(exc.reason), 0) from None
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Error types for the Duta SDK."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, List, Optional
|
|
6
|
+
|
|
7
|
+
_NAME_BY_STATUS = {
|
|
8
|
+
400: "validation_error",
|
|
9
|
+
401: "authentication_error",
|
|
10
|
+
403: "permission_denied",
|
|
11
|
+
404: "not_found",
|
|
12
|
+
422: "unprocessable_entity",
|
|
13
|
+
429: "rate_limit_exceeded",
|
|
14
|
+
500: "internal_server_error",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class DutaError(Exception):
|
|
19
|
+
"""Raised when the Duta API returns an error.
|
|
20
|
+
|
|
21
|
+
Attributes:
|
|
22
|
+
name: Machine-readable error name, e.g. ``rate_limit_exceeded``.
|
|
23
|
+
message: Human-readable description.
|
|
24
|
+
status_code: HTTP status code (0 for network errors).
|
|
25
|
+
blocked: Suppressed recipient addresses, present on some 422 errors.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, name: str, message: str, status_code: int, blocked: Optional[List[str]] = None) -> None:
|
|
29
|
+
super().__init__(message)
|
|
30
|
+
self.name = name
|
|
31
|
+
self.message = message
|
|
32
|
+
self.status_code = status_code
|
|
33
|
+
self.blocked = blocked
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def from_response(cls, status_code: int, body: Any) -> "DutaError":
|
|
37
|
+
"""Normalise either of Duta's error shapes into a DutaError."""
|
|
38
|
+
fallback = _NAME_BY_STATUS.get(status_code, "api_error")
|
|
39
|
+
if isinstance(body, dict):
|
|
40
|
+
blocked = body.get("blocked") if isinstance(body.get("blocked"), list) else None
|
|
41
|
+
# Rate-limit shape: { statusCode, name, message }
|
|
42
|
+
if isinstance(body.get("name"), str) and isinstance(body.get("message"), str):
|
|
43
|
+
return cls(body["name"], body["message"], status_code, blocked)
|
|
44
|
+
# Common shape: { error: str }
|
|
45
|
+
if isinstance(body.get("error"), str):
|
|
46
|
+
return cls(fallback, body["error"], status_code, blocked)
|
|
47
|
+
return cls(fallback, f"Request failed with status {status_code}", status_code)
|