-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcontacts_async.py
More file actions
75 lines (58 loc) · 1.92 KB
/
contacts_async.py
File metadata and controls
75 lines (58 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import asyncio
import os
import resend
if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")
# Set up async HTTP client
# replace with some audience id
audience_id: str = "ca4e37c5-a82a-4199-a3b8-bf912a6472aa"
async def main() -> None:
create_params: resend.Contacts.CreateParams = {
"audience_id": audience_id,
"email": "sw@exmple.com",
"first_name": "Steve",
"last_name": "Wozniak",
"unsubscribed": False,
}
contact: resend.Contacts.CreateContactResponse = await resend.Contacts.create_async(
create_params
)
print("Created contact !")
print(contact)
update_params: resend.Contacts.UpdateParams = {
"audience_id": audience_id,
"id": contact["id"],
"unsubscribed": False,
"first_name": "Steve (Async)",
}
updated: resend.Contacts.UpdateContactResponse = await resend.Contacts.update_async(
update_params
)
print("updated contact !")
print(updated)
cont_by_id: resend.Contact = await resend.Contacts.get_async(
id=contact["id"], audience_id=audience_id
)
print("Retrieved contact by ID")
print(cont_by_id)
cont_by_email: resend.Contact = await resend.Contacts.get_async(
email="sw@exmple.com", audience_id=audience_id
)
print("Retrieved contact by Email")
print(cont_by_email)
contacts: resend.Contacts.ListResponse = await resend.Contacts.list_async(
audience_id=audience_id
)
print("List of contacts")
for c in contacts["data"]:
print(c)
# remove by email
rmed = await resend.Contacts.remove_async(
audience_id=audience_id, email=cont_by_email["email"]
)
# remove by id
# rmed: resend.Contact = await resend.Contacts.remove_async(audience_id=audience_id, id=cont["id"])
print(f"Removed contact")
print(rmed)
if __name__ == "__main__":
asyncio.run(main())