mailset 0.7.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.
- mailset/__init__.py +121 -0
- mailset/_base.py +54 -0
- mailset/_client.py +309 -0
- mailset/_convert.py +195 -0
- mailset/_core.py +518 -0
- mailset/_credentials.py +171 -0
- mailset/_errors.py +324 -0
- mailset/_handwritten.py +609 -0
- mailset/_message.py +703 -0
- mailset/_platform.py +94 -0
- mailset/_resources.py +16210 -0
- mailset/_schema.py +118 -0
- mailset/_send.py +262 -0
- mailset/_typing.py +11 -0
- mailset/py.typed +0 -0
- mailset/types.py +9413 -0
- mailset/webhooks.py +366 -0
- mailset-0.7.0.dist-info/METADATA +80 -0
- mailset-0.7.0.dist-info/RECORD +21 -0
- mailset-0.7.0.dist-info/WHEEL +4 -0
- mailset-0.7.0.dist-info/licenses/LICENSE +18 -0
mailset/_errors.py
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"""Mailset's refusals as exceptions: one base class, one subclass per family."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict, List, Mapping, Optional, Tuple, Type, Union
|
|
6
|
+
|
|
7
|
+
from ._typing import TypedDict
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"APIConnectionError",
|
|
11
|
+
"APITimeoutError",
|
|
12
|
+
"AuthenticationError",
|
|
13
|
+
"ConflictError",
|
|
14
|
+
"InvalidRequestError",
|
|
15
|
+
"Issue",
|
|
16
|
+
"MailsetError",
|
|
17
|
+
"NotFoundError",
|
|
18
|
+
"PasskeyPurpose",
|
|
19
|
+
"PasskeyRequiredError",
|
|
20
|
+
"PermissionDeniedError",
|
|
21
|
+
"RateLimitError",
|
|
22
|
+
"ServerError",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
ERROR_DOCS = "https://mailset.app/docs/errors"
|
|
26
|
+
SUPPORT = "mailset.app/support"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Issue(TypedDict):
|
|
30
|
+
"""One wrong field of an ``invalid_input``."""
|
|
31
|
+
|
|
32
|
+
path: List[Union[str, int]]
|
|
33
|
+
"""Which input field: its name, then any keys or positions inside it."""
|
|
34
|
+
|
|
35
|
+
message: str
|
|
36
|
+
"""What is wrong with it."""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class PasskeyPurpose(TypedDict):
|
|
40
|
+
"""What a passkey has to approve, as a ``step_up_required`` names it.
|
|
41
|
+
``mailset.approvals.request`` takes it as it is."""
|
|
42
|
+
|
|
43
|
+
kind: str
|
|
44
|
+
"""What it's about: a ``domain``, a ``mailbox``, a ``member`` and so on."""
|
|
45
|
+
|
|
46
|
+
id: str
|
|
47
|
+
"""The action itself."""
|
|
48
|
+
|
|
49
|
+
org: Optional[str]
|
|
50
|
+
"""The org it's in, or None for a person's own account."""
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class MailsetError(Exception):
|
|
54
|
+
"""Mailset refused, or couldn't be reached. ``code`` is stable and
|
|
55
|
+
``message`` is for people. Each family has its subclass
|
|
56
|
+
(``AuthenticationError``, ``RateLimitError``…), so ``except`` can pick
|
|
57
|
+
one; ``code`` says the same in every SDK."""
|
|
58
|
+
|
|
59
|
+
code: str
|
|
60
|
+
"""Mailset's code: ``invalid_input``, ``unauthorized``, ``forbidden``,
|
|
61
|
+
``not_found``, ``conflict``, ``expired``, ``limit_reached``,
|
|
62
|
+
``step_up_required``, ``unavailable`` or ``internal``."""
|
|
63
|
+
|
|
64
|
+
message: str
|
|
65
|
+
"""Mailset's words, unchanged."""
|
|
66
|
+
|
|
67
|
+
status: Optional[int]
|
|
68
|
+
"""The HTTP status Mailset answered with, or None when it never answered."""
|
|
69
|
+
|
|
70
|
+
refusal: Optional[str]
|
|
71
|
+
"""Why it was refused, when Mailset says. ``recipient``: that address
|
|
72
|
+
can't get mail, so ask the person to check it. ``mail_domain``: a key was
|
|
73
|
+
asked for a domain that holds your team's mailboxes, so make one for a
|
|
74
|
+
mailbox instead. Others: ``domain_pending``, ``domain_blocked``,
|
|
75
|
+
``provider_refused``, and any word a later Mailset adds. An Apple Hide My
|
|
76
|
+
Email address, a temporary inbox or someone who unsubscribed is never a
|
|
77
|
+
refusal: see a receipt's ``skipped``."""
|
|
78
|
+
|
|
79
|
+
retry_at: Optional[int]
|
|
80
|
+
"""On ``limit_reached`` for mail with a ``deliver_by``: the soonest it
|
|
81
|
+
could go, in ms since the epoch. Send it again then, or with a later
|
|
82
|
+
``deliver_by``."""
|
|
83
|
+
|
|
84
|
+
issues: List[Issue]
|
|
85
|
+
"""On ``invalid_input``: each field that was wrong, and what's wrong with it."""
|
|
86
|
+
|
|
87
|
+
purpose: Optional[PasskeyPurpose]
|
|
88
|
+
"""On ``step_up_required``: what a passkey has to approve. See ``approvals``."""
|
|
89
|
+
|
|
90
|
+
command: Optional[str]
|
|
91
|
+
"""The command that was refused, by its id, when it was one."""
|
|
92
|
+
|
|
93
|
+
needs: Optional[str]
|
|
94
|
+
"""When this credential can't run the command: the one that can,
|
|
95
|
+
``MAILSET_TOKEN`` or ``MAILSET_KEY``, with the grant it needs."""
|
|
96
|
+
|
|
97
|
+
docs: str
|
|
98
|
+
"""Where the docs say what this ``code`` means and what to do."""
|
|
99
|
+
|
|
100
|
+
help: str
|
|
101
|
+
"""The docs' link and where to ask, in one line: what a log should keep
|
|
102
|
+
beside ``message``."""
|
|
103
|
+
|
|
104
|
+
def __init__(
|
|
105
|
+
self,
|
|
106
|
+
code: str,
|
|
107
|
+
message: str,
|
|
108
|
+
*,
|
|
109
|
+
status: Optional[int] = None,
|
|
110
|
+
refusal: Optional[str] = None,
|
|
111
|
+
retry_at: Optional[int] = None,
|
|
112
|
+
issues: Optional[List[Issue]] = None,
|
|
113
|
+
purpose: Optional[PasskeyPurpose] = None,
|
|
114
|
+
command: Optional[str] = None,
|
|
115
|
+
needs: Optional[str] = None,
|
|
116
|
+
) -> None:
|
|
117
|
+
super().__init__(message)
|
|
118
|
+
self.code = code
|
|
119
|
+
self.message = message
|
|
120
|
+
self.status = status
|
|
121
|
+
self.refusal = refusal
|
|
122
|
+
self.retry_at = retry_at
|
|
123
|
+
self.issues = list(issues or [])
|
|
124
|
+
self.purpose = purpose
|
|
125
|
+
self.command = command
|
|
126
|
+
self.needs = needs
|
|
127
|
+
self.docs = f"{ERROR_DOCS}#{code}"
|
|
128
|
+
self.help = f"Docs: {self.docs}. Ask us: {SUPPORT}"
|
|
129
|
+
|
|
130
|
+
def __str__(self) -> str:
|
|
131
|
+
return f"{self.message} [{self.code}] {self.help}"
|
|
132
|
+
|
|
133
|
+
def __repr__(self) -> str:
|
|
134
|
+
return (
|
|
135
|
+
f"{type(self).__name__}(code={self.code!r}, status={self.status!r}, "
|
|
136
|
+
f"message={self.message!r})"
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
def __reduce__(self) -> Tuple[Any, Tuple[Any, ...]]:
|
|
140
|
+
return (_rebuilt, (type(self), self.to_dict()))
|
|
141
|
+
|
|
142
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
143
|
+
"""The error as JSON, in the API's camelCase names."""
|
|
144
|
+
return {
|
|
145
|
+
"code": self.code,
|
|
146
|
+
"message": self.message,
|
|
147
|
+
"status": self.status,
|
|
148
|
+
"refusal": self.refusal,
|
|
149
|
+
"retryAt": self.retry_at,
|
|
150
|
+
"issues": [{"path": list(issue["path"]), "message": issue["message"]} for issue in self.issues],
|
|
151
|
+
"purpose": dict(self.purpose) if self.purpose else None,
|
|
152
|
+
"command": self.command,
|
|
153
|
+
"needs": self.needs,
|
|
154
|
+
"docs": self.docs,
|
|
155
|
+
"help": self.help,
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class AuthenticationError(MailsetError):
|
|
160
|
+
"""``unauthorized``: the credential, or a webhook's signature, isn't accepted."""
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class PermissionDeniedError(MailsetError):
|
|
164
|
+
"""``forbidden``: this credential may not do that. ``needs`` says which can."""
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class InvalidRequestError(MailsetError):
|
|
168
|
+
"""``invalid_input``: something given is wrong. ``issues`` says what."""
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class NotFoundError(MailsetError):
|
|
172
|
+
"""``not_found``: there is no such thing, or not one this credential sees."""
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
class ConflictError(MailsetError):
|
|
176
|
+
"""``conflict``: it clashes with how things are now."""
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
class RateLimitError(MailsetError):
|
|
180
|
+
"""``limit_reached``: a limit refused it. ``retry_at`` says when, when it can."""
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class PasskeyRequiredError(MailsetError):
|
|
184
|
+
"""``step_up_required``: someone has to approve it with their passkey.
|
|
185
|
+
``purpose`` is what to approve: see ``approvals``, or pass
|
|
186
|
+
``on_approval`` to the client."""
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class APIConnectionError(MailsetError):
|
|
190
|
+
"""``unavailable`` with no answer: Mailset couldn't be reached, after the retries."""
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
class APITimeoutError(APIConnectionError):
|
|
194
|
+
"""``unavailable`` because the last try took longer than its ``timeout``."""
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
class ServerError(MailsetError):
|
|
198
|
+
"""``internal``, or ``unavailable`` with an answer: Mailset failed, after the retries."""
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
_families: Dict[str, Type[MailsetError]] = {
|
|
202
|
+
"unauthorized": AuthenticationError,
|
|
203
|
+
"forbidden": PermissionDeniedError,
|
|
204
|
+
"invalid_input": InvalidRequestError,
|
|
205
|
+
"not_found": NotFoundError,
|
|
206
|
+
"conflict": ConflictError,
|
|
207
|
+
"limit_reached": RateLimitError,
|
|
208
|
+
"step_up_required": PasskeyRequiredError,
|
|
209
|
+
"internal": ServerError,
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def error_class(code: str, status: Optional[int]) -> Type[MailsetError]:
|
|
214
|
+
"""The subclass for a code: ``unavailable`` is a connection error when
|
|
215
|
+
Mailset never answered, a server error when it did."""
|
|
216
|
+
if code == "unavailable":
|
|
217
|
+
return APIConnectionError if status is None else ServerError
|
|
218
|
+
return _families.get(code, MailsetError)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def error(
|
|
222
|
+
code: str,
|
|
223
|
+
message: str,
|
|
224
|
+
**details: Any,
|
|
225
|
+
) -> MailsetError:
|
|
226
|
+
"""A ``MailsetError`` of the subclass its code and status call for."""
|
|
227
|
+
return error_class(code, details.get("status"))(code, message, **details)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _rebuilt(cls: Type[MailsetError], data: Mapping[str, Any]) -> MailsetError:
|
|
231
|
+
purpose = data.get("purpose")
|
|
232
|
+
return cls(
|
|
233
|
+
data["code"],
|
|
234
|
+
data["message"],
|
|
235
|
+
status=data.get("status"),
|
|
236
|
+
refusal=data.get("refusal"),
|
|
237
|
+
retry_at=data.get("retryAt"),
|
|
238
|
+
issues=[Issue(path=list(issue["path"]), message=issue["message"]) for issue in data.get("issues") or []],
|
|
239
|
+
purpose=PasskeyPurpose(kind=purpose["kind"], id=purpose["id"], org=purpose.get("org"))
|
|
240
|
+
if purpose
|
|
241
|
+
else None,
|
|
242
|
+
command=data.get("command"),
|
|
243
|
+
needs=data.get("needs"),
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def refused(message: str) -> MailsetError:
|
|
248
|
+
"""An ``invalid_input`` the SDK says itself, before any call."""
|
|
249
|
+
return InvalidRequestError("invalid_input", message)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _issues_of(value: Any) -> List[Issue]:
|
|
253
|
+
if not isinstance(value, list):
|
|
254
|
+
return []
|
|
255
|
+
issues = []
|
|
256
|
+
for item in value:
|
|
257
|
+
if (
|
|
258
|
+
isinstance(item, dict)
|
|
259
|
+
and isinstance(item.get("path"), list)
|
|
260
|
+
and isinstance(item.get("message"), str)
|
|
261
|
+
):
|
|
262
|
+
path = [
|
|
263
|
+
part
|
|
264
|
+
for part in item["path"]
|
|
265
|
+
if isinstance(part, (str, int)) and not isinstance(part, bool)
|
|
266
|
+
]
|
|
267
|
+
issues.append(Issue(path=path, message=item["message"]))
|
|
268
|
+
return issues
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def purpose_of(value: Any) -> Optional[PasskeyPurpose]:
|
|
272
|
+
"""A ``purpose`` as Mailset words it, or None when it isn't one."""
|
|
273
|
+
if (
|
|
274
|
+
not isinstance(value, dict)
|
|
275
|
+
or not isinstance(value.get("kind"), str)
|
|
276
|
+
or not isinstance(value.get("id"), str)
|
|
277
|
+
):
|
|
278
|
+
return None
|
|
279
|
+
org = value.get("org")
|
|
280
|
+
return PasskeyPurpose(kind=value["kind"], id=value["id"], org=org if isinstance(org, str) else None)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def error_of(value: Any, **details: Any) -> Optional[MailsetError]:
|
|
284
|
+
"""A refusal as Mailset words it (``{code, message, …}``), or None when it isn't one."""
|
|
285
|
+
if (
|
|
286
|
+
not isinstance(value, dict)
|
|
287
|
+
or not isinstance(value.get("code"), str)
|
|
288
|
+
or not isinstance(value.get("message"), str)
|
|
289
|
+
):
|
|
290
|
+
return None
|
|
291
|
+
refusal = value.get("refusal")
|
|
292
|
+
retry_at = value.get("retryAt")
|
|
293
|
+
return error(
|
|
294
|
+
value["code"],
|
|
295
|
+
value["message"],
|
|
296
|
+
refusal=refusal if isinstance(refusal, str) else None,
|
|
297
|
+
retry_at=retry_at
|
|
298
|
+
if isinstance(retry_at, (int, float)) and not isinstance(retry_at, bool)
|
|
299
|
+
else None,
|
|
300
|
+
issues=_issues_of(value.get("issues")),
|
|
301
|
+
purpose=purpose_of(value.get("purpose")),
|
|
302
|
+
**details,
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def unreachable(
|
|
307
|
+
*, status: Optional[int] = None, command: Optional[str] = None, timed_out: bool = False
|
|
308
|
+
) -> MailsetError:
|
|
309
|
+
"""``unavailable``: no answer, or one that isn't Mailset's."""
|
|
310
|
+
cls: Type[MailsetError] = error_class("unavailable", status)
|
|
311
|
+
if timed_out and status is None:
|
|
312
|
+
cls = APITimeoutError
|
|
313
|
+
return cls(
|
|
314
|
+
"unavailable",
|
|
315
|
+
"The Mailset API can’t be reached.",
|
|
316
|
+
status=status,
|
|
317
|
+
command=command,
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def malformed(*, command: Optional[str] = None) -> MailsetError:
|
|
322
|
+
"""``unavailable``: Mailset answered, but not with what the command
|
|
323
|
+
answers, so the SDK can't read it. A ``ServerError``, as the answer came."""
|
|
324
|
+
return ServerError("unavailable", "The Mailset API can’t be reached.", command=command)
|