Skip to content

Commit 196c841

Browse files
committed
fix: Use httpErrorAsException when passed as parameter to SendGridClient constructor
1 parent 4336992 commit 196c841

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/SendGrid/SendGridClient.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class SendGridClient : BaseClient
2222
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
2323
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
2424
public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false)
25-
: base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath))
25+
: base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
2626
{
2727
}
2828

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

@@ -50,8 +50,8 @@ public SendGridClient(HttpClient httpClient, string apiKey, string host = null,
5050
/// <param name="version">API version, override AddVersion to customize.</param>
5151
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
5252
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
53-
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null)
54-
: base(buildOptions(apiKey, host, requestHeaders, version, urlPath))
53+
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false)
54+
: base(buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
5555
{
5656
}
5757

@@ -76,7 +76,7 @@ public SendGridClient(HttpClient httpClient, SendGridClientOptions options)
7676
{
7777
}
7878

79-
private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath)
79+
private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath, bool httpErrorAsException)
8080
{
8181
return new SendGridClientOptions
8282
{
@@ -85,6 +85,7 @@ private static SendGridClientOptions buildOptions(string apiKey, string host, Di
8585
RequestHeaders = requestHeaders ?? DefaultOptions.RequestHeaders,
8686
Version = version ?? DefaultOptions.Version,
8787
UrlPath = urlPath ?? DefaultOptions.UrlPath,
88+
HttpErrorAsException = httpErrorAsException
8889
};
8990
}
9091
}

tests/SendGrid.Tests/Integration.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6121,7 +6121,18 @@ public async Task TestRetryBehaviourSucceedsOnSecondAttempt()
61216121
}
61226122

61236123
[Fact]
6124-
public async void TestHttpErrorAsException()
6124+
public async void TestHttpErrorAsExceptionWhenSetInOptions()
6125+
{
6126+
await TestHttpErrorAsException((client, apiKey) => new SendGridClient(client, new SendGridClientOptions {ApiKey = apiKey, HttpErrorAsException = true}));
6127+
}
6128+
6129+
[Fact]
6130+
public async void TestHttpErrorAsExceptionWhenSetAsParameter()
6131+
{
6132+
await TestHttpErrorAsException((client, apiKey) => new SendGridClient(client, apiKey, httpErrorAsException: true));
6133+
}
6134+
6135+
public async Task TestHttpErrorAsException(Func<HttpClient, string, SendGridClient> createClientFunc)
61256136
{
61266137
var responseObject = new
61276138
{
@@ -6138,7 +6149,7 @@ public async void TestHttpErrorAsException()
61386149
});
61396150
var mockHandler = new FixedStatusAndMessageHttpMessageHandler(HttpStatusCode.ServiceUnavailable, responseMessage);
61406151
var mockClient = new HttpClient(mockHandler);
6141-
var client = new SendGridClient(mockClient, new SendGridClientOptions { ApiKey = fixture.apiKey, HttpErrorAsException = true });
6152+
var client = createClientFunc(mockClient, fixture.apiKey);
61426153

61436154
try
61446155
{

0 commit comments

Comments
 (0)