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 src/Resend/IResend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,15 @@ public interface IResend
/// <returns>Segment.</returns>
Task<ResendResponse<Segment>> SegmentRetrieveAsync( Guid segmentId, CancellationToken cancellationToken = default );

/// <summary>
/// Updates a segment.
/// </summary>
/// <param name="segmentId">Segment identifier.</param>
/// <param name="segment">Segment data.</param>
/// <param name="cancellationToken">Cancelation token.</param>
/// <returns>Updated segment.</returns>
Task<ResendResponse<SegmentUpdateResult>> SegmentUpdateAsync( Guid segmentId, SegmentData segment, CancellationToken cancellationToken = default );

/// <summary>
/// Deletes a segment.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Resend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The `ResendClient` supports the following objects (and methods):
* Broadcast (List, Add, Retrieve, Update, Send, Schedule, Delete)
* Audience (List, Add, Retrieve, Delete)
* Contact (List, Add, Retrieve, Update, Delete)
* Segment (List, Create, Retrieve, Delete)
* Segment (List, Create, Retrieve, Update, Delete)
* Topics (List, Create, Retrieve, Update, Delete)
* Templates (List, Create, Retrieve, Update, Delete, Publish, Duplicate)
* Webhooks (List, Create, Retrieve, Update, Delete)
Expand Down
11 changes: 11 additions & 0 deletions src/Resend/ResendClient.Segments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public partial class ResendClient
}


/// <inheritdoc />
public Task<ResendResponse<SegmentUpdateResult>> SegmentUpdateAsync( Guid segmentId, SegmentData segment, CancellationToken cancellationToken = default )
{
var path = $"/segments/{segmentId}";
var req = new HttpRequestMessage( HttpMethod.Patch, path );
req.Content = JsonContent.Create( segment );

return Execute<SegmentUpdateResult, SegmentUpdateResult>( req, ( x ) => x, cancellationToken );
}


/// <inheritdoc />
public Task<ResendResponse> SegmentDeleteAsync( Guid segmentId, CancellationToken cancellationToken = default )
{
Expand Down
21 changes: 21 additions & 0 deletions src/Resend/SegmentUpdateResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;

namespace Resend;

/// <summary>
/// Result of updating a segment.
/// </summary>
public class SegmentUpdateResult
{
/// <summary>
/// Object type discriminator.
/// </summary>
[JsonPropertyName( "object" )]
public string Object { get; set; } = default!;

/// <summary>
/// Segment identifier.
/// </summary>
[JsonPropertyName( "id" )]
public Guid Id { get; set; }
}
21 changes: 21 additions & 0 deletions tests/Resend.Tests/ResendClientTests.Segment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Resend.Tests;

/// <summary />
public partial class ResendClientTests
{
/// <summary/>
[Fact]
public async Task SegmentUpdate()
{
var segmentId = Guid.NewGuid();

var resp = await _resend.SegmentUpdateAsync( segmentId, new SegmentData()
{
Name = "Renamed segment",
} );

Assert.NotNull( resp );
Assert.NotNull( resp.Content );
Assert.Equal( segmentId, resp.Content.Id );
}
}
32 changes: 32 additions & 0 deletions tools/Resend.ApiServer/Controllers/SegmentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Mvc;

namespace Resend.ApiServer.Controllers;

/// <summary />
[ApiController]
public class SegmentController : ControllerBase
{
private readonly ILogger<SegmentController> _logger;


/// <summary />
public SegmentController( ILogger<SegmentController> logger )
{
_logger = logger;
}


/// <summary />
[HttpPatch]
[Route( "segments/{id}" )]
public SegmentUpdateResult SegmentUpdate( [FromRoute] Guid id, [FromBody] SegmentData data )
{
_logger.LogDebug( "SegmentUpdate" );

return new SegmentUpdateResult()
{
Object = "segment",
Id = id,
};
}
}
42 changes: 42 additions & 0 deletions tools/Resend.Cli/Segment/SegmentUpdateCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using McMaster.Extensions.CommandLineUtils;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's maybe not update the CLI? we've already deprecated it anyway, we don't need to update it

using System.ComponentModel.DataAnnotations;

namespace Resend.Cli.Segment;

/// <summary />
[Command( "update", Description = "Update a segment" )]
public class SegmentUpdateCommand
{
private readonly IResend _resend;

/// <summary />
[Argument( 0, Description = "Segment identifier" )]
[Required]
public Guid? SegmentId { get; set; }

/// <summary />
[Argument( 1, Description = "Segment name" )]
[Required]
public string? Name { get; set; }


/// <summary />
public SegmentUpdateCommand( IResend resend )
{
_resend = resend;
}


/// <summary />
public async Task<int> OnExecuteAsync()
{
var data = new SegmentData()
{
Name = this.Name!,
};

await _resend.SegmentUpdateAsync( this.SegmentId!.Value, data );

return 0;
}
}
1 change: 1 addition & 0 deletions tools/Resend.Cli/SegmentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Resend.Cli;
[Subcommand( typeof( Segment.SegmentDeleteCommand ) )]
[Subcommand( typeof( Segment.SegmentListCommand ) )]
[Subcommand( typeof( Segment.SegmentRetrieveCommand ) )]
[Subcommand( typeof( Segment.SegmentUpdateCommand ) )]
public class SegmentCommand
{
/// <summary />
Expand Down
Loading