Skip to content

Commit

Permalink
chore: better types organization
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Dec 15, 2024
1 parent 41ea893 commit d7d76a0
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 67 deletions.
10 changes: 6 additions & 4 deletions examples/broadcasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
print("Broadcast is not in draft status, cannot remove it.\n")
7 changes: 1 addition & 6 deletions resend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,8 +44,5 @@
"Email",
"Attachment",
"Tag",
"CreateBroadcastResponse",
"SendBroadcastResponse",
"RemoveBroadcastResponse",
"Broadcast",
]
148 changes: 93 additions & 55 deletions resend/broadcasts/_broadcasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
31 changes: 29 additions & 2 deletions tests/broadcasts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
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()

0 comments on commit d7d76a0

Please sign in to comment.