smswire 0.2.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.
- smswire/__init__.py +17 -0
- smswire/_client.py +575 -0
- smswire/_http.py +141 -0
- smswire/_requests.py +166 -0
- smswire/_version.py +1 -0
- smswire/errors.py +24 -0
- smswire/py.typed +0 -0
- smswire/types.py +282 -0
- smswire/webhooks.py +61 -0
- smswire-0.2.0.dist-info/METADATA +206 -0
- smswire-0.2.0.dist-info/RECORD +13 -0
- smswire-0.2.0.dist-info/WHEEL +4 -0
- smswire-0.2.0.dist-info/licenses/LICENSE +21 -0
smswire/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""SMSWire: send SMS and one-time codes through your own Android phones."""
|
|
2
|
+
|
|
3
|
+
from ._client import DEFAULT_BASE_URL, AsyncSMSWire, SMSWire
|
|
4
|
+
from ._version import __version__
|
|
5
|
+
from .errors import SignatureError, SMSWireError
|
|
6
|
+
from .webhooks import SIGNATURE_HEADER, verify_webhook
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"SMSWire",
|
|
10
|
+
"AsyncSMSWire",
|
|
11
|
+
"SMSWireError",
|
|
12
|
+
"SignatureError",
|
|
13
|
+
"verify_webhook",
|
|
14
|
+
"SIGNATURE_HEADER",
|
|
15
|
+
"DEFAULT_BASE_URL",
|
|
16
|
+
"__version__",
|
|
17
|
+
]
|
smswire/_client.py
ADDED
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from typing import Any, AsyncIterator, Dict, Iterator, List, Optional, Sequence, cast
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
8
|
+
from . import _requests as r
|
|
9
|
+
from ._http import Request, Settings, send, send_async
|
|
10
|
+
from ._version import __version__
|
|
11
|
+
from .types import (
|
|
12
|
+
AddMembersResult,
|
|
13
|
+
BulkItem,
|
|
14
|
+
BulkResult,
|
|
15
|
+
Campaign,
|
|
16
|
+
CampaignList,
|
|
17
|
+
CampaignPreview,
|
|
18
|
+
CampaignResults,
|
|
19
|
+
CampaignSendResult,
|
|
20
|
+
Group,
|
|
21
|
+
GroupList,
|
|
22
|
+
GroupMember,
|
|
23
|
+
GroupMemberInput,
|
|
24
|
+
GroupMemberPage,
|
|
25
|
+
Message,
|
|
26
|
+
MessagePage,
|
|
27
|
+
MessageStatus,
|
|
28
|
+
MessageType,
|
|
29
|
+
Otp,
|
|
30
|
+
OtpVerification,
|
|
31
|
+
Schedule,
|
|
32
|
+
SchedulePage,
|
|
33
|
+
ScheduleRecipient,
|
|
34
|
+
)
|
|
35
|
+
from .webhooks import verify_webhook
|
|
36
|
+
|
|
37
|
+
DEFAULT_BASE_URL = "https://api.smswire.app"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _settings(api_key: Optional[str], project: Optional[str], base_url: Optional[str], max_retries: int, max_retry_wait: float, timeout: float) -> "tuple[Settings, r.Paths]":
|
|
41
|
+
key = api_key or os.environ.get("SMSWIRE_KEY")
|
|
42
|
+
slug = project or os.environ.get("SMSWIRE_PROJECT")
|
|
43
|
+
if not key:
|
|
44
|
+
raise ValueError("SMSWire: pass api_key or set SMSWIRE_KEY")
|
|
45
|
+
if not slug:
|
|
46
|
+
raise ValueError("SMSWire: pass project or set SMSWIRE_PROJECT")
|
|
47
|
+
settings = Settings(
|
|
48
|
+
api_key=key,
|
|
49
|
+
base_url=base_url or os.environ.get("SMSWIRE_URL") or DEFAULT_BASE_URL,
|
|
50
|
+
max_retries=max_retries,
|
|
51
|
+
max_retry_wait=max_retry_wait,
|
|
52
|
+
timeout=timeout,
|
|
53
|
+
user_agent=f"smswire-python/{__version__}",
|
|
54
|
+
)
|
|
55
|
+
return settings, r.Paths(slug)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class _Webhooks:
|
|
59
|
+
def __init__(self, secret: Optional[str]) -> None:
|
|
60
|
+
self._secret = secret
|
|
61
|
+
|
|
62
|
+
def verify(self, raw_body: "bytes | str", signature: Optional[str], *, tolerance: int = 300) -> Dict[str, Any]:
|
|
63
|
+
"""Checks X-SMSWire-Signature over the raw body and returns the event; raises SignatureError."""
|
|
64
|
+
if not self._secret:
|
|
65
|
+
raise ValueError("SMSWire: pass webhook_secret or set SMSWIRE_WEBHOOK_SECRET")
|
|
66
|
+
return verify_webhook(raw_body, signature, self._secret, tolerance=tolerance)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# ── Sync ─────────────────────────────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class Messages:
|
|
73
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
74
|
+
self._call, self._paths = call, paths
|
|
75
|
+
|
|
76
|
+
def send(
|
|
77
|
+
self,
|
|
78
|
+
to: str,
|
|
79
|
+
body: Optional[str] = None,
|
|
80
|
+
*,
|
|
81
|
+
template: Optional[str] = None,
|
|
82
|
+
language: Optional[str] = None,
|
|
83
|
+
variables: Optional[Dict[str, Any]] = None,
|
|
84
|
+
from_: Optional[str] = None,
|
|
85
|
+
phone: Optional[str] = None,
|
|
86
|
+
sim: Optional[int] = None,
|
|
87
|
+
type: Optional[MessageType] = None,
|
|
88
|
+
tags: Optional[Sequence[str]] = None,
|
|
89
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
90
|
+
send_at: r.When = None,
|
|
91
|
+
idempotency_key: Optional[str] = None,
|
|
92
|
+
) -> Message:
|
|
93
|
+
"""Queues one SMS. ``idempotency_key`` (default: random) makes repeating the call safe."""
|
|
94
|
+
req = r.send_message(self._paths, idempotency_key, to=to, body=body, template=template, language=language, variables=variables, from_=from_, phone=phone, sim=sim, type=type, tags=tags, metadata=metadata, send_at=send_at)
|
|
95
|
+
return cast(Message, self._call(req))
|
|
96
|
+
|
|
97
|
+
def send_bulk(
|
|
98
|
+
self,
|
|
99
|
+
messages: Sequence[BulkItem] = (),
|
|
100
|
+
*,
|
|
101
|
+
group_ids: Optional[Sequence[str]] = None,
|
|
102
|
+
name: Optional[str] = None,
|
|
103
|
+
body: Optional[str] = None,
|
|
104
|
+
template: Optional[str] = None,
|
|
105
|
+
language: Optional[str] = None,
|
|
106
|
+
rate: Optional[int] = None,
|
|
107
|
+
from_: Optional[str] = None,
|
|
108
|
+
phone: Optional[str] = None,
|
|
109
|
+
sim: Optional[int] = None,
|
|
110
|
+
type: Optional[MessageType] = None,
|
|
111
|
+
tags: Optional[Sequence[str]] = None,
|
|
112
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
113
|
+
send_at: r.When = None,
|
|
114
|
+
) -> BulkResult:
|
|
115
|
+
"""Queues a batch (``messages`` and/or the members of ``group_ids``); invalid or opted-out items come back in ``rejected``."""
|
|
116
|
+
req = r.send_bulk(self._paths, messages=messages, group_ids=group_ids, name=name, body=body, template=template, language=language, rate=rate, from_=from_, phone=phone, sim=sim, type=type, tags=tags, metadata=metadata, send_at=send_at)
|
|
117
|
+
return cast(BulkResult, self._call(req))
|
|
118
|
+
|
|
119
|
+
def get(self, id: str) -> Message:
|
|
120
|
+
return cast(Message, self._call(r.get_message(self._paths, id)))
|
|
121
|
+
|
|
122
|
+
def cancel(self, id: str) -> Message:
|
|
123
|
+
"""Stops a message that hasn't reached a phone yet."""
|
|
124
|
+
return cast(Message, self._call(r.cancel_message(self._paths, id)))
|
|
125
|
+
|
|
126
|
+
def list(
|
|
127
|
+
self,
|
|
128
|
+
*,
|
|
129
|
+
status: Optional[MessageStatus] = None,
|
|
130
|
+
type: Optional[MessageType] = None,
|
|
131
|
+
tag: Optional[str] = None,
|
|
132
|
+
to: Optional[str] = None,
|
|
133
|
+
batch_id: Optional[str] = None,
|
|
134
|
+
since: r.When = None,
|
|
135
|
+
until: r.When = None,
|
|
136
|
+
cursor: Optional[str] = None,
|
|
137
|
+
limit: Optional[int] = None,
|
|
138
|
+
) -> MessagePage:
|
|
139
|
+
"""One page, newest first; pass ``next_cursor`` back as ``cursor`` for the next."""
|
|
140
|
+
req = r.list_messages(self._paths, status=status, type=type, tag=tag, to=to, batch_id=batch_id, until=until, cursor=cursor, limit=limit, **{"from": since})
|
|
141
|
+
return cast(MessagePage, self._call(req))
|
|
142
|
+
|
|
143
|
+
def list_all(self, **filters: Any) -> Iterator[Message]:
|
|
144
|
+
"""Every matching message, newest first, fetching pages as you go."""
|
|
145
|
+
cursor: Optional[str] = None
|
|
146
|
+
while True:
|
|
147
|
+
page = self.list(**filters, cursor=cursor)
|
|
148
|
+
yield from page["data"]
|
|
149
|
+
cursor = page.get("next_cursor")
|
|
150
|
+
if not cursor:
|
|
151
|
+
return
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
class OneTimeCodes:
|
|
155
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
156
|
+
self._call, self._paths = call, paths
|
|
157
|
+
|
|
158
|
+
def send(self, to: str, *, length: Optional[int] = None, ttl: Optional[int] = None, body: Optional[str] = None, template: Optional[str] = None, language: Optional[str] = None, from_: Optional[str] = None, phone: Optional[str] = None, sim: Optional[int] = None) -> Otp:
|
|
159
|
+
"""Texts a fresh code to ``to``. Keep the returned ``id`` for verify()."""
|
|
160
|
+
return cast(Otp, self._call(r.send_otp(self._paths, to=to, length=length, ttl=ttl, body=body, template=template, language=language, from_=from_, phone=phone, sim=sim)))
|
|
161
|
+
|
|
162
|
+
def verify(self, id: str, code: str) -> OtpVerification:
|
|
163
|
+
"""``valid`` is False with a ``reason`` when the code is wrong, expired or used."""
|
|
164
|
+
return cast(OtpVerification, self._call(r.verify_otp(self._paths, id, code)))
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class Schedules:
|
|
168
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
169
|
+
self._call, self._paths = call, paths
|
|
170
|
+
|
|
171
|
+
def create(
|
|
172
|
+
self,
|
|
173
|
+
cron: str,
|
|
174
|
+
*,
|
|
175
|
+
name: Optional[str] = None,
|
|
176
|
+
timezone: Optional[str] = None,
|
|
177
|
+
to: Optional[str] = None,
|
|
178
|
+
messages: Optional[List[ScheduleRecipient]] = None,
|
|
179
|
+
group_ids: Optional[List[str]] = None,
|
|
180
|
+
body: Optional[str] = None,
|
|
181
|
+
template: Optional[str] = None,
|
|
182
|
+
language: Optional[str] = None,
|
|
183
|
+
variables: Optional[Dict[str, Any]] = None,
|
|
184
|
+
type: Optional[MessageType] = None,
|
|
185
|
+
tags: Optional[Sequence[str]] = None,
|
|
186
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
187
|
+
from_: Optional[str] = None,
|
|
188
|
+
phone: Optional[str] = None,
|
|
189
|
+
sim: Optional[int] = None,
|
|
190
|
+
rate: Optional[int] = None,
|
|
191
|
+
ends_at: r.When = None,
|
|
192
|
+
max_runs: Optional[int] = None,
|
|
193
|
+
enabled: Optional[bool] = None,
|
|
194
|
+
) -> Schedule:
|
|
195
|
+
"""A message (or batch) sent again and again on a cron schedule."""
|
|
196
|
+
req = r.create_schedule(self._paths, cron=cron, name=name, timezone=timezone, to=to, messages=messages, group_ids=group_ids, body=body, template=template, language=language, variables=variables, type=type, tags=tags, metadata=metadata, from_=from_, phone=phone, sim=sim, rate=rate, ends_at=ends_at, max_runs=max_runs, enabled=enabled)
|
|
197
|
+
return cast(Schedule, self._call(req))
|
|
198
|
+
|
|
199
|
+
def list(self) -> SchedulePage:
|
|
200
|
+
return cast(SchedulePage, self._call(r.list_schedules(self._paths)))
|
|
201
|
+
|
|
202
|
+
def get(self, id: str) -> Schedule:
|
|
203
|
+
"""One schedule, with its recent runs."""
|
|
204
|
+
return cast(Schedule, self._call(r.get_schedule(self._paths, id)))
|
|
205
|
+
|
|
206
|
+
def update(self, id: str, **changes: Any) -> Schedule:
|
|
207
|
+
"""Changes name, cron, timezone, body, template, type, ends_at, max_runs or enabled; None clears."""
|
|
208
|
+
return cast(Schedule, self._call(r.update_schedule(self._paths, id, **changes)))
|
|
209
|
+
|
|
210
|
+
def delete(self, id: str) -> None:
|
|
211
|
+
self._call(r.delete_schedule(self._paths, id))
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
class Groups:
|
|
215
|
+
"""The workspace's contact groups: every project's key sees the same ones."""
|
|
216
|
+
|
|
217
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
218
|
+
self._call, self._paths = call, paths
|
|
219
|
+
|
|
220
|
+
def create(self, name: str, *, keywords: Optional[Sequence[str]] = None, join_reply: Optional[str] = None) -> Group:
|
|
221
|
+
"""With ``keywords``, someone who texts one of them (the whole text) to a workspace phone joins."""
|
|
222
|
+
return cast(Group, self._call(r.create_group(self._paths, name=name, keywords=keywords, join_reply=join_reply)))
|
|
223
|
+
|
|
224
|
+
def list(self) -> GroupList:
|
|
225
|
+
return cast(GroupList, self._call(r.list_groups(self._paths)))
|
|
226
|
+
|
|
227
|
+
def get(self, id: str) -> Group:
|
|
228
|
+
return cast(Group, self._call(r.get_group(self._paths, id)))
|
|
229
|
+
|
|
230
|
+
def update(self, id: str, **changes: Any) -> Group:
|
|
231
|
+
"""Only the fields passed change (name, keywords, join_reply); ``keywords=[]`` turns joining by text off."""
|
|
232
|
+
return cast(Group, self._call(r.update_group(self._paths, id, **changes)))
|
|
233
|
+
|
|
234
|
+
def delete(self, id: str) -> None:
|
|
235
|
+
"""The group goes; its contacts stay."""
|
|
236
|
+
self._call(r.delete_group(self._paths, id))
|
|
237
|
+
|
|
238
|
+
def list_members(self, id: str, *, cursor: Optional[str] = None, limit: Optional[int] = None) -> GroupMemberPage:
|
|
239
|
+
"""One page, alphabetically; pass ``next_cursor`` back as ``cursor`` for the next."""
|
|
240
|
+
return cast(GroupMemberPage, self._call(r.list_group_members(self._paths, id, cursor=cursor, limit=limit)))
|
|
241
|
+
|
|
242
|
+
def list_all_members(self, id: str, *, limit: Optional[int] = None) -> Iterator[GroupMember]:
|
|
243
|
+
"""Every member, fetching pages as you iterate."""
|
|
244
|
+
cursor: Optional[str] = None
|
|
245
|
+
while True:
|
|
246
|
+
page = self.list_members(id, cursor=cursor, limit=limit)
|
|
247
|
+
yield from page["data"]
|
|
248
|
+
cursor = page.get("next_cursor")
|
|
249
|
+
if not cursor:
|
|
250
|
+
return
|
|
251
|
+
|
|
252
|
+
def add_members(self, id: str, members: Sequence[GroupMemberInput]) -> AddMembersResult:
|
|
253
|
+
"""Adds people by number; new numbers become contacts (a given name replaces theirs, fields merge)."""
|
|
254
|
+
return cast(AddMembersResult, self._call(r.add_group_members(self._paths, id, members)))
|
|
255
|
+
|
|
256
|
+
def remove_member(self, id: str, phone: str) -> None:
|
|
257
|
+
"""Takes the number out of the group; the contact stays."""
|
|
258
|
+
self._call(r.remove_group_member(self._paths, id, phone))
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
class Campaigns:
|
|
262
|
+
"""Campaigns: a send with a name, an audience, who to leave out and when.
|
|
263
|
+
|
|
264
|
+
A campaign is made as a draft and nothing goes out until ``send``, so an audience can be built
|
|
265
|
+
up over several calls and checked with ``preview`` first. Each run is an ordinary batch.
|
|
266
|
+
"""
|
|
267
|
+
|
|
268
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
269
|
+
self._call, self._paths = call, paths
|
|
270
|
+
|
|
271
|
+
def create(self, name: str, **fields: Any) -> Campaign:
|
|
272
|
+
"""Makes a draft. Marketing needs ``consent=True``; ``stop_footer`` ends every marketing
|
|
273
|
+
message with your opt-out line; ``cron`` repeats it instead of sending once."""
|
|
274
|
+
return cast(Campaign, self._call(r.create_campaign(self._paths, name=name, **fields)))
|
|
275
|
+
|
|
276
|
+
def list(self) -> CampaignList:
|
|
277
|
+
return cast(CampaignList, self._call(r.list_campaigns(self._paths)))
|
|
278
|
+
|
|
279
|
+
def get(self, id: str) -> Campaign:
|
|
280
|
+
return cast(Campaign, self._call(r.get_campaign(self._paths, id)))
|
|
281
|
+
|
|
282
|
+
def update(self, id: str, **changes: Any) -> Campaign:
|
|
283
|
+
"""Only what's passed changes, and the change reaches the runs still to come."""
|
|
284
|
+
return cast(Campaign, self._call(r.update_campaign(self._paths, id, **changes)))
|
|
285
|
+
|
|
286
|
+
def delete(self, id: str) -> None:
|
|
287
|
+
"""The campaign goes; its runs stay in the message log."""
|
|
288
|
+
self._call(r.delete_campaign(self._paths, id))
|
|
289
|
+
|
|
290
|
+
def preview(self, id: str) -> CampaignPreview:
|
|
291
|
+
"""Who it would reach right now: groups read, duplicates dropped, exclusions applied."""
|
|
292
|
+
return cast(CampaignPreview, self._call(r.preview_campaign(self._paths, id)))
|
|
293
|
+
|
|
294
|
+
def results(self, id: str) -> CampaignResults:
|
|
295
|
+
"""How it did: delivery, SMS parts, replies and the opt-outs it cost."""
|
|
296
|
+
return cast(CampaignResults, self._call(r.campaign_results(self._paths, id)))
|
|
297
|
+
|
|
298
|
+
def send(self, id: str) -> CampaignSendResult:
|
|
299
|
+
"""Starts it: ``queued`` with a batch, or ``scheduled`` when it waits for its time or cron."""
|
|
300
|
+
return cast(CampaignSendResult, self._call(r.send_campaign(self._paths, id)))
|
|
301
|
+
|
|
302
|
+
def control(self, id: str, status: str) -> Campaign:
|
|
303
|
+
""""paused" holds every run still going; "running" lets them carry on."""
|
|
304
|
+
return cast(Campaign, self._call(r.control_campaign(self._paths, id, status)))
|
|
305
|
+
|
|
306
|
+
def cancel(self, id: str) -> Campaign:
|
|
307
|
+
"""Stops it for good: what is still on the server is cancelled and refunded."""
|
|
308
|
+
return cast(Campaign, self._call(r.cancel_campaign(self._paths, id)))
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
class SMSWire:
|
|
312
|
+
"""Client for one project. Settings default to SMSWIRE_KEY, SMSWIRE_PROJECT, SMSWIRE_URL, SMSWIRE_WEBHOOK_SECRET."""
|
|
313
|
+
|
|
314
|
+
def __init__(
|
|
315
|
+
self,
|
|
316
|
+
api_key: Optional[str] = None,
|
|
317
|
+
project: Optional[str] = None,
|
|
318
|
+
*,
|
|
319
|
+
base_url: Optional[str] = None,
|
|
320
|
+
webhook_secret: Optional[str] = None,
|
|
321
|
+
max_retries: int = 2,
|
|
322
|
+
max_retry_wait: float = 60,
|
|
323
|
+
timeout: float = 30.0,
|
|
324
|
+
http_client: Optional[httpx.Client] = None,
|
|
325
|
+
) -> None:
|
|
326
|
+
self._settings, paths = _settings(api_key, project, base_url, max_retries, max_retry_wait, timeout)
|
|
327
|
+
self._own_client = http_client is None
|
|
328
|
+
self._client = http_client or httpx.Client()
|
|
329
|
+
self.messages = Messages(self._send, paths)
|
|
330
|
+
self.otp = OneTimeCodes(self._send, paths)
|
|
331
|
+
self.schedules = Schedules(self._send, paths)
|
|
332
|
+
self.groups = Groups(self._send, paths)
|
|
333
|
+
self.campaigns = Campaigns(self._send, paths)
|
|
334
|
+
self.webhooks = _Webhooks(webhook_secret or os.environ.get("SMSWIRE_WEBHOOK_SECRET"))
|
|
335
|
+
|
|
336
|
+
def _send(self, req: Request) -> Any:
|
|
337
|
+
return send(self._client, self._settings, req)
|
|
338
|
+
|
|
339
|
+
def close(self) -> None:
|
|
340
|
+
if self._own_client:
|
|
341
|
+
self._client.close()
|
|
342
|
+
|
|
343
|
+
def __enter__(self) -> "SMSWire":
|
|
344
|
+
return self
|
|
345
|
+
|
|
346
|
+
def __exit__(self, *_: object) -> None:
|
|
347
|
+
self.close()
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
# ── Async ────────────────────────────────────────────────────────────────────
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
class AsyncMessages:
|
|
354
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
355
|
+
self._call, self._paths = call, paths
|
|
356
|
+
|
|
357
|
+
async def send(
|
|
358
|
+
self,
|
|
359
|
+
to: str,
|
|
360
|
+
body: Optional[str] = None,
|
|
361
|
+
*,
|
|
362
|
+
template: Optional[str] = None,
|
|
363
|
+
language: Optional[str] = None,
|
|
364
|
+
variables: Optional[Dict[str, Any]] = None,
|
|
365
|
+
from_: Optional[str] = None,
|
|
366
|
+
phone: Optional[str] = None,
|
|
367
|
+
sim: Optional[int] = None,
|
|
368
|
+
type: Optional[MessageType] = None,
|
|
369
|
+
tags: Optional[Sequence[str]] = None,
|
|
370
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
371
|
+
send_at: r.When = None,
|
|
372
|
+
idempotency_key: Optional[str] = None,
|
|
373
|
+
) -> Message:
|
|
374
|
+
req = r.send_message(self._paths, idempotency_key, to=to, body=body, template=template, language=language, variables=variables, from_=from_, phone=phone, sim=sim, type=type, tags=tags, metadata=metadata, send_at=send_at)
|
|
375
|
+
return cast(Message, await self._call(req))
|
|
376
|
+
|
|
377
|
+
async def send_bulk(
|
|
378
|
+
self,
|
|
379
|
+
messages: Sequence[BulkItem] = (),
|
|
380
|
+
*,
|
|
381
|
+
group_ids: Optional[Sequence[str]] = None,
|
|
382
|
+
name: Optional[str] = None,
|
|
383
|
+
body: Optional[str] = None,
|
|
384
|
+
template: Optional[str] = None,
|
|
385
|
+
language: Optional[str] = None,
|
|
386
|
+
rate: Optional[int] = None,
|
|
387
|
+
from_: Optional[str] = None,
|
|
388
|
+
phone: Optional[str] = None,
|
|
389
|
+
sim: Optional[int] = None,
|
|
390
|
+
type: Optional[MessageType] = None,
|
|
391
|
+
tags: Optional[Sequence[str]] = None,
|
|
392
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
393
|
+
send_at: r.When = None,
|
|
394
|
+
) -> BulkResult:
|
|
395
|
+
req = r.send_bulk(self._paths, messages=messages, group_ids=group_ids, name=name, body=body, template=template, language=language, rate=rate, from_=from_, phone=phone, sim=sim, type=type, tags=tags, metadata=metadata, send_at=send_at)
|
|
396
|
+
return cast(BulkResult, await self._call(req))
|
|
397
|
+
|
|
398
|
+
async def get(self, id: str) -> Message:
|
|
399
|
+
return cast(Message, await self._call(r.get_message(self._paths, id)))
|
|
400
|
+
|
|
401
|
+
async def cancel(self, id: str) -> Message:
|
|
402
|
+
return cast(Message, await self._call(r.cancel_message(self._paths, id)))
|
|
403
|
+
|
|
404
|
+
async def list(
|
|
405
|
+
self,
|
|
406
|
+
*,
|
|
407
|
+
status: Optional[MessageStatus] = None,
|
|
408
|
+
type: Optional[MessageType] = None,
|
|
409
|
+
tag: Optional[str] = None,
|
|
410
|
+
to: Optional[str] = None,
|
|
411
|
+
batch_id: Optional[str] = None,
|
|
412
|
+
since: r.When = None,
|
|
413
|
+
until: r.When = None,
|
|
414
|
+
cursor: Optional[str] = None,
|
|
415
|
+
limit: Optional[int] = None,
|
|
416
|
+
) -> MessagePage:
|
|
417
|
+
req = r.list_messages(self._paths, status=status, type=type, tag=tag, to=to, batch_id=batch_id, until=until, cursor=cursor, limit=limit, **{"from": since})
|
|
418
|
+
return cast(MessagePage, await self._call(req))
|
|
419
|
+
|
|
420
|
+
async def list_all(self, **filters: Any) -> AsyncIterator[Message]:
|
|
421
|
+
cursor: Optional[str] = None
|
|
422
|
+
while True:
|
|
423
|
+
page = await self.list(**filters, cursor=cursor)
|
|
424
|
+
for message in page["data"]:
|
|
425
|
+
yield message
|
|
426
|
+
cursor = page.get("next_cursor")
|
|
427
|
+
if not cursor:
|
|
428
|
+
return
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
class AsyncOneTimeCodes:
|
|
432
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
433
|
+
self._call, self._paths = call, paths
|
|
434
|
+
|
|
435
|
+
async def send(self, to: str, *, length: Optional[int] = None, ttl: Optional[int] = None, body: Optional[str] = None, template: Optional[str] = None, language: Optional[str] = None, from_: Optional[str] = None, phone: Optional[str] = None, sim: Optional[int] = None) -> Otp:
|
|
436
|
+
return cast(Otp, await self._call(r.send_otp(self._paths, to=to, length=length, ttl=ttl, body=body, template=template, language=language, from_=from_, phone=phone, sim=sim)))
|
|
437
|
+
|
|
438
|
+
async def verify(self, id: str, code: str) -> OtpVerification:
|
|
439
|
+
return cast(OtpVerification, await self._call(r.verify_otp(self._paths, id, code)))
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
class AsyncSchedules:
|
|
443
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
444
|
+
self._call, self._paths = call, paths
|
|
445
|
+
|
|
446
|
+
async def create(self, cron: str, **fields: Any) -> Schedule:
|
|
447
|
+
"""Same arguments as SMSWire.schedules.create."""
|
|
448
|
+
return cast(Schedule, await self._call(r.create_schedule(self._paths, cron=cron, **fields)))
|
|
449
|
+
|
|
450
|
+
async def list(self) -> SchedulePage:
|
|
451
|
+
return cast(SchedulePage, await self._call(r.list_schedules(self._paths)))
|
|
452
|
+
|
|
453
|
+
async def get(self, id: str) -> Schedule:
|
|
454
|
+
return cast(Schedule, await self._call(r.get_schedule(self._paths, id)))
|
|
455
|
+
|
|
456
|
+
async def update(self, id: str, **changes: Any) -> Schedule:
|
|
457
|
+
return cast(Schedule, await self._call(r.update_schedule(self._paths, id, **changes)))
|
|
458
|
+
|
|
459
|
+
async def delete(self, id: str) -> None:
|
|
460
|
+
await self._call(r.delete_schedule(self._paths, id))
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
class AsyncGroups:
|
|
464
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
465
|
+
self._call, self._paths = call, paths
|
|
466
|
+
|
|
467
|
+
async def create(self, name: str, *, keywords: Optional[Sequence[str]] = None, join_reply: Optional[str] = None) -> Group:
|
|
468
|
+
"""Same arguments as SMSWire.groups.create."""
|
|
469
|
+
return cast(Group, await self._call(r.create_group(self._paths, name=name, keywords=keywords, join_reply=join_reply)))
|
|
470
|
+
|
|
471
|
+
async def list(self) -> GroupList:
|
|
472
|
+
return cast(GroupList, await self._call(r.list_groups(self._paths)))
|
|
473
|
+
|
|
474
|
+
async def get(self, id: str) -> Group:
|
|
475
|
+
return cast(Group, await self._call(r.get_group(self._paths, id)))
|
|
476
|
+
|
|
477
|
+
async def update(self, id: str, **changes: Any) -> Group:
|
|
478
|
+
return cast(Group, await self._call(r.update_group(self._paths, id, **changes)))
|
|
479
|
+
|
|
480
|
+
async def delete(self, id: str) -> None:
|
|
481
|
+
await self._call(r.delete_group(self._paths, id))
|
|
482
|
+
|
|
483
|
+
async def list_members(self, id: str, *, cursor: Optional[str] = None, limit: Optional[int] = None) -> GroupMemberPage:
|
|
484
|
+
return cast(GroupMemberPage, await self._call(r.list_group_members(self._paths, id, cursor=cursor, limit=limit)))
|
|
485
|
+
|
|
486
|
+
async def list_all_members(self, id: str, *, limit: Optional[int] = None) -> AsyncIterator[GroupMember]:
|
|
487
|
+
cursor: Optional[str] = None
|
|
488
|
+
while True:
|
|
489
|
+
page = await self.list_members(id, cursor=cursor, limit=limit)
|
|
490
|
+
for member in page["data"]:
|
|
491
|
+
yield member
|
|
492
|
+
cursor = page.get("next_cursor")
|
|
493
|
+
if not cursor:
|
|
494
|
+
return
|
|
495
|
+
|
|
496
|
+
async def add_members(self, id: str, members: Sequence[GroupMemberInput]) -> AddMembersResult:
|
|
497
|
+
return cast(AddMembersResult, await self._call(r.add_group_members(self._paths, id, members)))
|
|
498
|
+
|
|
499
|
+
async def remove_member(self, id: str, phone: str) -> None:
|
|
500
|
+
await self._call(r.remove_group_member(self._paths, id, phone))
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
class AsyncCampaigns:
|
|
504
|
+
def __init__(self, call: Any, paths: r.Paths) -> None:
|
|
505
|
+
self._call, self._paths = call, paths
|
|
506
|
+
|
|
507
|
+
async def create(self, name: str, **fields: Any) -> Campaign:
|
|
508
|
+
"""Same arguments as SMSWire.campaigns.create."""
|
|
509
|
+
return cast(Campaign, await self._call(r.create_campaign(self._paths, name=name, **fields)))
|
|
510
|
+
|
|
511
|
+
async def list(self) -> CampaignList:
|
|
512
|
+
return cast(CampaignList, await self._call(r.list_campaigns(self._paths)))
|
|
513
|
+
|
|
514
|
+
async def get(self, id: str) -> Campaign:
|
|
515
|
+
return cast(Campaign, await self._call(r.get_campaign(self._paths, id)))
|
|
516
|
+
|
|
517
|
+
async def update(self, id: str, **changes: Any) -> Campaign:
|
|
518
|
+
return cast(Campaign, await self._call(r.update_campaign(self._paths, id, **changes)))
|
|
519
|
+
|
|
520
|
+
async def delete(self, id: str) -> None:
|
|
521
|
+
await self._call(r.delete_campaign(self._paths, id))
|
|
522
|
+
|
|
523
|
+
async def preview(self, id: str) -> CampaignPreview:
|
|
524
|
+
return cast(CampaignPreview, await self._call(r.preview_campaign(self._paths, id)))
|
|
525
|
+
|
|
526
|
+
async def results(self, id: str) -> CampaignResults:
|
|
527
|
+
return cast(CampaignResults, await self._call(r.campaign_results(self._paths, id)))
|
|
528
|
+
|
|
529
|
+
async def send(self, id: str) -> CampaignSendResult:
|
|
530
|
+
return cast(CampaignSendResult, await self._call(r.send_campaign(self._paths, id)))
|
|
531
|
+
|
|
532
|
+
async def control(self, id: str, status: str) -> Campaign:
|
|
533
|
+
return cast(Campaign, await self._call(r.control_campaign(self._paths, id, status)))
|
|
534
|
+
|
|
535
|
+
async def cancel(self, id: str) -> Campaign:
|
|
536
|
+
return cast(Campaign, await self._call(r.cancel_campaign(self._paths, id)))
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
class AsyncSMSWire:
|
|
540
|
+
"""Async client for one project; same settings as SMSWire."""
|
|
541
|
+
|
|
542
|
+
def __init__(
|
|
543
|
+
self,
|
|
544
|
+
api_key: Optional[str] = None,
|
|
545
|
+
project: Optional[str] = None,
|
|
546
|
+
*,
|
|
547
|
+
base_url: Optional[str] = None,
|
|
548
|
+
webhook_secret: Optional[str] = None,
|
|
549
|
+
max_retries: int = 2,
|
|
550
|
+
max_retry_wait: float = 60,
|
|
551
|
+
timeout: float = 30.0,
|
|
552
|
+
http_client: Optional[httpx.AsyncClient] = None,
|
|
553
|
+
) -> None:
|
|
554
|
+
self._settings, paths = _settings(api_key, project, base_url, max_retries, max_retry_wait, timeout)
|
|
555
|
+
self._own_client = http_client is None
|
|
556
|
+
self._client = http_client or httpx.AsyncClient()
|
|
557
|
+
self.messages = AsyncMessages(self._send, paths)
|
|
558
|
+
self.otp = AsyncOneTimeCodes(self._send, paths)
|
|
559
|
+
self.schedules = AsyncSchedules(self._send, paths)
|
|
560
|
+
self.groups = AsyncGroups(self._send, paths)
|
|
561
|
+
self.campaigns = AsyncCampaigns(self._send, paths)
|
|
562
|
+
self.webhooks = _Webhooks(webhook_secret or os.environ.get("SMSWIRE_WEBHOOK_SECRET"))
|
|
563
|
+
|
|
564
|
+
async def _send(self, req: Request) -> Any:
|
|
565
|
+
return await send_async(self._client, self._settings, req)
|
|
566
|
+
|
|
567
|
+
async def aclose(self) -> None:
|
|
568
|
+
if self._own_client:
|
|
569
|
+
await self._client.aclose()
|
|
570
|
+
|
|
571
|
+
async def __aenter__(self) -> "AsyncSMSWire":
|
|
572
|
+
return self
|
|
573
|
+
|
|
574
|
+
async def __aexit__(self, *_: object) -> None:
|
|
575
|
+
await self.aclose()
|