Skip to content

Commit 8d1e10a

Browse files
authored
fix: Use httpErrorAsException when passed as parameter to SendGridClient constructor (#1180)
When passing httpErrorAsException as a parameter to the SendGridClient constructor, it was not used for anything. This change passes the value the the options object created.
1 parent 4336992 commit 8d1e10a

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/SendGrid/SendGridClient.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ public class SendGridClient : BaseClient
2020
/// <param name="requestHeaders">A dictionary of request headers.</param>
2121
/// <param name="version">API version, override AddVersion to customize.</param>
2222
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
23+
/// <param name="httpErrorAsException">Indicates whether HTTP error responses should be raised as exceptions. Default is false.</param>
2324
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
2425
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))
26+
: base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
2627
{
2728
}
2829

@@ -35,9 +36,10 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
3536
/// <param name="requestHeaders">A dictionary of request headers.</param>
3637
/// <param name="version">API version, override AddVersion to customize.</param>
3738
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
39+
/// <param name="httpErrorAsException">Indicates whether HTTP error responses should be raised as exceptions. Default is false.</param>
3840
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
3941
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))
42+
: base(httpClient, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
4143
{
4244
}
4345

@@ -51,7 +53,7 @@ public SendGridClient(HttpClient httpClient, string apiKey, string host = null,
5153
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
5254
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
5355
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))
56+
: base(buildOptions(apiKey, host, requestHeaders, version, urlPath, false))
5557
{
5658
}
5759

@@ -76,7 +78,7 @@ public SendGridClient(HttpClient httpClient, SendGridClientOptions options)
7678
{
7779
}
7880

79-
private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath)
81+
private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath, bool httpErrorAsException)
8082
{
8183
return new SendGridClientOptions
8284
{
@@ -85,6 +87,7 @@ private static SendGridClientOptions buildOptions(string apiKey, string host, Di
8587
RequestHeaders = requestHeaders ?? DefaultOptions.RequestHeaders,
8688
Version = version ?? DefaultOptions.Version,
8789
UrlPath = urlPath ?? DefaultOptions.UrlPath,
90+
HttpErrorAsException = httpErrorAsException
8891
};
8992
}
9093
}

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+
private 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)