Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion examples/broadcasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@
"subject": "Hello, world!",
"html": "<p>Hello, world!</p>",
"text": "Hello, world!",
"reply_to": ["[email protected]", "[email protected]"],
"reply_to": ["[email protected]", "[email protected]"],
"name": "Hello, world!",
}

broadcast: resend.Broadcasts.CreateResponse = resend.Broadcasts.create(create_params)
print("Created broadcast !")
print(broadcast)

update_params: resend.Broadcasts.UpdateParams = {
"broadcast_id": broadcast["id"],
"html": "<p>Hello, world! Updated</p>",
"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"],
}
Expand Down
85 changes: 85 additions & 0 deletions resend/broadcasts/_broadcasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
},
)

_UpdateParamsFrom = TypedDict(
"_UpdateParamsFrom",
{
"from": NotRequired[str],
},
)


class _CreateResponse(TypedDict):
id: str
Expand All @@ -24,6 +31,13 @@ class _CreateResponse(TypedDict):
"""


class _UpdateResponse(TypedDict):
id: str
"""
id of the updated broadcast
"""


class _SendResponse(_CreateResponse):
pass

Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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.

Expand All @@ -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.
Expand Down Expand Up @@ -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:
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/broadcasts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down