diff --git a/examples/scheduled_email.py b/examples/scheduled_email.py index 84c6bda..70eb240 100644 --- a/examples/scheduled_email.py +++ b/examples/scheduled_email.py @@ -17,11 +17,21 @@ # when scheduled_at is not in ISO 8601 format email: resend.Email = resend.Emails.send(params) -print(f"Email scheduled") -print("Email ID: ", email["id"]) +print(f"Email scheduled: {email['id']}") + +update_params: resend.Emails.UpdateParams = { + "id": email["id"], + "scheduled_at": "2024-09-07T11:52:01.858Z", +} + +updated_email: resend.Emails.UpdateEmailResponse = resend.Emails.update( + params=update_params +) + +print(f"Email updated: {updated_email['id']}") cancel_resp: resend.Emails.CancelScheduledEmailResponse = resend.Emails.cancel( email_id=email["id"] ) -print(f"Canceled email: {cancel_resp['id']}") +print(f"Email cancelled: {cancel_resp['id']}") diff --git a/resend/emails/_emails.py b/resend/emails/_emails.py index 688c106..9524758 100644 --- a/resend/emails/_emails.py +++ b/resend/emails/_emails.py @@ -8,6 +8,29 @@ from resend.emails._tag import Tag +class _UpdateParams(TypedDict): + id: str + """ + The ID of the email to update. + """ + scheduled_at: NotRequired[str] + """ + Schedule email to be sent later. + The date should be in ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z). + """ + + +class _UpdateEmailResponse(TypedDict): + object: str + """ + The object type: email + """ + id: str + """ + The ID of the scheduled email that was cancelled. + """ + + class _CancelScheduledEmailResponse(TypedDict): object: str id: str @@ -86,6 +109,25 @@ class CancelScheduledEmailResponse(_CancelScheduledEmailResponse): id (str): The ID of the scheduled email that was cancelled """ + class UpdateEmailResponse(_UpdateEmailResponse): + """ + UpdateEmailResponse is the type for the updated email response. + + Attributes: + object (str): The object type + id (str): The ID of the updated email. + """ + + class UpdateParams(_UpdateParams): + """ + UpdateParams is the class that wraps the parameters for the update method. + + Attributes: + id (str): The ID of the email to update. + scheduled_at (NotRequired[str]): Schedule email to be sent later. \ + The date should be in ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z). + """ + class SendParams(_SendParamsDefault): """SendParams is the class that wraps the parameters for the send method. @@ -162,3 +204,23 @@ def cancel(cls, email_id: str) -> CancelScheduledEmailResponse: verb="post", ).perform_with_content() return resp + + @classmethod + def update(cls, params: UpdateParams) -> UpdateEmailResponse: + """ + Update an email. + see more: https://resend.com/docs/api-reference/emails/update-email + + Args: + params (UpdateParams): The email parameters to update + + Returns: + Email: The email object that was updated + """ + path = f"/emails/{params['id']}" + resp = request.Request[_UpdateEmailResponse]( + path=path, + params=cast(Dict[Any, Any], params), + verb="patch", + ).perform_with_content() + return resp diff --git a/tests/emails_test.py b/tests/emails_test.py index f79fe31..c7d8ada 100644 --- a/tests/emails_test.py +++ b/tests/emails_test.py @@ -84,6 +84,21 @@ def test_email_response_html(self) -> None: except ResendError as e: assert e.message == "Failed to parse Resend API response. Please try again." + def test_update_email(self) -> None: + self.set_mock_json( + { + "id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794", + } + ) + update_params: resend.Emails.UpdateParams = { + "id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794", + "scheduled_at": "2024-09-07T11:52:01.858Z", + } + updated_email: resend.Emails.UpdateEmailResponse = resend.Emails.update( + params=update_params + ) + assert updated_email["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794" + def test_cancel_scheduled_email(self) -> None: self.set_mock_json( {