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

fix: Use httpErrorAsException when passed as parameter to SendGridClient constructor #1180

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 6 additions & 5 deletions src/SendGrid/SendGridClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SendGridClient : BaseClient
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false)
: base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath))
: base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
{
}

Expand All @@ -37,7 +37,7 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(HttpClient httpClient, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false)
: base(httpClient, buildOptions(apiKey, host, requestHeaders, version, urlPath))
: base(httpClient, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
{
}

Expand All @@ -50,8 +50,8 @@ public SendGridClient(HttpClient httpClient, string apiKey, string host = null,
/// <param name="version">API version, override AddVersion to customize.</param>
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null)
: base(buildOptions(apiKey, host, requestHeaders, version, urlPath))
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false)
mortenbock marked this conversation as resolved.
Show resolved Hide resolved
: base(buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
{
}

Expand All @@ -76,7 +76,7 @@ public SendGridClient(HttpClient httpClient, SendGridClientOptions options)
{
}

private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath)
private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath, bool httpErrorAsException)
{
return new SendGridClientOptions
{
Expand All @@ -85,6 +85,7 @@ private static SendGridClientOptions buildOptions(string apiKey, string host, Di
RequestHeaders = requestHeaders ?? DefaultOptions.RequestHeaders,
Version = version ?? DefaultOptions.Version,
UrlPath = urlPath ?? DefaultOptions.UrlPath,
HttpErrorAsException = httpErrorAsException
};
}
}
Expand Down
15 changes: 13 additions & 2 deletions tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6121,7 +6121,18 @@ public async Task TestRetryBehaviourSucceedsOnSecondAttempt()
}

[Fact]
public async void TestHttpErrorAsException()
public async void TestHttpErrorAsExceptionWhenSetInOptions()
{
await TestHttpErrorAsException((client, apiKey) => new SendGridClient(client, new SendGridClientOptions {ApiKey = apiKey, HttpErrorAsException = true}));
}

[Fact]
public async void TestHttpErrorAsExceptionWhenSetAsParameter()
{
await TestHttpErrorAsException((client, apiKey) => new SendGridClient(client, apiKey, httpErrorAsException: true));
}

public async Task TestHttpErrorAsException(Func<HttpClient, string, SendGridClient> createClientFunc)
{
var responseObject = new
{
Expand All @@ -6138,7 +6149,7 @@ public async void TestHttpErrorAsException()
});
var mockHandler = new FixedStatusAndMessageHttpMessageHandler(HttpStatusCode.ServiceUnavailable, responseMessage);
var mockClient = new HttpClient(mockHandler);
var client = new SendGridClient(mockClient, new SendGridClientOptions { ApiKey = fixture.apiKey, HttpErrorAsException = true });
var client = createClientFunc(mockClient, fixture.apiKey);

try
{
Expand Down