Skip to content
Merged
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
9 changes: 9 additions & 0 deletions examples/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
print(f"\n✓ Retrieved segment: {seg['name']}")
print(f" Created at: {seg['created_at']}")

# Update the segment's name
update_params: resend.Segments.UpdateParams = {
"name": "VIP Newsletter Subscribers (renamed)",
}
updated_segment: resend.Segments.UpdateSegmentResponse = resend.Segments.update(
id=segment["id"], params=update_params
)
print(f"\n✓ Updated segment: {updated_segment['id']}")

# List all segments
segments: resend.Segments.ListResponse = resend.Segments.list()
print(f"\n✓ List of segments: {[s['name'] for s in segments['data']]}")
Expand Down
64 changes: 64 additions & 0 deletions resend/segments/_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ class CreateParams(TypedDict):
The name of the segment.
"""

class UpdateSegmentResponse(BaseResponse):
"""
UpdateSegmentResponse is the type that wraps the response of the segment that was updated

Attributes:
object (str): The object type, "segment"
id (str): The ID of the updated segment
"""

object: str
"""
The object type, "segment"
"""
id: str
"""
The ID of the updated segment
"""

class UpdateParams(TypedDict):
name: str
"""
The new name of the segment.
"""

@classmethod
def create(cls, params: CreateParams) -> CreateSegmentResponse:
"""
Expand Down Expand Up @@ -171,6 +195,26 @@ def get(cls, id: str) -> Segment:
).perform_with_content()
return resp

@classmethod
def update(cls, id: str, params: UpdateParams) -> UpdateSegmentResponse:
"""
Update an existing segment's name.
see more: https://resend.com/docs/api-reference/segments/update-segment

Args:
id (str): The segment ID
params (UpdateParams): The segment update parameters
- name: The new name of the segment

Returns:
UpdateSegmentResponse: The updated segment response
"""
path = f"/segments/{id}"
resp = request.Request[Segments.UpdateSegmentResponse](
path=path, params=cast(Dict[Any, Any], params), verb="patch"
).perform_with_content()
return resp

@classmethod
def remove(cls, id: str) -> RemoveSegmentResponse:
"""
Expand Down Expand Up @@ -245,6 +289,26 @@ async def get_async(cls, id: str) -> Segment:
).perform_with_content()
return resp

@classmethod
async def update_async(cls, id: str, params: UpdateParams) -> UpdateSegmentResponse:
Comment thread
dielduarte marked this conversation as resolved.
"""
Update an existing segment's name (async).
see more: https://resend.com/docs/api-reference/segments/update-segment

Args:
id (str): The segment ID
params (UpdateParams): The segment update parameters
- name: The new name of the segment

Returns:
UpdateSegmentResponse: The updated segment response
"""
path = f"/segments/{id}"
resp = await AsyncRequest[Segments.UpdateSegmentResponse](
path=path, params=cast(Dict[Any, Any], params), verb="patch"
).perform_with_content()
return resp

@classmethod
async def remove_async(cls, id: str) -> RemoveSegmentResponse:
"""
Expand Down
40 changes: 40 additions & 0 deletions tests/segments_async_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest

import resend
from resend.exceptions import NoContentError
from tests.conftest import AsyncResendBaseTest

# flake8: noqa

pytestmark = pytest.mark.asyncio


class TestResendSegmentsAsync(AsyncResendBaseTest):
async def test_segments_update_async(self) -> None:
self.set_mock_json(
{
"object": "segment",
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
}
)

params: resend.Segments.UpdateParams = {
"name": "Renamed Segment",
}
segment = await resend.Segments.update_async(
id="78261eea-8f8b-4381-83c6-79fa7120f1cf", params=params
)
assert segment["object"] == "segment"
assert segment["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf"

async def test_update_segments_async_raises_exception_when_no_content(
self,
) -> None:
self.set_mock_json(None)
params: resend.Segments.UpdateParams = {
"name": "Renamed Segment",
}
with pytest.raises(NoContentError):
_ = await resend.Segments.update_async(
id="78261eea-8f8b-4381-83c6-79fa7120f1cf", params=params
)
27 changes: 27 additions & 0 deletions tests/segments_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ def test_should_get_segments_raise_exception_when_no_content(self) -> None:
with self.assertRaises(NoContentError):
_ = resend.Segments.get(id="78261eea-8f8b-4381-83c6-79fa7120f1cf")

def test_segments_update(self) -> None:
self.set_mock_json(
{
"object": "segment",
"id": "78261eea-8f8b-4381-83c6-79fa7120f1cf",
}
)

params: resend.Segments.UpdateParams = {
"name": "Renamed Segment",
}
segment = resend.Segments.update(
id="78261eea-8f8b-4381-83c6-79fa7120f1cf", params=params
)
assert segment["object"] == "segment"
assert segment["id"] == "78261eea-8f8b-4381-83c6-79fa7120f1cf"

def test_should_update_segments_raise_exception_when_no_content(self) -> None:
self.set_mock_json(None)
params: resend.Segments.UpdateParams = {
"name": "Renamed Segment",
}
with self.assertRaises(NoContentError):
_ = resend.Segments.update(
id="78261eea-8f8b-4381-83c6-79fa7120f1cf", params=params
)

def test_segments_remove(self) -> None:
self.set_mock_json(
{
Expand Down
Loading