|
6 | 6 |
|
7 | 7 | from ._contact import Contact |
8 | 8 |
|
| 9 | +# Async imports (optional - only available with pip install resend[async]) |
| 10 | +try: |
| 11 | + from resend.async_request import AsyncRequest |
| 12 | +except ImportError: |
| 13 | + pass |
| 14 | + |
9 | 15 |
|
10 | 16 | class _ListResponse(TypedDict): |
11 | 17 | data: List[Contact] |
@@ -182,3 +188,114 @@ def remove( |
182 | 188 | path=path, params={}, verb="delete" |
183 | 189 | ).perform_with_content() |
184 | 190 | return resp |
| 191 | + |
| 192 | + @classmethod |
| 193 | + async def create_async(cls, params: CreateParams) -> Contact: |
| 194 | + """ |
| 195 | + Create a new contact (async). |
| 196 | + see more: https://resend.com/docs/api-reference/contacts/create-contact |
| 197 | +
|
| 198 | + Args: |
| 199 | + params (CreateParams): The contact creation parameters |
| 200 | +
|
| 201 | + Returns: |
| 202 | + Contact: The new contact object |
| 203 | + """ |
| 204 | + path = f"/audiences/{params['audience_id']}/contacts" |
| 205 | + resp = await AsyncRequest[Contact]( |
| 206 | + path=path, params=cast(Dict[Any, Any], params), verb="post" |
| 207 | + ).perform_with_content() |
| 208 | + return resp |
| 209 | + |
| 210 | + @classmethod |
| 211 | + async def update_async(cls, params: UpdateParams) -> Contact: |
| 212 | + """ |
| 213 | + Update an existing contact (async). |
| 214 | + see more: https://resend.com/docs/api-reference/contacts/update-contact |
| 215 | +
|
| 216 | + Args: |
| 217 | + params (UpdateParams): The contact update parameters |
| 218 | +
|
| 219 | + Returns: |
| 220 | + Contact: The updated contact object |
| 221 | + """ |
| 222 | + if params.get("id") is None and params.get("email") is None: |
| 223 | + raise ValueError("id or email must be provided") |
| 224 | + |
| 225 | + val = params.get("id") if params.get("id") is not None else params.get("email") |
| 226 | + |
| 227 | + path = f"/audiences/{params['audience_id']}/contacts/{val}" |
| 228 | + resp = await AsyncRequest[Contact]( |
| 229 | + path=path, params=cast(Dict[Any, Any], params), verb="patch" |
| 230 | + ).perform_with_content() |
| 231 | + return resp |
| 232 | + |
| 233 | + @classmethod |
| 234 | + async def list_async(cls, audience_id: str) -> ListResponse: |
| 235 | + """ |
| 236 | + List all contacts for the provided audience (async). |
| 237 | + see more: https://resend.com/docs/api-reference/contacts/list-contacts |
| 238 | +
|
| 239 | + Args: |
| 240 | + audience_id (str): The audience ID |
| 241 | +
|
| 242 | + Returns: |
| 243 | + ListResponse: A list of contact objects |
| 244 | + """ |
| 245 | + path = f"/audiences/{audience_id}/contacts" |
| 246 | + resp = await AsyncRequest[_ListResponse]( |
| 247 | + path=path, params={}, verb="get" |
| 248 | + ).perform_with_content() |
| 249 | + return resp |
| 250 | + |
| 251 | + @classmethod |
| 252 | + async def get_async( |
| 253 | + cls, audience_id: str, id: Optional[str] = None, email: Optional[str] = None |
| 254 | + ) -> Contact: |
| 255 | + """ |
| 256 | + Get a contact (async). |
| 257 | + see more: https://resend.com/docs/api-reference/contacts/get-contact |
| 258 | +
|
| 259 | + Args: |
| 260 | + audience_id (str): The audience ID |
| 261 | + id (Optional[str]): The contact ID |
| 262 | + email (Optional[str]): The contact email |
| 263 | +
|
| 264 | + Returns: |
| 265 | + Contact: The contact object |
| 266 | + """ |
| 267 | + contact = email if id is None else id |
| 268 | + if contact is None: |
| 269 | + raise ValueError("id or email must be provided") |
| 270 | + |
| 271 | + path = f"/audiences/{audience_id}/contacts/{contact}" |
| 272 | + resp = await AsyncRequest[Contact]( |
| 273 | + path=path, params={}, verb="get" |
| 274 | + ).perform_with_content() |
| 275 | + return resp |
| 276 | + |
| 277 | + @classmethod |
| 278 | + async def remove_async( |
| 279 | + cls, audience_id: str, id: Optional[str] = None, email: Optional[str] = None |
| 280 | + ) -> Contact: |
| 281 | + """ |
| 282 | + Remove a contact by ID or by Email (async). |
| 283 | + see more: https://resend.com/docs/api-reference/contacts/delete-contact |
| 284 | +
|
| 285 | + Args: |
| 286 | + audience_id (str): The audience ID |
| 287 | + id (Optional[str]): The contact ID |
| 288 | + email (Optional[str]): The contact email |
| 289 | +
|
| 290 | + Returns: |
| 291 | + Contact: The removed contact object |
| 292 | + """ |
| 293 | + contact = email if id is None else id |
| 294 | + if contact is None: |
| 295 | + raise ValueError("id or email must be provided") |
| 296 | + path = f"/audiences/{audience_id}/contacts/{contact}" |
| 297 | + |
| 298 | + resp = await AsyncRequest[Contact]( |
| 299 | + path=path, params={}, verb="delete" |
| 300 | + ).perform_with_content() |
| 301 | + return resp |
0 commit comments