diff --git a/examples/broadcasts.py b/examples/broadcasts.py index 80b9a00..a519327 100644 --- a/examples/broadcasts.py +++ b/examples/broadcasts.py @@ -16,7 +16,7 @@ "subject": "Hello, world!", "html": "

Hello, world!

", "text": "Hello, world!", - "reply_to": ["lel@lel.com", "lil@lil.com"], + "reply_to": ["foo@resend.dev", "bar@resend.dev"], "name": "Hello, world!", } @@ -24,6 +24,19 @@ print("Created broadcast !") print(broadcast) +update_params: resend.Broadcasts.UpdateParams = { + "broadcast_id": broadcast["id"], + "html": "

Hello, world! Updated

", + "text": "Hello, world! Updated", + "name": "Hello, world! Updated", +} + +updated_broadcast: resend.Broadcasts.UpdateResponse = resend.Broadcasts.update( + update_params +) +print("Updated broadcast!") +print(updated_broadcast) + send_params: resend.Broadcasts.SendParams = { "broadcast_id": broadcast["id"], } diff --git a/resend/broadcasts/_broadcasts.py b/resend/broadcasts/_broadcasts.py index a6e323d..9e4d6c4 100644 --- a/resend/broadcasts/_broadcasts.py +++ b/resend/broadcasts/_broadcasts.py @@ -16,6 +16,13 @@ }, ) +_UpdateParamsFrom = TypedDict( + "_UpdateParamsFrom", + { + "from": NotRequired[str], + }, +) + class _CreateResponse(TypedDict): id: str @@ -24,6 +31,13 @@ class _CreateResponse(TypedDict): """ +class _UpdateResponse(TypedDict): + id: str + """ + id of the updated broadcast + """ + + class _SendResponse(_CreateResponse): pass @@ -81,6 +95,37 @@ class _CreateParamsDefault(_CreateParamsFrom): """ +class _UpdateParamsDefault(_UpdateParamsFrom): + broadcast_id: str + """ + The ID of the broadcast you want to update. + """ + audience_id: NotRequired[str] + """ + The ID of the audience you want to send to. + """ + subject: NotRequired[str] + """ + Email subject. + """ + reply_to: NotRequired[Union[List[str], str]] + """ + Reply-to email address. For multiple addresses, send as an array of strings. + """ + html: NotRequired[str] + """ + The HTML version of the message. + """ + text: NotRequired[str] + """ + The text version of the message. + """ + name: NotRequired[str] + """ + The friendly name of the broadcast. Only used for internal reference. + """ + + class _SendBroadcastParams(TypedDict): broadcast_id: str """ @@ -108,6 +153,20 @@ class CreateParams(_CreateParamsDefault): name (NotRequired[str]): The friendly name of the broadcast. Only used for internal reference. """ + class UpdateParams(_UpdateParamsDefault): + """UpdateParams is the class that wraps the parameters for the update method. + + Attributes: + broadcast_id (str): The ID of the broadcast you want to update. + audience_id (str): The ID of the audience you want to send to. + from (str): The sender email address + subject (NotRequired[str]): Email subject. + reply_to (NotRequired[Union[List[str], str]]): Reply-to email address(es). + html (NotRequired[str]): The HTML version of the message. + text (NotRequired[str]): The text version of the message. + name (NotRequired[str]): The friendly name of the broadcast. Only used for internal reference. + """ + class SendParams(_SendBroadcastParams): """SendParams is the class that wraps the parameters for the send method. @@ -125,6 +184,14 @@ class CreateResponse(_CreateResponse): id (str): id of the created broadcast """ + class UpdateResponse(_UpdateResponse): + """ + UpdateResponse is the class that wraps the response of the update method. + + Attributes: + id (str): id of the updated broadcast + """ + class SendResponse(_SendResponse): """ SendResponse is the class that wraps the response of the send method. @@ -170,6 +237,24 @@ def create(cls, params: CreateParams) -> CreateResponse: ).perform_with_content() return resp + @classmethod + def update(cls, params: UpdateParams) -> UpdateResponse: + """ + Update a broadcast. + see more: https://resend.com/docs/api-reference/broadcasts/update-broadcast + + Args: + params (UpdateParams): The broadcast update parameters + + Returns: + UpdateResponse: The updated broadcast object response + """ + path = f"/broadcasts/{params['broadcast_id']}" + resp = request.Request[_UpdateResponse]( + path=path, params=cast(Dict[Any, Any], params), verb="patch" + ).perform_with_content() + return resp + @classmethod def send(cls, params: SendParams) -> SendResponse: """ diff --git a/tests/broadcasts_test.py b/tests/broadcasts_test.py index c39a1e3..e5f63d0 100644 --- a/tests/broadcasts_test.py +++ b/tests/broadcasts_test.py @@ -18,6 +18,18 @@ def test_broadcasts_create(self) -> None: broadcast: resend.Broadcasts.CreateResponse = resend.Broadcasts.create(params) assert broadcast["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794" + def test_broadcasts_update(self) -> None: + self.set_mock_json({"id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"}) + + params: resend.Broadcasts.UpdateParams = { + "broadcast_id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794", + "audience_id": "78b8d3bc-a55a-45a3-aee6-6ec0a5e13d7e", + "subject": "Hello, world! Updated!", + "name": "Python SDK Broadcast", + } + broadcast: resend.Broadcasts.UpdateResponse = resend.Broadcasts.update(params) + assert broadcast["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794" + def test_broadcasts_get(self) -> None: self.set_mock_json( {