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
1 change: 1 addition & 0 deletions examples/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"name": "Weekly Newsletter",
"default_subscription": "opt_in",
"description": "Subscribe to our weekly newsletter for updates",
"visibility": "public",
}

topic: resend.Topics.CreateTopicResponse = resend.Topics.create(create_params)
Expand Down
3 changes: 2 additions & 1 deletion resend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
SuppressionsBatch)
from .templates._template import Template, TemplateListItem, Variable
from .templates._templates import Templates
from .topics._topic import Topic
from .topics._topic import Topic, TopicVisibility
from .topics._topics import Topics
from .usage._usage import (AiCreditsUsage, AutomationRunsUsage,
BroadcastsUsage, ContactsUsage, DomainsUsage,
Expand Down Expand Up @@ -325,6 +325,7 @@
"InboxDraftListItem",
"InboxDraftType",
"Topic",
"TopicVisibility",
"OAuthGrant",
"OAuthGrantClient",
"BatchValidationError",
Expand Down
8 changes: 7 additions & 1 deletion resend/topics/_topic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing_extensions import TypedDict
from typing_extensions import Literal, NotRequired, TypedDict

TopicVisibility = Literal["public", "private"]


class Topic(TypedDict):
Expand All @@ -18,6 +20,10 @@ class Topic(TypedDict):
"""
The topic description.
"""
visibility: NotRequired[TopicVisibility]
"""
The visibility of the topic on the unsubscribe page. Possible values: public or private.
"""
created_at: str
"""
The date and time the topic was created.
Expand Down
15 changes: 14 additions & 1 deletion resend/topics/_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
except ImportError:
pass

from ._topic import Topic
from ._topic import Topic, TopicVisibility


class Topics:
Expand Down Expand Up @@ -44,6 +44,13 @@ class CreateParams(TypedDict):
"""
The topic description. Max length is 200 characters.
"""
visibility: NotRequired[TopicVisibility]
"""
The visibility of the topic on the unsubscribe page. Possible values: public or private.
private: only contacts who are opted in to the topic can see it.
public: all contacts can see it.
Defaults to private when not provided.
"""

class UpdateTopicResponse(BaseResponse):
"""
Expand All @@ -67,6 +74,10 @@ class UpdateParams(TypedDict, total=False):
"""
The topic description. Max length is 200 characters.
"""
visibility: TopicVisibility
"""
The visibility of the topic on the unsubscribe page. Possible values: public or private.
"""

class RemoveTopicResponse(BaseResponse):
"""
Expand Down Expand Up @@ -143,6 +154,7 @@ def create(cls, params: CreateParams) -> CreateTopicResponse:
- name: The topic name (max 50 characters)
- default_subscription: The default subscription preference ("opt_in" or "opt_out")
- description: Optional topic description (max 200 characters)
- visibility: Optional topic visibility ("public" or "private")

Returns:
CreateTopicResponse: The created topic response with the topic ID
Expand Down Expand Up @@ -183,6 +195,7 @@ def update(cls, id: str, params: UpdateParams) -> UpdateTopicResponse:
params (UpdateParams): The topic update parameters
- name: Optional topic name (max 50 characters)
- description: Optional topic description (max 200 characters)
- visibility: Optional topic visibility ("public" or "private")

Returns:
UpdateTopicResponse: The updated topic response with the topic ID
Expand Down
2 changes: 1 addition & 1 deletion resend/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "2.54.0"
__version__ = "2.54.1"


def get_version() -> str:
Expand Down
91 changes: 91 additions & 0 deletions tests/topics_async_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from unittest.mock import AsyncMock

import pytest
from typing_extensions import assert_type

import resend
from resend.exceptions import NoContentError
Expand Down Expand Up @@ -138,3 +141,91 @@ async def test_should_list_topics_async_raise_exception_when_no_content(
self.set_mock_json(None)
with pytest.raises(NoContentError):
_ = await resend.Topics.list_async()

async def test_topics_get_async_returns_visibility(self) -> None:
self.set_mock_json(
{
"object": "topic",
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
"name": "Weekly Newsletter",
"description": "Weekly newsletter for our subscribers",
"default_subscription": "opt_in",
"visibility": "public",
"created_at": "2026-04-08 00:11:13.110779+00",
}
)

topic = await resend.Topics.get_async(id="b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
assert_type(topic["visibility"], resend.TopicVisibility)
assert topic["visibility"] == "public"

async def test_topics_list_async_returns_visibility(self) -> None:
self.set_mock_json(
{
"object": "list",
"has_more": False,
"data": [
{
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
"name": "Weekly Newsletter",
"description": "Weekly newsletter for our subscribers",
"default_subscription": "opt_in",
"visibility": "public",
"created_at": "2026-04-08 00:11:13.110779+00",
}
],
}
)

topics = await resend.Topics.list_async()
assert_type(topics["data"][0]["visibility"], resend.TopicVisibility)
assert topics["data"][0]["visibility"] == "public"


class TestTopicsRequestBodyAsync:
def setup_method(self) -> None:
self.previous_api_key = resend.api_key
resend.api_key = "re_123"
self.mock_client = AsyncMock()
self.mock_client.request.return_value = (
b'{"object": "topic", "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e"}',
200,
{"content-type": "application/json"},
)
self.previous_async_http_client = resend.default_async_http_client
resend.default_async_http_client = self.mock_client

def teardown_method(self) -> None:
resend.default_async_http_client = self.previous_async_http_client
resend.api_key = self.previous_api_key

async def test_create_async_sends_visibility(self) -> None:
params: resend.Topics.CreateParams = {
"name": "Weekly Newsletter",
"default_subscription": "opt_in",
"visibility": "public",
}
await resend.Topics.create_async(params)

_, kwargs = self.mock_client.request.call_args
assert kwargs["method"] == "post"
assert kwargs["url"] == f"{resend.api_url}/topics"
assert kwargs["json"] == {
"name": "Weekly Newsletter",
"default_subscription": "opt_in",
"visibility": "public",
}

async def test_update_async_sends_visibility(self) -> None:
params: resend.Topics.UpdateParams = {"visibility": "private"}
await resend.Topics.update_async(
id="b6d24b8e-af0b-4c3c-be0c-359bbd97381e", params=params
)

_, kwargs = self.mock_client.request.call_args
assert kwargs["method"] == "patch"
assert (
kwargs["url"]
== f"{resend.api_url}/topics/b6d24b8e-af0b-4c3c-be0c-359bbd97381e"
)
assert kwargs["json"] == {"visibility": "private"}
92 changes: 92 additions & 0 deletions tests/topics_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from unittest import TestCase
from unittest.mock import create_autospec

from typing_extensions import assert_type

import resend
from resend.exceptions import NoContentError
from resend.http_client import HTTPClient
from tests.conftest import ResendBaseTest

# flake8: noqa
Expand Down Expand Up @@ -237,3 +243,89 @@ def test_topics_list_with_before_param(self) -> None:
assert topics["has_more"] is False
assert len(topics["data"]) == 1
assert topics["data"][0]["id"] == "topic-3"

def test_topics_get_returns_visibility(self) -> None:
self.set_mock_json(
{
"object": "topic",
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
"name": "Weekly Newsletter",
"description": "Weekly newsletter for our subscribers",
"default_subscription": "opt_in",
"visibility": "public",
"created_at": "2026-04-08 00:11:13.110779+00",
}
)

topic = resend.Topics.get(id="b6d24b8e-af0b-4c3c-be0c-359bbd97381e")
assert_type(topic["visibility"], resend.TopicVisibility)
assert topic["visibility"] == "public"

def test_topics_list_returns_visibility(self) -> None:
self.set_mock_json(
{
"object": "list",
"has_more": False,
"data": [
{
"id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e",
"name": "Weekly Newsletter",
"description": "Weekly newsletter for our subscribers",
"default_subscription": "opt_in",
"visibility": "public",
"created_at": "2026-04-08 00:11:13.110779+00",
}
],
}
)

topics = resend.Topics.list()
assert_type(topics["data"][0]["visibility"], resend.TopicVisibility)
assert topics["data"][0]["visibility"] == "public"


class TestTopicsRequestBody(TestCase):
def setUp(self) -> None:
self.previous_api_key = resend.api_key
resend.api_key = "re_123"
self.mock_client = create_autospec(HTTPClient, instance=True)
self.mock_client.request.return_value = (
b'{"object": "topic", "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e"}',
200,
{"Content-Type": "application/json"},
)
self.previous_http_client = resend.default_http_client
resend.default_http_client = self.mock_client

def tearDown(self) -> None:
resend.default_http_client = self.previous_http_client
resend.api_key = self.previous_api_key

def test_create_sends_visibility(self) -> None:
params: resend.Topics.CreateParams = {
"name": "Weekly Newsletter",
"default_subscription": "opt_in",
"visibility": "public",
}
resend.Topics.create(params)

_, kwargs = self.mock_client.request.call_args
assert kwargs["method"] == "post"
assert kwargs["url"] == f"{resend.api_url}/topics"
assert kwargs["json"] == {
"name": "Weekly Newsletter",
"default_subscription": "opt_in",
"visibility": "public",
}

def test_update_sends_visibility(self) -> None:
params: resend.Topics.UpdateParams = {"visibility": "private"}
resend.Topics.update(id="b6d24b8e-af0b-4c3c-be0c-359bbd97381e", params=params)

_, kwargs = self.mock_client.request.call_args
assert kwargs["method"] == "patch"
assert (
kwargs["url"]
== f"{resend.api_url}/topics/b6d24b8e-af0b-4c3c-be0c-359bbd97381e"
)
assert kwargs["json"] == {"visibility": "private"}
Loading