diff --git a/examples/contacts.py b/examples/contacts.py index 4239d0e..32e1384 100644 --- a/examples/contacts.py +++ b/examples/contacts.py @@ -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, @@ -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) diff --git a/resend/contacts/_contacts.py b/resend/contacts/_contacts.py index 7f89216..381bfa2 100644 --- a/resend/contacts/_contacts.py +++ b/resend/contacts/_contacts.py @@ -51,7 +51,7 @@ class UpdateParams(TypedDict): """ The audience id. """ - id: str + id: NotRequired[str] """ The contact id. """ @@ -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() diff --git a/tests/contacts_test.py b/tests/contacts_test.py index 2100023..b3738e7 100644 --- a/tests/contacts_test.py +++ b/tests/contacts_test.py @@ -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 = {