Skip to content

Commit

Permalink
feat: Update contact by email or id
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Jan 19, 2025
1 parent b468d4d commit 6c9f252
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
7 changes: 4 additions & 3 deletions examples/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
raise EnvironmentError("RESEND_API_KEY is missing")

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

create_params: resend.Contacts.CreateParams = {
"audience_id": audience_id,
Expand All @@ -23,9 +24,9 @@

update_params: resend.Contacts.UpdateParams = {
"audience_id": audience_id,
"id": contact["id"],
"id": contact_id,
"unsubscribed": False,
"first_name": "Steve1",
"first_name": "Steve",
}

updated: resend.Contact = resend.Contacts.update(update_params)
Expand Down
9 changes: 7 additions & 2 deletions resend/contacts/_contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class UpdateParams(TypedDict):
"""
The audience id.
"""
id: str
id: NotRequired[str]
"""
The contact id.
"""
Expand Down Expand Up @@ -102,7 +102,12 @@ def update(cls, params: UpdateParams) -> Contact:
Returns:
Contact: The updated contact object
"""
path = f"/audiences/{params['audience_id']}/contacts/{params['id']}"
if params.get("id") is None and params.get("email") is None:
raise ValueError("id or email must be provided")

val = params.get("id") if params.get("id") is not None else params.get("email")

path = f"/audiences/{params['audience_id']}/contacts/{val}"
resp = request.Request[Contact](
path=path, params=cast(Dict[Any, Any], params), verb="patch"
).perform_with_content()
Expand Down
13 changes: 13 additions & 0 deletions tests/contacts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ def test_contacts_update(self) -> None:
contact = resend.Contacts.update(params)
assert contact["id"] == "479e3145-dd38-476b-932c-529ceb705947"

def test_contacts_update_missing_required_params(self) -> None:

params: resend.Contacts.UpdateParams = {
"audience_id": "48c269ed-9873-4d60-bdd9-cd7e6fc0b9b8",
"first_name": "Updated",
"unsubscribed": True,
}

try:
resend.Contacts.update(params)
except ValueError as e:
assert str(e) == "id or email must be provided"

def test_should_update_contacts_raise_exception_when_no_content(self) -> None:
self.set_mock_json(None)
params: resend.Contacts.UpdateParams = {
Expand Down

0 comments on commit 6c9f252

Please sign in to comment.