Skip to content

Commit

Permalink
feat: update email
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Aug 13, 2024
1 parent be070a2 commit f6f9ff3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
16 changes: 13 additions & 3 deletions examples/scheduled_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']}")
62 changes: 62 additions & 0 deletions resend/emails/_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions tests/emails_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down

0 comments on commit f6f9ff3

Please sign in to comment.