Skip to content

Commit e741f0a

Browse files
committed
feat: domains module async
1 parent b46e064 commit e741f0a

3 files changed

Lines changed: 404 additions & 0 deletions

File tree

examples/domains_async.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import asyncio
2+
import os
3+
4+
import resend
5+
6+
if not os.environ["RESEND_API_KEY"]:
7+
raise EnvironmentError("RESEND_API_KEY is missing")
8+
9+
# Set up async HTTP client
10+
resend.default_http_client = resend.HTTPXClient()
11+
12+
13+
async def main() -> None:
14+
create_params: resend.Domains.CreateParams = {
15+
"name": "example.com",
16+
"region": "us-east-1",
17+
"custom_return_path": "outbound",
18+
}
19+
domain: resend.Domain = await resend.Domains.create_async(params=create_params)
20+
print(domain)
21+
22+
retrieved: resend.Domain = await resend.Domains.get_async(domain_id=domain["id"])
23+
if retrieved["records"] is not None:
24+
for record in retrieved["records"]:
25+
print(record)
26+
27+
update_params: resend.Domains.UpdateParams = {
28+
"id": domain["id"],
29+
"open_tracking": True,
30+
"click_tracking": True,
31+
"tls": "enforced",
32+
}
33+
34+
updated_domain: resend.Domain = await resend.Domains.update_async(update_params)
35+
print(f"Updated domain: {updated_domain['id']}")
36+
37+
domains: resend.Domains.ListResponse = await resend.Domains.list_async()
38+
if not domains:
39+
print("No domains found")
40+
for domain in domains["data"]:
41+
print(domain)
42+
43+
verified_domain: resend.Domain = await resend.Domains.verify_async(
44+
domain_id=domain["id"]
45+
)
46+
print(f"Verified")
47+
print(verified_domain)
48+
49+
rm_domain: resend.Domain = await resend.Domains.remove_async(domain_id=domain["id"])
50+
print(rm_domain)
51+
52+
53+
if __name__ == "__main__":
54+
asyncio.run(main())

resend/domains/_domains.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
from resend import request
66
from resend.domains._domain import Domain
77

8+
# Async imports (optional - only available with pip install resend[async])
9+
try:
10+
from resend.async_request import AsyncRequest
11+
except ImportError:
12+
pass
13+
814
TlsOptions = Literal["enforced", "opportunistic"]
915

1016

@@ -171,3 +177,108 @@ def verify(cls, domain_id: str) -> Domain:
171177
path=path, params={}, verb="post"
172178
).perform_with_content()
173179
return resp
180+
181+
@classmethod
182+
async def create_async(cls, params: CreateParams) -> Domain:
183+
"""
184+
Create a domain through the Resend Email API (async).
185+
see more: https://resend.com/docs/api-reference/domains/create-domain
186+
187+
Args:
188+
params (CreateParams): The domain creation parameters
189+
190+
Returns:
191+
Domain: The new domain object
192+
"""
193+
path = "/domains"
194+
resp = await AsyncRequest[Domain](
195+
path=path, params=cast(Dict[Any, Any], params), verb="post"
196+
).perform_with_content()
197+
return resp
198+
199+
@classmethod
200+
async def update_async(cls, params: UpdateParams) -> Domain:
201+
"""
202+
Update an existing domain (async).
203+
see more: https://resend.com/docs/api-reference/domains/update-domain
204+
205+
Args:
206+
params (UpdateParams): The domain update parameters
207+
208+
Returns:
209+
Domain: The updated domain object
210+
"""
211+
path = f"/domains/{params['id']}"
212+
resp = await AsyncRequest[Domain](
213+
path=path, params=cast(Dict[Any, Any], params), verb="patch"
214+
).perform_with_content()
215+
return resp
216+
217+
@classmethod
218+
async def get_async(cls, domain_id: str) -> Domain:
219+
"""
220+
Retrieve a single domain for the authenticated user (async).
221+
see more: https://resend.com/docs/api-reference/domains/get-domain
222+
223+
Args:
224+
domain_id (str): The domain ID
225+
226+
Returns:
227+
Domain: The domain object
228+
"""
229+
path = f"/domains/{domain_id}"
230+
resp = await AsyncRequest[Domain](
231+
path=path, params={}, verb="get"
232+
).perform_with_content()
233+
return resp
234+
235+
@classmethod
236+
async def list_async(cls) -> ListResponse:
237+
"""
238+
Retrieve a list of domains for the authenticated user (async).
239+
see more: https://resend.com/docs/api-reference/domains/list-domains
240+
241+
Returns:
242+
ListResponse: A list of domain objects
243+
"""
244+
path = "/domains"
245+
resp = await AsyncRequest[_ListResponse](
246+
path=path, params={}, verb="get"
247+
).perform_with_content()
248+
return resp
249+
250+
@classmethod
251+
async def remove_async(cls, domain_id: str) -> Domain:
252+
"""
253+
Remove an existing domain (async).
254+
see more: https://resend.com/docs/api-reference/domains/delete-domain
255+
256+
Args:
257+
domain_id (str): The domain ID
258+
259+
Returns:
260+
Domain: The removed domain object
261+
"""
262+
path = f"/domains/{domain_id}"
263+
resp = await AsyncRequest[Domain](
264+
path=path, params={}, verb="delete"
265+
).perform_with_content()
266+
return resp
267+
268+
@classmethod
269+
async def verify_async(cls, domain_id: str) -> Domain:
270+
"""
271+
Verify an existing domain (async).
272+
see more: https://resend.com/docs/api-reference/domains/verify-domain
273+
274+
Args:
275+
domain_id (str): The domain ID
276+
277+
Returns:
278+
Domain: The verified domain object
279+
"""
280+
path = f"/domains/{domain_id}/verify"
281+
resp = await AsyncRequest[Domain](
282+
path=path, params={}, verb="post"
283+
).perform_with_content()
284+
return resp

0 commit comments

Comments
 (0)