stalsend-sdk 0.1.0__py3-none-any.whl

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.
stalsend/__init__.py ADDED
@@ -0,0 +1,74 @@
1
+ """StalSend API client. Keep API keys in server-side environment variables."""
2
+ import json
3
+ import secrets
4
+ import urllib.error
5
+ import urllib.request
6
+ from urllib.parse import urlencode
7
+
8
+
9
+ class StalSendError(Exception):
10
+ def __init__(self, status, code, retry_after=None):
11
+ super().__init__(code)
12
+ self.status, self.code, self.retry_after = status, code, retry_after
13
+
14
+
15
+ def _key():
16
+ return secrets.token_urlsafe(24)
17
+
18
+
19
+ class StalSend:
20
+ def __init__(self, api_key, base_url="https://api.stalsend.com/api/v1", timeout=15):
21
+ if not api_key or not api_key.strip():
22
+ raise ValueError("api_key is required")
23
+ self.api_key = api_key.strip()
24
+ self.base_url = base_url.rstrip("/")
25
+ self.timeout = timeout
26
+
27
+ def send(self, message, idempotency_key=None):
28
+ return self._request("/email/send", "POST", message, {"Idempotency-Key": idempotency_key or _key()})
29
+
30
+ def send_batch(self, messages):
31
+ if not 1 <= len(messages) <= 100:
32
+ raise ValueError("send_batch requires between 1 and 100 messages")
33
+ items = []
34
+ for message in messages:
35
+ item = dict(message)
36
+ item["idempotencyKey"] = item.get("idempotencyKey") or _key()
37
+ items.append(item)
38
+ return self._request("/email/batch", "POST", {"messages": items})
39
+
40
+ def list_email_logs(self, limit=50, cursor=None, status=None, query=None):
41
+ params = {k: v for k, v in {"limit": limit, "cursor": cursor, "status": status, "q": query}.items() if v is not None}
42
+ return self._request("/email/logs?" + urlencode(params), "GET")
43
+
44
+ def get_usage(self):
45
+ return self._request("/usage", "GET")
46
+
47
+ def send_otp(self, email, purpose, context):
48
+ return self._request("/otp/send", "POST", {"email": email, "purpose": purpose, "context": context})
49
+
50
+ def resend_otp(self, challenge_id):
51
+ return self._request("/otp/resend", "POST", {"challengeId": challenge_id})
52
+
53
+ def verify_otp(self, challenge_id, code, purpose, context):
54
+ return self._request("/otp/verify", "POST", {"challengeId": challenge_id, "code": code, "purpose": purpose, "context": context})
55
+
56
+ def _request(self, path, method, body=None, extra_headers=None):
57
+ data = None if body is None else json.dumps(body).encode()
58
+ headers = {"Authorization": "Bearer " + self.api_key, "Accept": "application/json"}
59
+ if data is not None:
60
+ headers["Content-Type"] = "application/json"
61
+ headers.update(extra_headers or {})
62
+ req = urllib.request.Request(self.base_url + path, data=data, headers=headers, method=method)
63
+ try:
64
+ with urllib.request.urlopen(req, timeout=self.timeout) as response:
65
+ return None if response.status == 204 else json.loads(response.read())
66
+ except urllib.error.HTTPError as exc:
67
+ try:
68
+ payload = json.loads(exc.read())
69
+ except Exception:
70
+ payload = {}
71
+ raise StalSendError(exc.code, payload.get("error", "request_failed"), exc.headers.get("Retry-After")) from exc
72
+
73
+
74
+ __all__ = ["StalSend", "StalSendError"]
@@ -0,0 +1,31 @@
1
+ Metadata-Version: 2.4
2
+ Name: stalsend-sdk
3
+ Version: 0.1.0
4
+ Summary: Official Python client for the StalSend email API
5
+ Author: StalSend
6
+ License-Expression: MIT
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # StalSend Python SDK
13
+
14
+ Server-side Python client for StalSend email, batch, logs, usage, and OTP APIs. Requires Python 3.9+. Uses only the Python standard library.
15
+
16
+ ```sh
17
+ pip install ./sdks/python
18
+ ```
19
+
20
+ ```python
21
+ import os
22
+ from stalsend import StalSend
23
+
24
+ client = StalSend(os.environ["STALSEND_API_KEY"])
25
+ result = client.send({"to": "customer@example.com", "subject": "Order update", "text": "Your order shipped."})
26
+ logs = client.list_email_logs(limit=20)
27
+ ```
28
+
29
+ Batch send supports 1–100 messages and assigns a unique idempotency key per message. To retry a single send safely, pass the original `idempotency_key` again with identical content. Never expose API keys in browser or mobile code, source control, screenshots, or logs.
30
+
31
+ The package source is prepared for publication as `stalsend-sdk`; registry publication still requires the StalSend publisher account.
@@ -0,0 +1,6 @@
1
+ stalsend/__init__.py,sha256=eiNlJX38eylF1K-ZDrSwtY45RMnJTu_XEytW5WU2wqo,3147
2
+ stalsend_sdk-0.1.0.dist-info/licenses/LICENSE,sha256=MVyV0FMkS9wMlHB-603NCHpNKKFezW0oWAt8s-pqdr8,1065
3
+ stalsend_sdk-0.1.0.dist-info/METADATA,sha256=XeePetM5EnBnxTWsXmByAZ5P7HPWScjm-uj4mca9X18,1129
4
+ stalsend_sdk-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ stalsend_sdk-0.1.0.dist-info/top_level.txt,sha256=Sa0s8ekWE5UtFjXZu3YIbi8eZEd7zKFniTuuK1kTEbI,9
6
+ stalsend_sdk-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 StalSend
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.
@@ -0,0 +1 @@
1
+ stalsend