Skip to content

fix: changing the response types to keep consistency with Resend docs #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions examples/api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"name": "example.com",
}

key: resend.ApiKey = resend.ApiKeys.create(params=create_params)
key: resend.ApiKeys.CreateResponse = resend.ApiKeys.create(params=create_params)
print("Created new api key")
print(f"Key id: {key['id']} and token: {key['token']}")

keys: resend.ApiKeys.ListResponse = resend.ApiKeys.list()
for key in keys["data"]:
print(key["id"])
print(key["name"])
print(key["created_at"])
for k in keys["data"]:
print(k["id"])
print(k["name"])
print(k["created_at"])

if len(keys) > 0:
resend.ApiKeys.remove(api_key_id=keys["data"][0]["id"])
4 changes: 2 additions & 2 deletions examples/audiences.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
create_params: resend.Audiences.CreateParams = {
"name": "New Audience from Python SDK",
}
audience: resend.Audience = resend.Audiences.create(create_params)
audience: resend.Audiences.CreateResponse = resend.Audiences.create(create_params)
print(f"Created audience: {audience['id']}")
print(audience)

Expand All @@ -20,6 +20,6 @@
audiences: resend.Audiences.ListResponse = resend.Audiences.list()
print("List of audiences:", [a["id"] for a in audiences["data"]])

rmed: resend.Audience = resend.Audiences.remove(id=audience["id"])
rmed: resend.Audiences.RemoveResponse = resend.Audiences.remove(id=audience["id"])
print(f"Deleted audience")
print(rmed)
12 changes: 7 additions & 5 deletions examples/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"unsubscribed": False,
}

contact: resend.Contact = resend.Contacts.create(create_params)
contact: resend.Contacts.CreateResponse = resend.Contacts.create(create_params)
print("Created contact !")
print(contact)

Expand All @@ -28,7 +28,7 @@
"first_name": "Steve1",
}

updated: resend.Contact = resend.Contacts.update(update_params)
updated: resend.Contacts.CreateResponse = resend.Contacts.update(update_params)
print("updated contact !")
print(updated)

Expand All @@ -38,11 +38,13 @@

contacts: resend.Contacts.ListResponse = resend.Contacts.list(audience_id=audience_id)
print("List of contacts")
for contact in contacts["data"]:
print(contact)
for c in contacts["data"]:
print(c)

# remove by email
rmed = resend.Contacts.remove(audience_id=audience_id, email=cont["email"])
rmed: resend.Contacts.RemoveResponse = resend.Contacts.remove(
audience_id=audience_id, email=cont["email"]
)

# remove by id
# rmed: resend.Contact = resend.Contacts.remove(audience_id=audience_id, id=cont["id"])
Expand Down
14 changes: 8 additions & 6 deletions examples/domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"name": "example.com",
"region": "us-east-1",
}
domain: resend.Domain = resend.Domains.create(params=create_params)
domain: resend.Domains.CreateResponse = resend.Domains.create(params=create_params)
print(domain)

retrieved: resend.Domain = resend.Domains.get(domain_id=domain["id"])
Expand All @@ -27,18 +27,20 @@
"click_tracking": True,
}

updated_domain: resend.Domain = resend.Domains.update(update_params)
updated_domain: resend.Domains.UpdateResponse = resend.Domains.update(update_params)
print(f"Updated domain: {updated_domain['id']}")

domains: resend.Domains.ListResponse = resend.Domains.list()
if not domains:
print("No domains found")
for domain in domains["data"]:
print(domain)
for d in domains["data"]:
print(d)

verified_domain: resend.Domain = resend.Domains.verify(domain_id=domain["id"])
verified_domain: resend.Domains.VerifyResponse = resend.Domains.verify(
domain_id=domain["id"]
)
print(f"Verified")
print(verified_domain)

rm_domain: resend.Domain = resend.Domains.remove(domain_id=domain["id"])
rm_domain: resend.Domains.RemoveResponse = resend.Domains.remove(domain_id=domain["id"])
print(rm_domain)
2 changes: 1 addition & 1 deletion examples/simple_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
}

email: resend.Email = resend.Emails.send(params)
email: resend.Emails.SendResponse = resend.Emails.send(params)
print(f"Sent email")
print("Email ID: ", email["id"])

Expand Down
2 changes: 1 addition & 1 deletion examples/with_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"attachments": [attachment],
}

email: resend.Email = resend.Emails.send(params)
email: resend.Emails.SendResponse = resend.Emails.send(params)
print("Sent email with attachment")
print(email)
2 changes: 1 addition & 1 deletion examples/with_b64_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
}

# Send email
email: resend.Email = resend.Emails.send(params)
email: resend.Emails.SendResponse = resend.Emails.send(params)
print("Email sent with base64 string attachment")
print(email)
2 changes: 1 addition & 1 deletion examples/with_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
}

try:
email: resend.Email = resend.Emails.send(params)
email: resend.Emails.SendResponse = resend.Emails.send(params)
except resend.exceptions.ResendError as e:
print(f"Error sending email: {e}")
2 changes: 1 addition & 1 deletion examples/with_html_file_as_b64_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
}

# Send email
email: resend.Email = resend.Emails.send(params)
email: resend.Emails.SendResponse = resend.Emails.send(params)
print("Sent email with base64 string attachment")
print("Email ID: ", email["id"])
4 changes: 0 additions & 4 deletions resend/api_keys/_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class ApiKey(TypedDict):
"""
The api key ID
"""
token: str
"""
The api key token
"""
name: str
"""
The api key token
Expand Down
24 changes: 22 additions & 2 deletions resend/api_keys/_api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,28 @@ class _ListResponse(TypedDict):
"""


class _CreateResponse(TypedDict):
id: str
"""
The api key ID
"""
token: str
"""
The api key token
"""


class ApiKeys:

class CreateResponse(_CreateResponse):
"""
Class that wraps the response of the create API key endpoint

Attributes:
id (str): The API key ID
token (str): The API key token
"""

class ListResponse(_ListResponse):
"""
ListResponse type that wraps a list of API key objects
Expand All @@ -41,7 +61,7 @@ class CreateParams(TypedDict):
"""

@classmethod
def create(cls, params: CreateParams) -> ApiKey:
def create(cls, params: CreateParams) -> CreateResponse:
"""
Add a new API key to authenticate communications with Resend.
see more: https://resend.com/docs/api-reference/api-keys/create-api-key
Expand All @@ -53,7 +73,7 @@ def create(cls, params: CreateParams) -> ApiKey:
ApiKey: The new API key object
"""
path = "/api-keys"
resp = request.Request[ApiKey](
resp = request.Request[_CreateResponse](
path=path, params=cast(Dict[Any, Any], params), verb="post"
).perform_with_content()
return resp
Expand Down
13 changes: 9 additions & 4 deletions resend/audiences/_audience.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing_extensions import TypedDict
from typing_extensions import Literal, TypedDict

AudienceObject = Literal["audience"]

class Audience(TypedDict):

class ShortAudience(TypedDict):
id: str
"""
The unique identifier of the audience.
Expand All @@ -14,7 +16,10 @@ class Audience(TypedDict):
"""
The date and time the audience was created.
"""
deleted: bool


class Audience(ShortAudience):
object: AudienceObject
"""
Wether the audience was deleted. Only returned on the "remove" call
The object type
"""
71 changes: 63 additions & 8 deletions resend/audiences/_audiences.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,82 @@
from typing import Any, Dict, List, cast

from typing_extensions import TypedDict
from typing_extensions import Literal, TypedDict

from resend import request

from ._audience import Audience
from ._audience import Audience, AudienceObject, ShortAudience


class _ListResponse(TypedDict):
data: List[Audience]
object: Literal["list"]
"""
The object type
"""
data: List[ShortAudience]
"""
A list of audience objects
"""


class _CreateResponse(TypedDict):
object: AudienceObject
"""
The object type
"""
id: str
"""
The unique identifier of the audience.
"""
name: str
"""
The name of the audience.
"""


class _RemoveResponse(TypedDict):
object: AudienceObject
"""
The object type
"""
id: str
"""
The unique identifier of the audience.
"""
deleted: bool
"""
Whether the audience was deleted.
"""


class Audiences:

class CreateResponse(_CreateResponse):
"""
Class that wraps the response of the create audience endpoint

Attributes:
object (str): The object type
id (str): The audience ID
name (str): The audience name
"""

class ListResponse(_ListResponse):
"""
ListResponse type that wraps a list of audience objects

Attributes:
data (List[Audience]): A list of audience objects
object (str): The object type
data (List[ShortAudience]): A list of audience objects
"""

class RemoveResponse(_RemoveResponse):
"""
Class that wraps the response of the remove audience endpoint

Attributes:
object (str): The object type
id (str): The audience ID
deleted (bool): Whether the audience was deleted
"""

class CreateParams(TypedDict):
Expand All @@ -31,7 +86,7 @@ class CreateParams(TypedDict):
"""

@classmethod
def create(cls, params: CreateParams) -> Audience:
def create(cls, params: CreateParams) -> CreateResponse:
"""
Create a list of contacts.
see more: https://resend.com/docs/api-reference/audiences/create-audience
Expand All @@ -43,7 +98,7 @@ def create(cls, params: CreateParams) -> Audience:
Audience: The new audience object
"""
path = "/audiences"
resp = request.Request[Audience](
resp = request.Request[_CreateResponse](
path=path, params=cast(Dict[Any, Any], params), verb="post"
).perform_with_content()
return resp
Expand Down Expand Up @@ -82,7 +137,7 @@ def get(cls, id: str) -> Audience:
return resp

@classmethod
def remove(cls, id: str) -> Audience:
def remove(cls, id: str) -> RemoveResponse:
"""
Delete a single audience.
see more: https://resend.com/docs/api-reference/audiences/delete-audience
Expand All @@ -94,7 +149,7 @@ def remove(cls, id: str) -> Audience:
Audience: The audience object
"""
path = f"/audiences/{id}"
resp = request.Request[Audience](
resp = request.Request[_RemoveResponse](
path=path, params={}, verb="delete"
).perform_with_content()
return resp
13 changes: 9 additions & 4 deletions resend/contacts/_contact.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing_extensions import TypedDict
from typing_extensions import Literal, TypedDict

ContactObject = Literal["contact"]

class Contact(TypedDict):

class ShortContact(TypedDict):
id: str
"""
The contact id.
Expand All @@ -26,7 +28,10 @@ class Contact(TypedDict):
"""
The unsubscribed status of the contact.
"""
deleted: bool


class Contact(ShortContact):
object: ContactObject
"""
Wether the contact is deleted or not.
The object type
"""
Loading
Loading