Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw derived ResendRateLimitExceededException. #26

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions src/Resend/IHasRateLimit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Resend
{
public interface IHasRateLimit
{
ResendRateLimit? Limits { get; }
}
}
11 changes: 10 additions & 1 deletion src/Resend/ResendClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ private async Task<ResendResponse> Execute( HttpRequestMessage req, Cancellation

if ( err == null )
ex = new ResendException( resp.StatusCode, ErrorType.MissingResponse, "Missing error response" );
else if ( err.ErrorType == ErrorType.RateLimitExceeded )
{
ex = new ResendRateLimitExceededException( HttpStatusCode.TooManyRequests, err.Message, rrl );
}
else
ex = new ResendException( (HttpStatusCode) err.StatusCode, err.ErrorType, err.Message );

Expand Down Expand Up @@ -551,6 +555,10 @@ private async Task<ResendResponse<T2>> Execute<T1, T2>( HttpRequestMessage req,

if ( err == null )
ex = new ResendException( resp.StatusCode, ErrorType.MissingResponse, "Missing error response" );
else if ( err.ErrorType == ErrorType.RateLimitExceeded )
{
ex = new ResendRateLimitExceededException( HttpStatusCode.TooManyRequests, err.Message, rrl );
}
else
ex = new ResendException( (HttpStatusCode) err.StatusCode, err.ErrorType, err.Message );

Expand Down Expand Up @@ -625,7 +633,8 @@ private async Task<ResendResponse<T2>> Execute<T1, T2>( HttpRequestMessage req,
return new ResendResponse<T2>( res, rrl );
}


/// <summary>
/// Rate limit info from the HTTP response headers.
/// <summary />
private ResendRateLimit FromHeaders( HttpResponseHeaders headers )
{
Expand Down
48 changes: 24 additions & 24 deletions src/Resend/ResendClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
namespace Resend;

/// <summary />
public class ResendClientOptions
{
/// <summary>
/// Base URL for Resend API.
/// </summary>
/// <remarks>
/// Default is `https://api.resend.com`.
/// </remarks>
public string ApiUrl { get; set; } = "https://api.resend.com";

/// <summary>
/// API key token.
/// </summary>
public string ApiToken { get; set; } = default!;

/// <summary>
/// Whether exceptions should be thrown when the API invocation
/// fails (for whatever reason).
/// </summary>
public bool ThrowExceptions { get; set; } = true;
}
namespace Resend;
/// <summary />
public class ResendClientOptions
{
/// <summary>
/// Base URL for Resend API.
/// </summary>
/// <remarks>
/// Default is `https://api.resend.com`.
/// </remarks>
public string ApiUrl { get; set; } = "https://api.resend.com";
/// <summary>
/// API key token.
/// </summary>
public string ApiToken { get; set; } = default!;
/// <summary>
/// Whether exceptions should be thrown when the API invocation
/// fails (for whatever reason).
/// </summary>
public bool ThrowExceptions { get; set; } = true;
}
32 changes: 32 additions & 0 deletions src/Resend/ResendRateLimitExceededException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Net;

namespace Resend;

/// <summary>
/// Status code 429. User has hit their rate limit and should wait before resending the request.
/// <summary />
public class ResendRateLimitExceededException : ResendException, IHasRateLimit
{
/// <summary>
/// Initializes a new instance of the <see cref="ResendRateLimitExceededException"/> class.
/// <summary />
public ResendRateLimitExceededException( HttpStatusCode? statusCode, string message, ResendRateLimit? rateLimit )
: base( statusCode, ErrorType.RateLimitExceeded, message )
{
this.Limits = rateLimit;
}

/// <summary>
/// Initializes a new instance of the <see cref="ResendRateLimitExceededException"/> class.
/// <summary />
public ResendRateLimitExceededException( HttpStatusCode? statusCode, string message, Exception? innerException, ResendRateLimit? rateLimit )
: base( statusCode, ErrorType.RateLimitExceeded, message, innerException )
{
this.Limits = rateLimit;
}

/// <summary>
/// Gets the rate limit information from the response headers.
/// <summary />
public ResendRateLimit? Limits { get; }
}