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: add back client constructors with client options and actually use its reliability settings #1001

Merged
merged 1 commit into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
eshanholtz marked this conversation as resolved.
Show resolved Hide resolved
: 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