ashemail-sdk 1.0.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.
- ashemail_sdk/__init__.py +6 -0
- ashemail_sdk/client.py +615 -0
- ashemail_sdk/generated_types.py +725 -0
- ashemail_sdk-1.0.0.dist-info/METADATA +97 -0
- ashemail_sdk-1.0.0.dist-info/RECORD +8 -0
- ashemail_sdk-1.0.0.dist-info/WHEEL +5 -0
- ashemail_sdk-1.0.0.dist-info/licenses/LICENSE +18 -0
- ashemail_sdk-1.0.0.dist-info/top_level.txt +1 -0
ashemail_sdk/__init__.py
ADDED
ashemail_sdk/client.py
ADDED
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
"""Official Python SDK for the Ashemail Public API v1.
|
|
2
|
+
|
|
3
|
+
Same resource semantics as the TypeScript SDK (@ashemail/sdk): same
|
|
4
|
+
methods, same auth story, same retry and idempotency rules, same error
|
|
5
|
+
shape. Types live in generated_types.py, generated from
|
|
6
|
+
docs/openapi/openapi.json; never hand-edit that file.
|
|
7
|
+
|
|
8
|
+
Auth: one machine-auth story. An API key (Bearer) or a Service Account
|
|
9
|
+
credential (the same form, the same header) authenticates the sending,
|
|
10
|
+
credential message-log, conversion, and usage surfaces. Session-managed
|
|
11
|
+
surfaces (credentials, domains, suppressions, webhooks, Service Accounts,
|
|
12
|
+
organization message logs) need a session token instead; the client raises
|
|
13
|
+
AshemailError(401, "missing_session_token") rather than sending an
|
|
14
|
+
unauthenticated request when one is required and absent.
|
|
15
|
+
|
|
16
|
+
Retries: only safe reads (GET) and idempotent sends (POST /v1/messages
|
|
17
|
+
with an idempotency key) are retried, on 429/502/503/504 and network
|
|
18
|
+
failures, with exponential backoff. Rotation, revocation, and credential
|
|
19
|
+
creation are never auto-retried: a repeat of a rotate mints a new
|
|
20
|
+
secret, so retrying one would burn the secret the caller just received.
|
|
21
|
+
|
|
22
|
+
This is a sync client built on the standard library only (urllib), so it
|
|
23
|
+
has no third-party dependencies. An async client is deliberately not
|
|
24
|
+
shipped: the maintenance budget supports one client, done well.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
import hashlib
|
|
30
|
+
import hmac
|
|
31
|
+
import json
|
|
32
|
+
import re
|
|
33
|
+
import time
|
|
34
|
+
import urllib.error
|
|
35
|
+
import urllib.parse
|
|
36
|
+
import urllib.request
|
|
37
|
+
from dataclasses import dataclass
|
|
38
|
+
from typing import Any, Callable, Generator, Optional
|
|
39
|
+
|
|
40
|
+
from .generated_types import (
|
|
41
|
+
Credential,
|
|
42
|
+
CredentialList,
|
|
43
|
+
DeliverabilityReport,
|
|
44
|
+
Domain,
|
|
45
|
+
DnsRequirement,
|
|
46
|
+
MessageAccepted,
|
|
47
|
+
MessageLogDetail,
|
|
48
|
+
MessageLogPage,
|
|
49
|
+
MessageStatus,
|
|
50
|
+
MessageSubmission,
|
|
51
|
+
RevealedCredential,
|
|
52
|
+
ServiceAccount,
|
|
53
|
+
ServiceAccountCredential,
|
|
54
|
+
Suppression,
|
|
55
|
+
UsageReport,
|
|
56
|
+
WebhookDeadLetter,
|
|
57
|
+
WebhookDelivery,
|
|
58
|
+
WebhookEndpoint,
|
|
59
|
+
WebhookEndpointCreated,
|
|
60
|
+
WebhookSecretRotated,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
__all__ = [
|
|
64
|
+
"AshemailClient",
|
|
65
|
+
"AshemailError",
|
|
66
|
+
"FieldError",
|
|
67
|
+
"verify_webhook_signature",
|
|
68
|
+
"Credential",
|
|
69
|
+
"CredentialList",
|
|
70
|
+
"DeliverabilityReport",
|
|
71
|
+
"Domain",
|
|
72
|
+
"DnsRequirement",
|
|
73
|
+
"MessageAccepted",
|
|
74
|
+
"MessageLogDetail",
|
|
75
|
+
"MessageLogPage",
|
|
76
|
+
"MessageStatus",
|
|
77
|
+
"MessageSubmission",
|
|
78
|
+
"RevealedCredential",
|
|
79
|
+
"ServiceAccount",
|
|
80
|
+
"ServiceAccountCredential",
|
|
81
|
+
"Suppression",
|
|
82
|
+
"UsageReport",
|
|
83
|
+
"WebhookDeadLetter",
|
|
84
|
+
"WebhookDelivery",
|
|
85
|
+
"WebhookEndpoint",
|
|
86
|
+
"WebhookEndpointCreated",
|
|
87
|
+
"WebhookSecretRotated",
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@dataclass
|
|
92
|
+
class FieldError:
|
|
93
|
+
field: str
|
|
94
|
+
message: str
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class AshemailError(Exception):
|
|
98
|
+
"""The one SDK error shape. request_id is the x-correlation-id response
|
|
99
|
+
header, falling back to the body's traceId."""
|
|
100
|
+
|
|
101
|
+
def __init__(
|
|
102
|
+
self,
|
|
103
|
+
status: int,
|
|
104
|
+
code: Optional[str],
|
|
105
|
+
message: str,
|
|
106
|
+
request_id: Optional[str] = None,
|
|
107
|
+
field_errors: Optional[list[FieldError]] = None,
|
|
108
|
+
) -> None:
|
|
109
|
+
super().__init__(message)
|
|
110
|
+
self.status = status
|
|
111
|
+
self.code = code
|
|
112
|
+
self.request_id = request_id
|
|
113
|
+
self.field_errors: list[FieldError] = field_errors or []
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclass
|
|
117
|
+
class _Response:
|
|
118
|
+
status: int
|
|
119
|
+
headers: dict[str, str]
|
|
120
|
+
body: Any
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
# Transport injection for tests: a callable taking (method, url, headers,
|
|
124
|
+
# body_bytes) and returning _Response. The default transport uses urllib.
|
|
125
|
+
Transport = Callable[[str, str, dict[str, str], Optional[bytes]], _Response]
|
|
126
|
+
|
|
127
|
+
_RETRYABLE = {429, 502, 503, 504}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _default_transport(
|
|
131
|
+
method: str, url: str, headers: dict[str, str], body: Optional[bytes], timeout_seconds: float = 30.0
|
|
132
|
+
) -> _Response:
|
|
133
|
+
request = urllib.request.Request(url, data=body, headers=headers, method=method)
|
|
134
|
+
try:
|
|
135
|
+
with urllib.request.urlopen(request, timeout=timeout_seconds) as response:
|
|
136
|
+
raw = response.read()
|
|
137
|
+
lowered = {k.lower(): v for k, v in response.headers.items()}
|
|
138
|
+
parsed = json.loads(raw.decode("utf-8")) if raw else None
|
|
139
|
+
return _Response(response.status, lowered, parsed)
|
|
140
|
+
except urllib.error.HTTPError as error:
|
|
141
|
+
raw = error.read()
|
|
142
|
+
lowered = {k.lower(): v for k, v in error.headers.items()}
|
|
143
|
+
parsed = json.loads(raw.decode("utf-8")) if raw else None
|
|
144
|
+
return _Response(error.code, lowered, parsed)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _error_from_body(status: int, body: Any, request_id: Optional[str]) -> AshemailError:
|
|
148
|
+
record = body if isinstance(body, dict) else {}
|
|
149
|
+
code = record.get("code") if isinstance(record.get("code"), str) else None
|
|
150
|
+
if request_id is None and isinstance(record.get("traceId"), str):
|
|
151
|
+
request_id = record["traceId"]
|
|
152
|
+
detail = record.get("detail")
|
|
153
|
+
message_value = record.get("message")
|
|
154
|
+
message = (
|
|
155
|
+
detail
|
|
156
|
+
if isinstance(detail, str)
|
|
157
|
+
else message_value
|
|
158
|
+
if isinstance(message_value, str)
|
|
159
|
+
else f"Request failed with status {status}."
|
|
160
|
+
)
|
|
161
|
+
field_errors = []
|
|
162
|
+
if isinstance(record.get("errors"), list):
|
|
163
|
+
for item in record["errors"]:
|
|
164
|
+
if isinstance(item, dict) and isinstance(item.get("field"), str) and isinstance(item.get("message"), str):
|
|
165
|
+
field_errors.append(FieldError(field=item["field"], message=item["message"]))
|
|
166
|
+
return AshemailError(status, code, message, request_id, field_errors)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def verify_webhook_signature(
|
|
170
|
+
*,
|
|
171
|
+
secret: str,
|
|
172
|
+
raw_body: str,
|
|
173
|
+
signature_header: str,
|
|
174
|
+
tolerance_seconds: int = 300,
|
|
175
|
+
now_seconds: Optional[int] = None,
|
|
176
|
+
) -> bool:
|
|
177
|
+
"""Verify an ashemail-signature header. Entries are t=<unix>,v1=<hex>;
|
|
178
|
+
any matching v1 within tolerance accepts. The signed string is
|
|
179
|
+
timestamp + '.' + raw body, HMAC-SHA256, hex."""
|
|
180
|
+
now = now_seconds if now_seconds is not None else int(time.time())
|
|
181
|
+
# Entries are comma-joined, but each entry itself contains a comma
|
|
182
|
+
# (t=<unix>,v1=<hex>), so splitting on commas would break every entry.
|
|
183
|
+
# Extract entries with a pattern instead.
|
|
184
|
+
for timestamp_text, digest in re.findall(r"t=(\d+),v1=([0-9a-fA-F]+)", signature_header):
|
|
185
|
+
try:
|
|
186
|
+
timestamp = int(timestamp_text)
|
|
187
|
+
except ValueError:
|
|
188
|
+
continue
|
|
189
|
+
if abs(now - timestamp) > tolerance_seconds:
|
|
190
|
+
continue
|
|
191
|
+
expected = hmac.new(
|
|
192
|
+
secret.encode("utf-8"),
|
|
193
|
+
f"{timestamp}.{raw_body}".encode("utf-8"),
|
|
194
|
+
hashlib.sha256,
|
|
195
|
+
).hexdigest()
|
|
196
|
+
if hmac.compare_digest(expected, digest.lower()):
|
|
197
|
+
return True
|
|
198
|
+
return False
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class AshemailClient:
|
|
202
|
+
def __init__(
|
|
203
|
+
self,
|
|
204
|
+
*,
|
|
205
|
+
api_key: Optional[str] = None,
|
|
206
|
+
session_token: Optional[str] = None,
|
|
207
|
+
base_url: str = "https://app.ashemail.com",
|
|
208
|
+
timeout_seconds: float = 30.0,
|
|
209
|
+
max_retries: int = 3,
|
|
210
|
+
transport: Optional[Transport] = None,
|
|
211
|
+
) -> None:
|
|
212
|
+
self._api_key = api_key
|
|
213
|
+
self._session_token = session_token
|
|
214
|
+
self._base_url = base_url.rstrip("/")
|
|
215
|
+
self._timeout_seconds = timeout_seconds
|
|
216
|
+
self._max_retries = max_retries
|
|
217
|
+
self._transport = transport or (lambda m, u, h, b: _default_transport(m, u, h, b, self._timeout_seconds))
|
|
218
|
+
|
|
219
|
+
def _require_api_key(self) -> str:
|
|
220
|
+
if not self._api_key:
|
|
221
|
+
raise AshemailError(401, "missing_api_key", "This operation needs an API key or Service Account credential.")
|
|
222
|
+
return self._api_key
|
|
223
|
+
|
|
224
|
+
def _require_session(self) -> str:
|
|
225
|
+
if not self._session_token:
|
|
226
|
+
raise AshemailError(
|
|
227
|
+
401,
|
|
228
|
+
"missing_session_token",
|
|
229
|
+
"This operation needs a session token; management permissions are session permissions, never API key scopes.",
|
|
230
|
+
)
|
|
231
|
+
return self._session_token
|
|
232
|
+
|
|
233
|
+
def _request(
|
|
234
|
+
self,
|
|
235
|
+
method: str,
|
|
236
|
+
path: str,
|
|
237
|
+
*,
|
|
238
|
+
auth: str,
|
|
239
|
+
body: Any = None,
|
|
240
|
+
idempotency_key: Optional[str] = None,
|
|
241
|
+
retryable: Optional[bool] = None,
|
|
242
|
+
query: Optional[dict[str, Any]] = None,
|
|
243
|
+
) -> Any:
|
|
244
|
+
token = (
|
|
245
|
+
self._require_api_key()
|
|
246
|
+
if auth == "api_key"
|
|
247
|
+
else self._require_session()
|
|
248
|
+
if auth == "session"
|
|
249
|
+
else None
|
|
250
|
+
)
|
|
251
|
+
url = self._base_url + path
|
|
252
|
+
if query:
|
|
253
|
+
params = {k: str(v) for k, v in query.items() if v is not None}
|
|
254
|
+
if params:
|
|
255
|
+
url += "?" + urllib.parse.urlencode(params)
|
|
256
|
+
headers: dict[str, str] = {}
|
|
257
|
+
if token:
|
|
258
|
+
headers["Authorization"] = f"Bearer {token}"
|
|
259
|
+
if body is not None:
|
|
260
|
+
headers["Content-Type"] = "application/json"
|
|
261
|
+
if idempotency_key:
|
|
262
|
+
headers["Idempotency-Key"] = idempotency_key
|
|
263
|
+
data = json.dumps(body).encode("utf-8") if body is not None else None
|
|
264
|
+
should_retry = retryable if retryable is not None else method == "GET"
|
|
265
|
+
attempt = 0
|
|
266
|
+
while True:
|
|
267
|
+
attempt += 1
|
|
268
|
+
try:
|
|
269
|
+
response = self._transport(method, url, headers, data)
|
|
270
|
+
except Exception as error: # network failure
|
|
271
|
+
if should_retry and attempt <= self._max_retries:
|
|
272
|
+
time.sleep(0.25 * (2 ** (attempt - 1)))
|
|
273
|
+
continue
|
|
274
|
+
raise AshemailError(0, "network_error", str(error)) from error
|
|
275
|
+
if response.status == 204:
|
|
276
|
+
return None
|
|
277
|
+
request_id = response.headers.get("x-correlation-id")
|
|
278
|
+
if response.status >= 400:
|
|
279
|
+
error = _error_from_body(response.status, response.body, request_id)
|
|
280
|
+
if should_retry and response.status in _RETRYABLE and attempt <= self._max_retries:
|
|
281
|
+
time.sleep(0.25 * (2 ** (attempt - 1)))
|
|
282
|
+
continue
|
|
283
|
+
raise error
|
|
284
|
+
return response.body
|
|
285
|
+
|
|
286
|
+
# ---- Sending and credential message log ----
|
|
287
|
+
|
|
288
|
+
def send_message(self, message: MessageSubmission, *, idempotency_key: Optional[str] = None) -> MessageAccepted:
|
|
289
|
+
key = idempotency_key if idempotency_key is not None else message.get("idempotencyKey")
|
|
290
|
+
body = dict(message)
|
|
291
|
+
if key and not message.get("idempotencyKey"):
|
|
292
|
+
body["idempotencyKey"] = key
|
|
293
|
+
return self._request(
|
|
294
|
+
"POST", "/v1/messages", auth="api_key", body=body,
|
|
295
|
+
idempotency_key=key, retryable=key is not None and key != "",
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
def get_message(self, message_id: str) -> MessageStatus:
|
|
299
|
+
return self._request("GET", f"/v1/messages/{urllib.parse.quote(message_id)}", auth="api_key")
|
|
300
|
+
|
|
301
|
+
def search_messages(self, **params: Any) -> MessageLogPage:
|
|
302
|
+
return self._request("GET", "/v1/messages", auth="api_key", query=params)
|
|
303
|
+
|
|
304
|
+
def search_all_messages(self, **params: Any) -> Generator[Any, None, None]:
|
|
305
|
+
"""Yield every item across all pages of a credential message-log search."""
|
|
306
|
+
cursor: Optional[str] = None
|
|
307
|
+
while True:
|
|
308
|
+
page = self.search_messages(cursor=cursor, **params)
|
|
309
|
+
for item in page["items"]:
|
|
310
|
+
yield item
|
|
311
|
+
cursor = page.get("nextCursor")
|
|
312
|
+
if not cursor:
|
|
313
|
+
return
|
|
314
|
+
|
|
315
|
+
def search_organization_messages(self, organization_id: str, **params: Any) -> MessageLogPage:
|
|
316
|
+
return self._request(
|
|
317
|
+
"GET",
|
|
318
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/messages",
|
|
319
|
+
auth="session",
|
|
320
|
+
query=params,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
def get_organization_message(self, organization_id: str, message_id: str) -> MessageLogDetail:
|
|
324
|
+
return self._request(
|
|
325
|
+
"GET",
|
|
326
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/messages/{urllib.parse.quote(message_id)}",
|
|
327
|
+
auth="session",
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
def get_usage(self, organization_id: Optional[str] = None) -> UsageReport:
|
|
331
|
+
return self._request("GET", "/v1/usage", auth="api_key", query={"organizationId": organization_id})
|
|
332
|
+
|
|
333
|
+
def submit_conversion_event(self, event: dict[str, Any]) -> Any:
|
|
334
|
+
return self._request("POST", "/v1/conversion-events", auth="api_key", body=event)
|
|
335
|
+
|
|
336
|
+
# ---- Session-managed surfaces ----
|
|
337
|
+
|
|
338
|
+
def list_api_keys(self, organization_id: str, limit: Optional[int] = None, offset: Optional[int] = None) -> CredentialList:
|
|
339
|
+
return self._request(
|
|
340
|
+
"GET",
|
|
341
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/api-keys",
|
|
342
|
+
auth="session",
|
|
343
|
+
query={"limit": limit, "offset": offset},
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
def create_api_key(self, organization_id: str, payload: dict[str, Any]) -> RevealedCredential:
|
|
347
|
+
return self._request(
|
|
348
|
+
"POST",
|
|
349
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/api-keys",
|
|
350
|
+
auth="session",
|
|
351
|
+
body=payload,
|
|
352
|
+
retryable=False,
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
def get_api_key(self, organization_id: str, credential_id: str) -> Credential:
|
|
356
|
+
return self._request(
|
|
357
|
+
"GET",
|
|
358
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/api-keys/{urllib.parse.quote(credential_id)}",
|
|
359
|
+
auth="session",
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
def rotate_api_key(self, organization_id: str, credential_id: str) -> RevealedCredential:
|
|
363
|
+
return self._request(
|
|
364
|
+
"POST",
|
|
365
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/api-keys/{urllib.parse.quote(credential_id)}/rotate",
|
|
366
|
+
auth="session",
|
|
367
|
+
retryable=False,
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
def revoke_api_key(self, organization_id: str, credential_id: str) -> None:
|
|
371
|
+
self._request(
|
|
372
|
+
"DELETE",
|
|
373
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/api-keys/{urllib.parse.quote(credential_id)}",
|
|
374
|
+
auth="session",
|
|
375
|
+
retryable=False,
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
def list_smtp_credentials(self, organization_id: str, limit: Optional[int] = None, offset: Optional[int] = None) -> CredentialList:
|
|
379
|
+
return self._request(
|
|
380
|
+
"GET",
|
|
381
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/smtp-credentials",
|
|
382
|
+
auth="session",
|
|
383
|
+
query={"limit": limit, "offset": offset},
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
def create_smtp_credential(self, organization_id: str, payload: dict[str, Any]) -> RevealedCredential:
|
|
387
|
+
return self._request(
|
|
388
|
+
"POST",
|
|
389
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/smtp-credentials",
|
|
390
|
+
auth="session",
|
|
391
|
+
body=payload,
|
|
392
|
+
retryable=False,
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
def list_domains(self, organization_id: str, limit: Optional[int] = None, offset: Optional[int] = None) -> list[Domain]:
|
|
396
|
+
return self._request(
|
|
397
|
+
"GET",
|
|
398
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/domains",
|
|
399
|
+
auth="session",
|
|
400
|
+
query={"limit": limit, "offset": offset},
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
def create_domain(self, organization_id: str, name: str) -> Domain:
|
|
404
|
+
return self._request(
|
|
405
|
+
"POST",
|
|
406
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/domains",
|
|
407
|
+
auth="session",
|
|
408
|
+
body={"name": name},
|
|
409
|
+
retryable=False,
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
def get_domain(self, organization_id: str, domain_id: str) -> Domain:
|
|
413
|
+
return self._request(
|
|
414
|
+
"GET",
|
|
415
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/domains/{urllib.parse.quote(domain_id)}",
|
|
416
|
+
auth="session",
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
def get_domain_dns_requirements(self, organization_id: str, domain_id: str) -> list[DnsRequirement]:
|
|
420
|
+
return self._request(
|
|
421
|
+
"GET",
|
|
422
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/domains/{urllib.parse.quote(domain_id)}/dns-requirements",
|
|
423
|
+
auth="session",
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
def verify_domain(self, organization_id: str, domain_id: str) -> Domain:
|
|
427
|
+
return self._request(
|
|
428
|
+
"POST",
|
|
429
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/domains/{urllib.parse.quote(domain_id)}/verify",
|
|
430
|
+
auth="session",
|
|
431
|
+
retryable=False,
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
def delete_domain(self, organization_id: str, domain_id: str) -> None:
|
|
435
|
+
self._request(
|
|
436
|
+
"DELETE",
|
|
437
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/domains/{urllib.parse.quote(domain_id)}",
|
|
438
|
+
auth="session",
|
|
439
|
+
retryable=False,
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
def list_suppressions(self, organization_id: str, limit: Optional[int] = None, cursor: Optional[str] = None) -> list[Suppression]:
|
|
443
|
+
return self._request(
|
|
444
|
+
"GET",
|
|
445
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/suppressions",
|
|
446
|
+
auth="session",
|
|
447
|
+
query={"limit": limit, "cursor": cursor},
|
|
448
|
+
)
|
|
449
|
+
|
|
450
|
+
def create_suppression(self, organization_id: str, payload: dict[str, Any]) -> Suppression:
|
|
451
|
+
return self._request(
|
|
452
|
+
"POST",
|
|
453
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/suppressions",
|
|
454
|
+
auth="session",
|
|
455
|
+
body=payload,
|
|
456
|
+
retryable=False,
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
def delete_suppression(self, organization_id: str, suppression_id: str) -> None:
|
|
460
|
+
self._request(
|
|
461
|
+
"DELETE",
|
|
462
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/suppressions/{urllib.parse.quote(suppression_id)}",
|
|
463
|
+
auth="session",
|
|
464
|
+
retryable=False,
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
def list_webhook_endpoints(self, organization_id: str, limit: Optional[int] = None, offset: Optional[int] = None) -> list[WebhookEndpoint]:
|
|
468
|
+
return self._request(
|
|
469
|
+
"GET",
|
|
470
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/webhook-endpoints",
|
|
471
|
+
auth="session",
|
|
472
|
+
query={"limit": limit, "offset": offset},
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
def create_webhook_endpoint(self, organization_id: str, payload: dict[str, Any]) -> WebhookEndpointCreated:
|
|
476
|
+
return self._request(
|
|
477
|
+
"POST",
|
|
478
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/webhook-endpoints",
|
|
479
|
+
auth="session",
|
|
480
|
+
body=payload,
|
|
481
|
+
retryable=False,
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
def get_webhook_endpoint(self, organization_id: str, endpoint_id: str) -> WebhookEndpoint:
|
|
485
|
+
return self._request(
|
|
486
|
+
"GET",
|
|
487
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/webhook-endpoints/{urllib.parse.quote(endpoint_id)}",
|
|
488
|
+
auth="session",
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
def delete_webhook_endpoint(self, organization_id: str, endpoint_id: str) -> None:
|
|
492
|
+
self._request(
|
|
493
|
+
"DELETE",
|
|
494
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/webhook-endpoints/{urllib.parse.quote(endpoint_id)}",
|
|
495
|
+
auth="session",
|
|
496
|
+
retryable=False,
|
|
497
|
+
)
|
|
498
|
+
|
|
499
|
+
def rotate_webhook_secret(self, organization_id: str, endpoint_id: str) -> WebhookSecretRotated:
|
|
500
|
+
return self._request(
|
|
501
|
+
"POST",
|
|
502
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/webhook-endpoints/{urllib.parse.quote(endpoint_id)}/rotate-secret",
|
|
503
|
+
auth="session",
|
|
504
|
+
retryable=False,
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
def list_webhook_deliveries(
|
|
508
|
+
self, organization_id: str, endpoint_id: str, limit: Optional[int] = None, cursor: Optional[str] = None, state: Optional[str] = None
|
|
509
|
+
) -> list[WebhookDelivery]:
|
|
510
|
+
return self._request(
|
|
511
|
+
"GET",
|
|
512
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/webhook-endpoints/{urllib.parse.quote(endpoint_id)}/deliveries",
|
|
513
|
+
auth="session",
|
|
514
|
+
query={"limit": limit, "cursor": cursor, "state": state},
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
def list_webhook_dead_letters(self, organization_id: str, limit: Optional[int] = None, offset: Optional[int] = None) -> list[WebhookDeadLetter]:
|
|
518
|
+
return self._request(
|
|
519
|
+
"GET",
|
|
520
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/webhook-dead-letters",
|
|
521
|
+
auth="session",
|
|
522
|
+
query={"limit": limit, "offset": offset},
|
|
523
|
+
)
|
|
524
|
+
|
|
525
|
+
def replay_webhook_dead_letter(self, organization_id: str, dead_letter_id: str) -> Any:
|
|
526
|
+
return self._request(
|
|
527
|
+
"POST",
|
|
528
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/webhook-dead-letters/{urllib.parse.quote(dead_letter_id)}/replay",
|
|
529
|
+
auth="session",
|
|
530
|
+
retryable=False,
|
|
531
|
+
)
|
|
532
|
+
|
|
533
|
+
def get_deliverability(self, organization_id: str, from_date: Optional[str] = None, to_date: Optional[str] = None) -> DeliverabilityReport:
|
|
534
|
+
return self._request(
|
|
535
|
+
"GET",
|
|
536
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/deliverability",
|
|
537
|
+
auth="session",
|
|
538
|
+
query={"from": from_date, "to": to_date},
|
|
539
|
+
)
|
|
540
|
+
|
|
541
|
+
# ---- Service Accounts ----
|
|
542
|
+
|
|
543
|
+
def list_service_accounts(self, organization_id: str) -> list[ServiceAccount]:
|
|
544
|
+
return self._request(
|
|
545
|
+
"GET",
|
|
546
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts",
|
|
547
|
+
auth="session",
|
|
548
|
+
)
|
|
549
|
+
|
|
550
|
+
def create_service_account(self, organization_id: str, payload: dict[str, Any]) -> ServiceAccountCredential:
|
|
551
|
+
return self._request(
|
|
552
|
+
"POST",
|
|
553
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts",
|
|
554
|
+
auth="session",
|
|
555
|
+
body=payload,
|
|
556
|
+
retryable=False,
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
def get_service_account(self, organization_id: str, account_id: str) -> ServiceAccount:
|
|
560
|
+
return self._request(
|
|
561
|
+
"GET",
|
|
562
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts/{urllib.parse.quote(account_id)}",
|
|
563
|
+
auth="session",
|
|
564
|
+
)
|
|
565
|
+
|
|
566
|
+
def issue_service_account_credential(self, organization_id: str, account_id: str, scopes: list[str]) -> ServiceAccountCredential:
|
|
567
|
+
return self._request(
|
|
568
|
+
"POST",
|
|
569
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts/{urllib.parse.quote(account_id)}/credentials",
|
|
570
|
+
auth="session",
|
|
571
|
+
body={"scopes": scopes},
|
|
572
|
+
retryable=False,
|
|
573
|
+
)
|
|
574
|
+
|
|
575
|
+
def rotate_service_account_credential(self, organization_id: str, account_id: str, credential_id: str) -> ServiceAccountCredential:
|
|
576
|
+
return self._request(
|
|
577
|
+
"POST",
|
|
578
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts/{urllib.parse.quote(account_id)}/credentials/{urllib.parse.quote(credential_id)}/rotate",
|
|
579
|
+
auth="session",
|
|
580
|
+
retryable=False,
|
|
581
|
+
)
|
|
582
|
+
|
|
583
|
+
def revoke_service_account_credential(self, organization_id: str, account_id: str, credential_id: str) -> None:
|
|
584
|
+
self._request(
|
|
585
|
+
"DELETE",
|
|
586
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts/{urllib.parse.quote(account_id)}/credentials/{urllib.parse.quote(credential_id)}",
|
|
587
|
+
auth="session",
|
|
588
|
+
retryable=False,
|
|
589
|
+
)
|
|
590
|
+
|
|
591
|
+
def change_service_account_scopes(self, organization_id: str, account_id: str, scopes: list[str]) -> ServiceAccount:
|
|
592
|
+
return self._request(
|
|
593
|
+
"POST",
|
|
594
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts/{urllib.parse.quote(account_id)}/scopes",
|
|
595
|
+
auth="session",
|
|
596
|
+
body={"scopes": scopes},
|
|
597
|
+
retryable=False,
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
def set_service_account_enabled(self, organization_id: str, account_id: str, enabled: bool) -> ServiceAccount:
|
|
601
|
+
action = "enable" if enabled else "disable"
|
|
602
|
+
return self._request(
|
|
603
|
+
"POST",
|
|
604
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts/{urllib.parse.quote(account_id)}/{action}",
|
|
605
|
+
auth="session",
|
|
606
|
+
retryable=False,
|
|
607
|
+
)
|
|
608
|
+
|
|
609
|
+
def revoke_service_account(self, organization_id: str, account_id: str) -> ServiceAccount:
|
|
610
|
+
return self._request(
|
|
611
|
+
"POST",
|
|
612
|
+
f"/v1/organizations/{urllib.parse.quote(organization_id)}/service-accounts/{urllib.parse.quote(account_id)}/revoke",
|
|
613
|
+
auth="session",
|
|
614
|
+
retryable=False,
|
|
615
|
+
)
|