Skip to content

Commit 6364c57

Browse files
committed
feat: get contact by email
1 parent 5956b79 commit 6364c57

3 files changed

Lines changed: 63 additions & 13 deletions

File tree

examples/contacts.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
raise EnvironmentError("RESEND_API_KEY is missing")
88

99
# replace with some audience id
10-
audience_id: str = "78b8d3bc-a55a-45a3-aee6-6ec0a5e13d7e"
11-
contact_id: str = "b5c95127-937b-4872-a765-06636e5f73da"
10+
audience_id: str = "ca4e37c5-a82a-4199-a3b8-bf912a6472aa"
1211

1312
create_params: resend.Contacts.CreateParams = {
1413
"audience_id": audience_id,
@@ -24,7 +23,7 @@
2423

2524
update_params: resend.Contacts.UpdateParams = {
2625
"audience_id": audience_id,
27-
"id": contact_id,
26+
"id": contact["id"],
2827
"unsubscribed": False,
2928
"first_name": "Steve",
3029
}
@@ -33,17 +32,25 @@
3332
print("updated contact !")
3433
print(updated)
3534

36-
cont: resend.Contact = resend.Contacts.get(audience_id=audience_id, id=contact["id"])
37-
print("Retrieved contact")
38-
print(cont)
35+
cont_by_id: resend.Contact = resend.Contacts.get(
36+
email=contact["id"], audience_id=audience_id
37+
)
38+
print("Retrieved contact by ID")
39+
print(cont_by_id)
40+
41+
cont_by_email: resend.Contact = resend.Contacts.get(
42+
email="sw@exmple.com", audience_id=audience_id
43+
)
44+
print("Retrieved contact by Email")
45+
print(cont_by_email)
3946

4047
contacts: resend.Contacts.ListResponse = resend.Contacts.list(audience_id=audience_id)
4148
print("List of contacts")
4249
for contact in contacts["data"]:
4350
print(contact)
4451

4552
# remove by email
46-
rmed = resend.Contacts.remove(audience_id=audience_id, email=cont["email"])
53+
rmed = resend.Contacts.remove(audience_id=audience_id, email=contact["email"])
4754

4855
# remove by id
4956
# rmed: resend.Contact = resend.Contacts.remove(audience_id=audience_id, id=cont["id"])

resend/contacts/_contacts.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, cast
1+
from typing import Any, Dict, List, Optional, cast
22

33
from typing_extensions import NotRequired, TypedDict
44

@@ -132,26 +132,35 @@ def list(cls, audience_id: str) -> ListResponse:
132132
return resp
133133

134134
@classmethod
135-
def get(cls, id: str, audience_id: str) -> Contact:
135+
def get(
136+
cls, audience_id: str, id: Optional[str] = None, email: Optional[str] = None
137+
) -> Contact:
136138
"""
137139
Get a contact.
138140
see more: https://resend.com/docs/api-reference/contacts/get-contact
139141
140142
Args:
141143
id (str): The contact ID
142144
audience_id (str): The audience ID
145+
email (Optional[str]): The contact email
143146
144147
Returns:
145148
Contact: The contact object
146149
"""
147-
path = f"/audiences/{audience_id}/contacts/{id}"
150+
contact = email if id is None else id
151+
if contact is None:
152+
raise ValueError("id or email must be provided")
153+
154+
path = f"/audiences/{audience_id}/contacts/{contact}"
148155
resp = request.Request[Contact](
149156
path=path, params={}, verb="get"
150157
).perform_with_content()
151158
return resp
152159

153160
@classmethod
154-
def remove(cls, audience_id: str, id: str = "", email: str = "") -> Contact:
161+
def remove(
162+
cls, audience_id: str, id: Optional[str] = None, email: Optional[str] = None
163+
) -> Contact:
155164
"""
156165
Remove a contact by ID or by Email
157166
see more: https://resend.com/docs/api-reference/contacts/delete-contact
@@ -164,8 +173,8 @@ def remove(cls, audience_id: str, id: str = "", email: str = "") -> Contact:
164173
Returns:
165174
Contact: The removed contact object
166175
"""
167-
contact = email if id == "" else id
168-
if contact == "":
176+
contact = email if id is None else id
177+
if contact is None:
169178
raise ValueError("id or email must be provided")
170179
path = f"/audiences/{audience_id}/contacts/{contact}"
171180

tests/contacts_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,40 @@ def test_contacts_get(self) -> None:
9898
assert contact["created_at"] == "2023-10-06T23:47:56.678Z"
9999
assert contact["unsubscribed"] is False
100100

101+
def test_contacts_get_by_email(self) -> None:
102+
self.set_mock_json(
103+
{
104+
"object": "contact",
105+
"id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
106+
"email": "steve@woz.com",
107+
"first_name": "Steve",
108+
"last_name": "Wozniak",
109+
"created_at": "2023-10-06T23:47:56.678Z",
110+
"unsubscribed": False,
111+
}
112+
)
113+
114+
contact: resend.Contact = resend.Contacts.get(
115+
email="steve@woz.com",
116+
audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8",
117+
)
118+
assert contact["id"] == "e169aa45-1ecf-4183-9955-b1499d5701d3"
119+
assert contact["email"] == "steve@woz.com"
120+
assert contact["first_name"] == "Steve"
121+
assert contact["last_name"] == "Wozniak"
122+
assert contact["created_at"] == "2023-10-06T23:47:56.678Z"
123+
assert contact["unsubscribed"] is False
124+
125+
def test_contacts_get_raises(self) -> None:
126+
resend.api_key = "re_123"
127+
128+
with self.assertRaises(ValueError) as context:
129+
resend.Contacts.get(
130+
audience_id="48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8",
131+
)
132+
133+
self.assertEqual("id or email must be provided", str(context.exception))
134+
101135
def test_should_get_contacts_raise_exception_when_no_content(self) -> None:
102136
self.set_mock_json(None)
103137
with self.assertRaises(NoContentError):

0 commit comments

Comments
 (0)