Skip to content
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
88 changes: 88 additions & 0 deletions resend/audiences/_audiences.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,91 @@ def remove(cls, id: str) -> RemoveAudienceResponse:
stacklevel=2,
)
return Segments.remove(id)

@classmethod
async def create_async(cls, params: CreateParams) -> CreateAudienceResponse:
"""
Create a list of contacts (async).
see more: https://resend.com/docs/api-reference/audiences/create-audience

Args:
params (CreateParams): The audience creation parameters

Returns:
CreateAudienceResponse: The created audience response

.. deprecated::
Use Segments.create_async() instead. Audiences is now an alias for Segments.
"""
warnings.warn(
"Audiences is deprecated. Use Segments instead.",
DeprecationWarning,
stacklevel=2,
)
return await Segments.create_async(params)

@classmethod
async def list_async(cls, params: Optional[ListParams] = None) -> ListResponse:
"""
Retrieve a list of audiences (async).
see more: https://resend.com/docs/api-reference/audiences/list-audiences

Args:
params (Optional[ListParams]): Optional pagination parameters

Returns:
ListResponse: A list of audience objects

.. deprecated::
Use Segments.list_async() instead. Audiences is now an alias for Segments.
"""
warnings.warn(
"Audiences is deprecated. Use Segments instead.",
DeprecationWarning,
stacklevel=2,
)
return await Segments.list_async(params)

@classmethod
async def get_async(cls, id: str) -> Audience:
"""
Retrieve a single audience (async).
see more: https://resend.com/docs/api-reference/audiences/get-audience

Args:
id (str): The audience ID

Returns:
Audience: The audience object

.. deprecated::
Use Segments.get_async() instead. Audiences is now an alias for Segments.
"""
warnings.warn(
"Audiences is deprecated. Use Segments instead.",
DeprecationWarning,
stacklevel=2,
)
return await Segments.get_async(id)

@classmethod
async def remove_async(cls, id: str) -> RemoveAudienceResponse:
"""
Delete a single audience (async).
see more: https://resend.com/docs/api-reference/audiences/delete-audience

Args:
id (str): The audience ID

Returns:
RemoveAudienceResponse: The removed audience response

.. deprecated::
Use Segments.remove_async() instead. Audiences is now an alias for Segments.
"""
warnings.warn(
"Audiences is deprecated. Use Segments instead.",
DeprecationWarning,
stacklevel=2,
)
return await Segments.remove_async(id)
119 changes: 119 additions & 0 deletions tests/audiences_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,122 @@ async def test_should_list_segments_async_raise_exception_when_no_content(
self.set_mock_json(None)
with pytest.raises(NoContentError):
_ = await resend.Segments.list_async()


class TestResendAudiencesAsync(AsyncResendBaseTest):
async def test_audiences_create_async(self) -> None:
self.set_mock_json(
{
"object": "audience",
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
"name": "Registered Users",
}
)

params: resend.Audiences.CreateParams = {
"name": "Python SDK Audience",
}
with pytest.warns(DeprecationWarning):
audience = await resend.Audiences.create_async(params)
assert audience["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf"
assert audience["name"] == "Registered Users"

async def test_audiences_create_async_raises_when_no_content(
self,
) -> None:
self.set_mock_json(None)
params: resend.Audiences.CreateParams = {
"name": "Python SDK Audience",
}
with pytest.warns(DeprecationWarning):
with pytest.raises(NoContentError):
_ = await resend.Audiences.create_async(params)

async def test_audiences_get_async(self) -> None:
self.set_mock_json(
{
"object": "audience",
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
"name": "Registered Users",
"created_at": "2023-10-06T22:59:55.977Z",
}
)

with pytest.warns(DeprecationWarning):
audience = await resend.Audiences.get_async(
id="78261eea-8f8b-4381-83c6-79fa7120f1cf"
)
assert audience["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf"
assert audience["name"] == "Registered Users"
assert audience["created_at"] == "2023-10-06T22:59:55.977Z"

async def test_audiences_get_async_raises_when_no_content(
self,
) -> None:
self.set_mock_json(None)
with pytest.warns(DeprecationWarning):
with pytest.raises(NoContentError):
_ = await resend.Audiences.get_async(
id="78261eea-8f8b-4381-83c6-79fa7120f1cf"
)

async def test_audiences_remove_async(self) -> None:
self.set_mock_json(
{
"object": "audience",
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
"deleted": True,
}
)

with pytest.warns(DeprecationWarning):
rmed: resend.Audiences.RemoveAudienceResponse = (
await resend.Audiences.remove_async(
"78261eea-8f8b-4381-83c6-79fa7120f1cf"
)
)
assert rmed["object"] == "audience"
assert rmed["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf"
assert rmed["deleted"] is True

async def test_audiences_remove_async_raises_when_no_content(
self,
) -> None:
self.set_mock_json(None)
with pytest.warns(DeprecationWarning):
with pytest.raises(NoContentError):
_ = await resend.Audiences.remove_async(
id="78261eea-8f8b-4381-83c6-79fa7120f1cf"
)

async def test_audiences_list_async(self) -> None:
self.set_mock_json(
{
"object": "list",
"has_more": False,
"data": [
{
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
"name": "Registered Users",
"created_at": "2023-10-06T22:59:55.977Z",
}
],
}
)

with pytest.warns(DeprecationWarning):
audiences: resend.Audiences.ListResponse = (
await resend.Audiences.list_async()
)
assert audiences["object"] == "list"
assert audiences["has_more"] is False
assert audiences["data"][0]["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf"
assert audiences["data"][0]["name"] == "Registered Users"

async def test_audiences_list_async_raises_when_no_content(
self,
) -> None:
self.set_mock_json(None)
with pytest.warns(DeprecationWarning):
with pytest.raises(NoContentError):
_ = await resend.Audiences.list_async()
Loading