Skip to content

Commit dfbd13f

Browse files
committed
feat: broadcasts async module
1 parent b626f00 commit dfbd13f

3 files changed

Lines changed: 388 additions & 0 deletions

File tree

examples/broadcasts_async.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import asyncio
2+
import os
3+
from typing import List
4+
5+
import resend
6+
import resend.broadcasts
7+
8+
if not os.environ["RESEND_API_KEY"]:
9+
raise EnvironmentError("RESEND_API_KEY is missing")
10+
11+
# Set up async HTTP client
12+
resend.default_http_client = resend.HTTPXClient()
13+
14+
# replace with some existing audience id
15+
audience_id: str = "78b8d3bc-a55a-45a3-aee6-6ec0a5e13d7e"
16+
17+
18+
async def main() -> None:
19+
create_params: resend.Broadcasts.CreateParams = {
20+
"audience_id": audience_id,
21+
"from": "onboarding@resend.dev",
22+
"subject": "Hello, world! (Async)",
23+
"html": "<p>Hello, world!</p>",
24+
"text": "Hello, world!",
25+
"reply_to": ["foo@resend.dev", "bar@resend.dev"],
26+
"name": "Hello, world! (Async)",
27+
}
28+
29+
broadcast: resend.Broadcasts.CreateResponse = await resend.Broadcasts.create_async(
30+
create_params
31+
)
32+
print("Created broadcast !")
33+
print(broadcast)
34+
35+
update_params: resend.Broadcasts.UpdateParams = {
36+
"broadcast_id": broadcast["id"],
37+
"html": "<p>Hello, world! Updated (Async)</p>",
38+
"text": "Hello, world! Updated (Async)",
39+
"name": "Hello, world! Updated (Async)",
40+
}
41+
42+
updated_broadcast: resend.Broadcasts.UpdateResponse = (
43+
await resend.Broadcasts.update_async(update_params)
44+
)
45+
print("Updated broadcast!")
46+
print(updated_broadcast)
47+
48+
send_params: resend.Broadcasts.SendParams = {
49+
"broadcast_id": broadcast["id"],
50+
}
51+
sent: resend.Broadcasts.SendResponse = await resend.Broadcasts.send_async(
52+
send_params
53+
)
54+
print("Sent broadcast !\n")
55+
print(sent)
56+
57+
retrieved: resend.Broadcast = await resend.Broadcasts.get_async(id=broadcast["id"])
58+
print("retrieved broadcast !\n")
59+
print(retrieved)
60+
61+
if retrieved["status"] == "draft":
62+
removed: resend.Broadcasts.RemoveResponse = (
63+
await resend.Broadcasts.remove_async(id=broadcast["id"])
64+
)
65+
print("Removed broadcast !\n")
66+
print(removed)
67+
print("\n")
68+
else:
69+
print("Broadcast is not in draft status, cannot remove it.\n")
70+
71+
list_response: resend.Broadcasts.ListResponse = await resend.Broadcasts.list_async()
72+
print("List of broadcasts !\n")
73+
print(list_response)
74+
75+
76+
if __name__ == "__main__":
77+
asyncio.run(main())

resend/broadcasts/_broadcasts.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
from ._broadcast import Broadcast
88

9+
# Async imports (optional - only available with pip install resend[async])
10+
try:
11+
from resend.async_request import AsyncRequest
12+
except ImportError:
13+
pass
14+
915
# _CreateParamsFrom is declared with functional TypedDict syntax here because
1016
# "from" is a reserved keyword in Python, and this is the best way to
1117
# support type-checking for it.
@@ -323,3 +329,108 @@ def remove(cls, id: str) -> RemoveResponse:
323329
path=path, params={}, verb="delete"
324330
).perform_with_content()
325331
return resp
332+
333+
@classmethod
334+
async def create_async(cls, params: CreateParams) -> CreateResponse:
335+
"""
336+
Create a broadcast (async).
337+
see more: https://resend.com/docs/api-reference/broadcasts/create-broadcast
338+
339+
Args:
340+
params (CreateParams): The broadcast creation parameters
341+
342+
Returns:
343+
CreateResponse: The new broadcast object response
344+
"""
345+
path = "/broadcasts"
346+
resp = await AsyncRequest[_CreateResponse](
347+
path=path, params=cast(Dict[Any, Any], params), verb="post"
348+
).perform_with_content()
349+
return resp
350+
351+
@classmethod
352+
async def update_async(cls, params: UpdateParams) -> UpdateResponse:
353+
"""
354+
Update a broadcast (async).
355+
see more: https://resend.com/docs/api-reference/broadcasts/update-broadcast
356+
357+
Args:
358+
params (UpdateParams): The broadcast update parameters
359+
360+
Returns:
361+
UpdateResponse: The updated broadcast object response
362+
"""
363+
path = f"/broadcasts/{params['broadcast_id']}"
364+
resp = await AsyncRequest[_UpdateResponse](
365+
path=path, params=cast(Dict[Any, Any], params), verb="patch"
366+
).perform_with_content()
367+
return resp
368+
369+
@classmethod
370+
async def send_async(cls, params: SendParams) -> SendResponse:
371+
"""
372+
Sends a broadcast (async).
373+
see more: https://resend.com/docs/api-reference/broadcasts/send-broadcast
374+
375+
Args:
376+
params (SendParams): The broadcast send parameters
377+
378+
Returns:
379+
SendResponse: The new broadcast object response
380+
"""
381+
path = f"/broadcasts/{params['broadcast_id']}/send"
382+
resp = await AsyncRequest[_SendResponse](
383+
path=path, params=cast(Dict[Any, Any], params), verb="post"
384+
).perform_with_content()
385+
return resp
386+
387+
@classmethod
388+
async def list_async(cls) -> ListResponse:
389+
"""
390+
Retrieve a list of broadcasts (async).
391+
see more: https://resend.com/docs/api-reference/broadcasts/list-broadcasts
392+
393+
Returns:
394+
ListResponse: A list of broadcast objects
395+
"""
396+
path = "/broadcasts/"
397+
resp = await AsyncRequest[_ListResponse](
398+
path=path, params={}, verb="get"
399+
).perform_with_content()
400+
return resp
401+
402+
@classmethod
403+
async def get_async(cls, id: str) -> Broadcast:
404+
"""
405+
Retrieve a single broadcast (async).
406+
see more: https://resend.com/docs/api-reference/broadcasts/get-broadcast
407+
408+
Args:
409+
id (str): The broadcast ID
410+
411+
Returns:
412+
Broadcast: The broadcast object
413+
"""
414+
path = f"/broadcasts/{id}"
415+
resp = await AsyncRequest[Broadcast](
416+
path=path, params={}, verb="get"
417+
).perform_with_content()
418+
return resp
419+
420+
@classmethod
421+
async def remove_async(cls, id: str) -> RemoveResponse:
422+
"""
423+
Delete a single broadcast (async).
424+
see more: https://resend.com/docs/api-reference/broadcasts/delete-broadcasts
425+
426+
Args:
427+
id (str): The broadcast ID
428+
429+
Returns:
430+
RemoveResponse: The remove response object
431+
"""
432+
path = f"/broadcasts/{id}"
433+
resp = await AsyncRequest[_RemoveResponse](
434+
path=path, params={}, verb="delete"
435+
).perform_with_content()
436+
return resp

0 commit comments

Comments
 (0)