Skip to content

Commit 7343b4b

Browse files
author
childish-sambino
authored
fix: revert the 'SendGridClient' constructor changes (#1029)
Changes to released public method signatures are breaking changes. This fix reverts the recent constructor changes and replaces the example/tests with usage of `SendGridClientOptions` for passing the `HttpErrorAsException` flag.
1 parent 998837c commit 7343b4b

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

ExampleNet45Project/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static async Task Main()
2626
// Retrieve the API key.
2727
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY") ?? configuration["SendGrid:ApiKey"];
2828

29-
var client = new SendGridClient(HttpClient, apiKey, httpErrorAsException: true);
29+
var client = new SendGridClient(HttpClient, new SendGridClientOptions { ApiKey = apiKey, HttpErrorAsException = true });
3030

3131
// Send a Single Email using the Mail Helper
3232
var from = new EmailAddress(configuration.GetValue("SendGrid:From", "[email protected]"), "Example User");

TROUBLESHOOTING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ Console.WriteLine(response.Body.ReadAsStringAsync().Result); // The message will
6464
Console.WriteLine(response.Headers.ToString());
6565
```
6666

67-
If you want to throw the exception when the API returns an error, init the SendGridClient with the parameter 'httpErrorAsException' as true:
67+
If you want to throw the exception when the API returns an error, init the SendGridClient with options including 'HttpErrorAsException' as true:
6868

6969
```csharp
70-
SendGridClient client = new SendGridClient(apiKey, httpErrorAsException: true);
70+
var client = new SendGridClient(new SendGridClientOptions{ ApiKey = apiKey, HttpErrorAsException = true });
7171
```
7272

7373
Then if an error is thrown due to a failed request, a summary of that error along with a link to the appropriate place in the documentation will be sent back as a JSON object in the Exception Message. You can also deserialize the JSON object as a SendGridErrorResponse. Every error status code returned by the API has its own type of exception (BadRequestException, UnauthorizedException, PayloadTooLargeException, SendGridInternalException, etc.). All the possibles status codes and errors are documented [here](https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html).
@@ -77,7 +77,7 @@ Then if an error is thrown due to a failed request, a summary of that error alon
7777
#### 401 - Unauthorized
7878

7979
```csharp
80-
SendGridClient client = new SendGridClient("", httpErrorAsException: true);
80+
var client = new SendGridClient(new SendGridClientOptions{ ApiKey = "SG.12345678901234567890123456789012", HttpErrorAsException = true });
8181

8282
try
8383
{
@@ -102,7 +102,7 @@ catch(Exception ex)
102102
#### 400 - Bad Request - From Email Null
103103

104104
```csharp
105-
var client = new SendGridClient(apiKey, httpErrorAsException: true);
105+
var client = new SendGridClient(new SendGridClientOptions{ ApiKey = apiKey, HttpErrorAsException = true });
106106

107107
try
108108
{

src/SendGrid/SendGridClient.cs

+5-9
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ 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">Whether HTTP error responses should be raised as exceptions.</param>
2423
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
2524
public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false)
26-
: base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
25+
: base(webProxy, buildOptions(apiKey, host, requestHeaders, version, urlPath))
2726
{
2827
}
2928

@@ -36,10 +35,9 @@ public SendGridClient(IWebProxy webProxy, string apiKey, string host = null, Dic
3635
/// <param name="requestHeaders">A dictionary of request headers.</param>
3736
/// <param name="version">API version, override AddVersion to customize.</param>
3837
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
39-
/// <param name="httpErrorAsException">Whether HTTP error responses should be raised as exceptions.</param>
4038
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
4139
public SendGridClient(HttpClient httpClient, string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false)
42-
: base(httpClient, buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
40+
: base(httpClient, buildOptions(apiKey, host, requestHeaders, version, urlPath))
4341
{
4442
}
4543

@@ -51,10 +49,9 @@ public SendGridClient(HttpClient httpClient, string apiKey, string host = null,
5149
/// <param name="requestHeaders">A dictionary of request headers.</param>
5250
/// <param name="version">API version, override AddVersion to customize.</param>
5351
/// <param name="urlPath">Path to endpoint (e.g. /path/to/endpoint).</param>
54-
/// <param name="httpErrorAsException">Whether HTTP error responses should be raised as exceptions.</param>
5552
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
56-
public SendGridClient(string apiKey, string host = null, Dictionary<string, string> requestHeaders = null, string version = null, string urlPath = null, bool httpErrorAsException = false)
57-
: base(buildOptions(apiKey, host, requestHeaders, version, urlPath, httpErrorAsException))
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))
5855
{
5956
}
6057

@@ -79,7 +76,7 @@ public SendGridClient(HttpClient httpClient, SendGridClientOptions options)
7976
{
8077
}
8178

82-
private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath, bool httpErrorAsException)
79+
private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath)
8380
{
8481
return new SendGridClientOptions
8582
{
@@ -88,7 +85,6 @@ private static SendGridClientOptions buildOptions(string apiKey, string host, Di
8885
RequestHeaders = requestHeaders ?? DefaultOptions.RequestHeaders,
8986
Version = version ?? DefaultOptions.Version,
9087
UrlPath = urlPath ?? DefaultOptions.UrlPath,
91-
HttpErrorAsException = httpErrorAsException // No default needed for bool.
9288
};
9389
}
9490
}

tests/SendGrid.Tests/Integration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5929,7 +5929,7 @@ public async void TestHttpErrorAsException()
59295929
var responseMessage = Newtonsoft.Json.JsonConvert.SerializeObject(responseObject);
59305930
var mockHandler = new FixedStatusAndMessageHttpMessageHandler(HttpStatusCode.ServiceUnavailable, responseMessage);
59315931
var mockClient = new HttpClient(mockHandler);
5932-
var client = new SendGridClient(mockClient, fixture.apiKey, httpErrorAsException: true);
5932+
var client = new SendGridClient(mockClient, new SendGridClientOptions { ApiKey = fixture.apiKey, HttpErrorAsException = true });
59335933

59345934
try
59355935
{

0 commit comments

Comments
 (0)