Skip to content

Commit

Permalink
fix: add back client constructors with client options and actually us…
Browse files Browse the repository at this point in the history
…e its reliability settings (#1001)

Fixes #1000
Relates to #839

When the default client options were made static and the constructors later removed, there was no way to modify the reliability settings used by the retry handler. This fix adds back the constructors so clients can now be constructed with just the client options and the options will be used when creating a retry handler, if any.
  • Loading branch information
childish-sambino authored May 19, 2020
1 parent 22d9430 commit ecc86b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/SendGrid/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class BaseClient : ISendGridClient
/// </summary>
/// <param name="options">A <see cref="BaseClientOptions"/> instance that defines the configuration settings to use with the client.</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public BaseClient(BaseClientOptions options)
protected BaseClient(BaseClientOptions options)
: this(httpClient: null, options)
{
}
Expand All @@ -53,8 +53,8 @@ public BaseClient(BaseClientOptions options)
/// <param name="webProxy">Web proxy.</param>
/// <param name="options">A <see cref="BaseClientOptions"/> instance that defines the configuration settings to use with the client.</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public BaseClient(IWebProxy webProxy, BaseClientOptions options)
: this(CreateHttpClientWithWebProxy(webProxy), options)
protected BaseClient(IWebProxy webProxy, BaseClientOptions options)
: this(CreateHttpClientWithWebProxy(webProxy, options), options)
{
}

Expand All @@ -64,11 +64,11 @@ public BaseClient(IWebProxy webProxy, BaseClientOptions options)
/// <param name="httpClient">An optional HTTP client which may me injected in order to facilitate testing.</param>
/// <param name="options">A <see cref="BaseClientOptions"/> instance that defines the configuration settings to use with the client.</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public BaseClient(HttpClient httpClient, BaseClientOptions options)
protected BaseClient(HttpClient httpClient, BaseClientOptions options)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));

this.client = httpClient ?? CreateHttpClientWithRetryHandler();
this.client = httpClient ?? CreateHttpClientWithRetryHandler(options);
if (this.options.RequestHeaders != null && this.options.RequestHeaders.TryGetValue(ContentType, out var contentType))
{
this.MediaType = contentType;
Expand Down Expand Up @@ -221,17 +221,18 @@ public async Task<Response> RequestAsync(
cancellationToken: cancellationToken).ConfigureAwait(false);
}

private static HttpClient CreateHttpClientWithRetryHandler()
private static HttpClient CreateHttpClientWithRetryHandler(BaseClientOptions options)
{
return new HttpClient(new RetryDelegatingHandler(new ReliabilitySettings()));
return new HttpClient(new RetryDelegatingHandler(options.ReliabilitySettings));
}

/// <summary>
/// Create client with WebProxy if set.
/// </summary>
/// <param name="webProxy">the WebProxy.</param>
/// <param name="options">A <see cref="BaseClientOptions"/> instance that defines the configuration settings to use with the client.</param>
/// <returns>HttpClient with RetryDelegatingHandler and WebProxy if set.</returns>
private static HttpClient CreateHttpClientWithWebProxy(IWebProxy webProxy)
private static HttpClient CreateHttpClientWithWebProxy(IWebProxy webProxy, BaseClientOptions options)
{
if (webProxy != null)
{
Expand All @@ -242,13 +243,13 @@ private static HttpClient CreateHttpClientWithWebProxy(IWebProxy webProxy)
UseDefaultCredentials = false,
};

var retryHandler = new RetryDelegatingHandler(httpClientHandler, new ReliabilitySettings());
var retryHandler = new RetryDelegatingHandler(httpClientHandler, options.ReliabilitySettings);

return new HttpClient(retryHandler);
}
else
{
return CreateHttpClientWithRetryHandler();
return CreateHttpClientWithRetryHandler(options);
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/SendGrid/SendGridClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ public SendGridClient(string apiKey, string host = null, Dictionary<string, stri
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SendGridClient"/> class.
/// </summary>
/// <param name="options">A <see cref="SendGridClientOptions"/> instance that defines the configuration settings to use with the client.</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(SendGridClientOptions options)
: base(options)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SendGridClient"/> class.
/// </summary>
/// <param name="httpClient">An optional HTTP client which may me injected in order to facilitate testing.</param>
/// <param name="options">A <see cref="SendGridClientOptions"/> instance that defines the configuration settings to use with the client.</param>
/// <returns>Interface to the Twilio SendGrid REST API.</returns>
public SendGridClient(HttpClient httpClient, SendGridClientOptions options)
: base(httpClient, options)
{
}

private static SendGridClientOptions buildOptions(string apiKey, string host, Dictionary<string, string> requestHeaders, string version, string urlPath)
{
return new SendGridClientOptions
Expand Down

0 comments on commit ecc86b4

Please sign in to comment.