Skip to content

Commit 1f858a4

Browse files
committed
refactor(Contacts): fix return type for create/udpate and cleanup types definitions
1 parent d915c74 commit 1f858a4

3 files changed

Lines changed: 58 additions & 23 deletions

File tree

examples/contacts.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"unsubscribed": False,
1818
}
1919

20-
contact: resend.Contact = resend.Contacts.create(create_params)
21-
print("Created contact !")
20+
contact: resend.Contacts.CreateContactResponse = resend.Contacts.create(create_params)
21+
print("Created contact with ID: {}".format(contact["id"]))
2222
print(contact)
2323

2424
update_params: resend.Contacts.UpdateParams = {
@@ -28,8 +28,8 @@
2828
"first_name": "Steve",
2929
}
3030

31-
updated: resend.Contact = resend.Contacts.update(update_params)
32-
print("updated contact !")
31+
updated: resend.Contacts.UpdateContactResponse = resend.Contacts.update(update_params)
32+
print("updated contact with ID: {}".format(updated["id"]))
3333
print(updated)
3434

3535
cont_by_id: resend.Contact = resend.Contacts.get(
@@ -46,11 +46,11 @@
4646

4747
contacts: resend.Contacts.ListResponse = resend.Contacts.list(audience_id=audience_id)
4848
print("List of contacts")
49-
for contact in contacts["data"]:
50-
print(contact)
49+
for c in contacts["data"]:
50+
print(c)
5151

5252
# remove by email
53-
rmed = resend.Contacts.remove(audience_id=audience_id, email=contact["email"])
53+
rmed = resend.Contacts.remove(audience_id=audience_id, email=cont_by_email["email"])
5454

5555
# remove by id
5656
# rmed: resend.Contact = resend.Contacts.remove(audience_id=audience_id, id=cont["id"])

resend/contacts/_contacts.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,57 @@
77
from ._contact import Contact
88

99

10-
class _ListResponse(TypedDict):
11-
data: List[Contact]
12-
"""
13-
A list of contact objects
14-
"""
15-
16-
1710
class Contacts:
1811

19-
class ListResponse(_ListResponse):
12+
class ListResponse(TypedDict):
2013
"""
2114
ListResponse type that wraps a list of contact objects
2215
2316
Attributes:
2417
data (List[Contact]): A list of contact objects
2518
"""
2619

20+
data: List[Contact]
21+
"""
22+
A list of contact objects
23+
"""
24+
25+
class CreateContactResponse(TypedDict):
26+
"""
27+
CreateContactResponse is the type that wraps the response of the contact that was created
28+
29+
Attributes:
30+
object (str): The ID of the created contact
31+
id (str): The ID of the created contact
32+
"""
33+
34+
object: str
35+
"""
36+
The object type: email
37+
"""
38+
id: str
39+
"""
40+
The ID of the scheduled email that was canceled.
41+
"""
42+
43+
class UpdateContactResponse(TypedDict):
44+
"""
45+
UpdateContactResponse is the type that wraps the response of the contact that was updated
46+
47+
Attributes:
48+
object (str): The ID of the updated contact
49+
id (str): The ID of the updated contact
50+
"""
51+
52+
object: str
53+
"""
54+
The object type: email
55+
"""
56+
id: str
57+
"""
58+
The ID of the updated contact.
59+
"""
60+
2761
class CreateParams(TypedDict):
2862
audience_id: str
2963
"""
@@ -73,7 +107,7 @@ class UpdateParams(TypedDict):
73107
"""
74108

75109
@classmethod
76-
def create(cls, params: CreateParams) -> Contact:
110+
def create(cls, params: CreateParams) -> CreateContactResponse:
77111
"""
78112
Create a new contact.
79113
see more: https://resend.com/docs/api-reference/contacts/create-contact
@@ -82,16 +116,17 @@ def create(cls, params: CreateParams) -> Contact:
82116
params (CreateParams): The contact creation parameters
83117
84118
Returns:
85-
Contact: The new contact object
119+
CreateContactResponse: The created contact response
86120
"""
121+
87122
path = f"/audiences/{params['audience_id']}/contacts"
88-
resp = request.Request[Contact](
123+
resp = request.Request[Contacts.CreateContactResponse](
89124
path=path, params=cast(Dict[Any, Any], params), verb="post"
90125
).perform_with_content()
91126
return resp
92127

93128
@classmethod
94-
def update(cls, params: UpdateParams) -> Contact:
129+
def update(cls, params: UpdateParams) -> UpdateContactResponse:
95130
"""
96131
Update an existing contact.
97132
see more: https://resend.com/docs/api-reference/contacts/update-contact
@@ -100,15 +135,15 @@ def update(cls, params: UpdateParams) -> Contact:
100135
params (UpdateParams): The contact update parameters
101136
102137
Returns:
103-
Contact: The updated contact object
138+
UpdateContactResponse: The updated contact response.
104139
"""
105140
if params.get("id") is None and params.get("email") is None:
106141
raise ValueError("id or email must be provided")
107142

108143
val = params.get("id") if params.get("id") is not None else params.get("email")
109144

110145
path = f"/audiences/{params['audience_id']}/contacts/{val}"
111-
resp = request.Request[Contact](
146+
resp = request.Request[Contacts.UpdateContactResponse](
112147
path=path, params=cast(Dict[Any, Any], params), verb="patch"
113148
).perform_with_content()
114149
return resp
@@ -126,7 +161,7 @@ def list(cls, audience_id: str) -> ListResponse:
126161
ListResponse: A list of contact objects
127162
"""
128163
path = f"/audiences/{audience_id}/contacts"
129-
resp = request.Request[_ListResponse](
164+
resp = request.Request[Contacts.ListResponse](
130165
path=path, params={}, verb="get"
131166
).perform_with_content()
132167
return resp

tests/contacts_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_contacts_create(self) -> None:
1818
"last_name": "Wozniak",
1919
"unsubscribed": True,
2020
}
21-
contact: resend.Contact = resend.Contacts.create(params)
21+
contact: resend.Contacts.CreateContactResponse = resend.Contacts.create(params)
2222
assert contact["id"] == "479e3145-dd38-476b-932c-529ceb705947"
2323

2424
def test_should_create_contacts_raise_exception_when_no_content(self) -> None:

0 commit comments

Comments
 (0)