diff --git a/examples/broadcasts.py b/examples/broadcasts.py index e355214..907db83 100644 --- a/examples/broadcasts.py +++ b/examples/broadcasts.py @@ -20,14 +20,14 @@ "name": "Hello, world!", } -broadcast: resend.CreateBroadcastResponse = resend.Broadcasts.create(create_params) +broadcast: resend.Broadcasts.CreateResponse = resend.Broadcasts.create(create_params) print("Created broadcast !") print(broadcast) send_params: resend.Broadcasts.SendParams = { "broadcast_id": broadcast["id"], } -sent: resend.SendBroadcastResponse = resend.Broadcasts.send(send_params) +sent: resend.Broadcasts.SendResponse = resend.Broadcasts.send(send_params) print("Sent broadcast !\n") print(sent) @@ -36,9 +36,11 @@ print(retrieved) if retrieved["status"] == "draft": - removed: resend.RemoveBroadcastResponse = resend.Broadcasts.remove(id=broadcast["id"]) + removed: resend.Broadcasts.RemoveResponse = resend.Broadcasts.remove( + id=broadcast["id"] + ) print("Removed broadcast !\n") print(removed) print("\n") else: - print("Broadcast is not in draft status, cannot remove it.\n") \ No newline at end of file + print("Broadcast is not in draft status, cannot remove it.\n") diff --git a/resend/__init__.py b/resend/__init__.py index 4b4d07e..773817f 100644 --- a/resend/__init__.py +++ b/resend/__init__.py @@ -5,9 +5,7 @@ from .audiences._audience import Audience from .audiences._audiences import Audiences from .broadcasts._broadcast import Broadcast -from .broadcasts._broadcasts import (Broadcasts, CreateBroadcastResponse, - RemoveBroadcastResponse, - SendBroadcastResponse) +from .broadcasts._broadcasts import Broadcasts from .contacts._contact import Contact from .contacts._contacts import Contacts from .domains._domain import Domain @@ -46,8 +44,5 @@ "Email", "Attachment", "Tag", - "CreateBroadcastResponse", - "SendBroadcastResponse", - "RemoveBroadcastResponse", "Broadcast", ] diff --git a/resend/broadcasts/_broadcasts.py b/resend/broadcasts/_broadcasts.py index 484c5d7..ada3a82 100644 --- a/resend/broadcasts/_broadcasts.py +++ b/resend/broadcasts/_broadcasts.py @@ -17,6 +17,43 @@ ) +class _CreateResponse(TypedDict): + id: str + """ + id of the created broadcast + """ + + +class _SendResponse(_CreateResponse): + pass + + +class _RemoveResponse(TypedDict): + object: str + """ + object type: "broadcast" + """ + id: str + """ + id of the removed broadcast + """ + deleted: bool + """ + True if the broadcast was deleted + """ + + +class _ListResponse(TypedDict): + object: str + """ + object type: "list" + """ + data: List[Broadcast] + """ + A list of broadcast objects + """ + + class _CreateParamsDefault(_CreateParamsFrom): audience_id: str """ @@ -56,32 +93,6 @@ class _SendBroadcastParams(TypedDict): """ -class CreateBroadcastResponse(TypedDict): - id: str - """ - id of the created broadcast - """ - - -class SendBroadcastResponse(CreateBroadcastResponse): - pass - - -class RemoveBroadcastResponse(TypedDict): - object: str - """ - object type: "broadcast" - """ - id: str - """ - id of the removed broadcast - """ - deleted: bool - """ - True if the broadcast was deleted - """ - - class Broadcasts: class CreateParams(_CreateParamsDefault): @@ -106,16 +117,43 @@ class SendParams(_SendBroadcastParams): The date should be in language natural (e.g.: in 1 min) or ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z). """ - # class ListResponse(_ListResponse): - # """ - # ListResponse type that wraps a list of audience objects + class CreateResponse(_CreateResponse): + """ + CreateResponse is the class that wraps the response of the create method. + + Attributes: + id (str): id of the created broadcast + """ - # Attributes: - # data (List[Audience]): A list of audience objects - # """ + class SendResponse(_SendResponse): + """ + SendResponse is the class that wraps the response of the send method. + + Attributes: + id (str): id of the created broadcast + """ + + class ListResponse(_ListResponse): + """ + ListResponse is the class that wraps the response of the list method. + + Attributes: + object (str): object type: "list" + data (List[Broadcast]): A list of broadcast objects + """ + + class RemoveResponse(_RemoveResponse): + """ + RemoveResponse is the class that wraps the response of the remove method. + + Attributes: + object (str): object type: "broadcast" + id (str): id of the removed broadcast + deleted (bool): True if the broadcast was deleted + """ @classmethod - def create(cls, params: CreateParams) -> CreateBroadcastResponse: + def create(cls, params: CreateParams) -> CreateResponse: """ Create a broadcast. see more: https://resend.com/docs/api-reference/broadcasts/create-broadcast @@ -124,16 +162,16 @@ def create(cls, params: CreateParams) -> CreateBroadcastResponse: params (CreateParams): The audience creation parameters Returns: - CreateBroadcastResponse: The new broadcast object response + CreateResponse: The new broadcast object response """ path = "/broadcasts" - resp = request.Request[CreateBroadcastResponse]( + resp = request.Request[_CreateResponse]( path=path, params=cast(Dict[Any, Any], params), verb="post" ).perform_with_content() return resp @classmethod - def send(cls, params: SendParams) -> SendBroadcastResponse: + def send(cls, params: SendParams) -> SendResponse: """ Sends a broadcast. see more: https://resend.com/docs/api-reference/broadcasts/send-broadcast @@ -142,28 +180,28 @@ def send(cls, params: SendParams) -> SendBroadcastResponse: params (CreateParams): The audience creation parameters Returns: - SendBroadcastResponse: The new broadcast object response + SendResponse: The new broadcast object response """ path = f"/broadcasts/{params['broadcast_id']}/send" - resp = request.Request[SendBroadcastResponse]( + resp = request.Request[_SendResponse]( path=path, params=cast(Dict[Any, Any], params), verb="post" ).perform_with_content() return resp - # @classmethod - # def list(cls) -> ListResponse: - # """ - # Retrieve a list of audiences. - # see more: https://resend.com/docs/api-reference/audiences/list-audiences - - # Returns: - # ListResponse: A list of audience objects - # """ - # path = "/audiences/" - # resp = request.Request[_ListResponse]( - # path=path, params={}, verb="get" - # ).perform_with_content() - # return resp + @classmethod + def list(cls) -> ListResponse: + """ + Retrieve a list of broadcasts. + see more: https://resend.com/docs/api-reference/broadcasts/list-broadcasts + + Returns: + ListResponse: A list of broadcast objects + """ + path = "/broadcasts/" + resp = request.Request[_ListResponse]( + path=path, params={}, verb="get" + ).perform_with_content() + return resp @classmethod def get(cls, id: str) -> Broadcast: @@ -184,7 +222,7 @@ def get(cls, id: str) -> Broadcast: return resp @classmethod - def remove(cls, id: str) -> RemoveBroadcastResponse: + def remove(cls, id: str) -> RemoveResponse: """ Delete a single broadcast. see more: https://resend.com/docs/api-reference/broadcasts/delete-broadcasts @@ -193,10 +231,10 @@ def remove(cls, id: str) -> RemoveBroadcastResponse: id (str): The broadcast ID Returns: - RemoveBroadcastResponse: The remove response object + RemoveResponse: The remove response object """ path = f"/broadcasts/{id}" - resp = request.Request[RemoveBroadcastResponse]( + resp = request.Request[_RemoveResponse]( path=path, params={}, verb="delete" ).perform_with_content() return resp diff --git a/tests/broadcasts_test.py b/tests/broadcasts_test.py index e23b13f..005ee33 100644 --- a/tests/broadcasts_test.py +++ b/tests/broadcasts_test.py @@ -15,7 +15,7 @@ def test_broadcasts_create(self) -> None: "subject": "Hello, world!", "name": "Python SDK Broadcast", } - broadcast = resend.Broadcasts.create(params) + broadcast: resend.Broadcasts.CreateResponse = resend.Broadcasts.create(params) assert broadcast["id"] == "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794" def test_broadcasts_get(self) -> None: @@ -69,4 +69,31 @@ def test_broadcasts_remove(self) -> None: rmed = resend.Broadcasts.remove("78261eea-8f8b-4381-83c6-79fa7120f1cf") assert rmed["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf" - assert rmed["deleted"] is True \ No newline at end of file + assert rmed["deleted"] is True + + def test_broadcasts_list(self) -> None: + self.set_mock_json( + { + "object": "list", + "data": [ + { + "id": "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794", + "audience_id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", + "status": "draft", + "created_at": "2024-11-01T15:13:31.723Z", + "scheduled_at": None, + "sent_at": None, + }, + { + "id": "559ac32e-9ef5-46fb-82a1-b76b840c0f7b", + "audience_id": "78261eea-8f8b-4381-83c6-79fa7120f1cf", + "status": "sent", + "created_at": "2024-12-01T19:32:22.980Z", + "scheduled_at": "2024-12-02T19:32:22.980Z", + "sent_at": "2024-12-02T19:32:22.980Z", + }, + ], + } + ) + + broadcasts: resend.Broadcasts.ListResponse = resend.Broadcasts.list()