Skip to content

Commit

Permalink
Merge branch 'release/0.74.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Nov 26, 2020
2 parents 2e9fa0f + 8c92f57 commit 282eeed
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task RunAsync(IBaseClient client, TextWriter log, CancellationToken
});
await Task.WhenAll(cleanUpTasks).ConfigureAwait(false);

var domain = await client.SenderAuthentication.CreateDomainAsync(fictitiousDomain, "email", false, false, false, null, cancellationToken).ConfigureAwait(false);
var domain = await client.SenderAuthentication.CreateDomainAsync(fictitiousDomain, "email", null, null, false, false, false, null, null, cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"AuthenticatedSender domain created. Id: {domain.Id}").ConfigureAwait(false);

var domainValidation = await client.SenderAuthentication.ValidateDomainAsync(domain.Id, null, cancellationToken).ConfigureAwait(false);
Expand Down
7 changes: 1 addition & 6 deletions Source/StrongGrid.UnitTests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,12 @@ public void Dispose()
}

[Fact]
public void Throws_if_apikey_and_username_are_null()
public void Throws_if_apikey_is_null()
{
string apiKey = null;
string username = null;
string password = "myPassword";

Should.Throw<ArgumentNullException>(() => new Client(apiKey));
Should.Throw<ArgumentNullException>(() => new Client(username, password));

Should.Throw<ArgumentNullException>(() => new LegacyClient(apiKey));
Should.Throw<ArgumentNullException>(() => new LegacyClient(username, password));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Shouldly;
using StrongGrid.Models;
using StrongGrid.Resources;
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -404,9 +405,12 @@ public async Task CreateDomainAsync()
// Arrange
var domain = "";
var subdomain = "";
var username = "";
var ips = Array.Empty<string>();
var automaticSecurity = true;
var customSpf = false;
var isDefault = true;
var customDkimSelector = "";

var mockHttp = new MockHttpMessageHandler();
mockHttp.Expect(HttpMethod.Post, Utils.GetSendGridApiUri(ENDPOINT, "domains")).Respond("application/json", SINGLE_DOMAIN_JSON);
Expand All @@ -415,7 +419,7 @@ public async Task CreateDomainAsync()
var senderAuthentication = new SenderAuthentication(client);

// Act
var result = await senderAuthentication.CreateDomainAsync(domain, subdomain, automaticSecurity, customSpf, isDefault, null, CancellationToken.None).ConfigureAwait(false);
var result = await senderAuthentication.CreateDomainAsync(domain, subdomain, username, ips, automaticSecurity, customSpf, isDefault, customDkimSelector, null, CancellationToken.None).ConfigureAwait(false);

// Assert
mockHttp.VerifyNoOutstandingExpectation();
Expand Down
20 changes: 3 additions & 17 deletions Source/StrongGrid/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,11 @@ internal Pathoschild.Http.Client.IClient FluentClient
/// Initializes a new instance of the <see cref="BaseClient" /> class.
/// </summary>
/// <param name="apiKey">Your api key.</param>
/// <param name="username">Your username. Ignored if the api key is specified.</param>
/// <param name="password">Your password. Ignored if the api key is specified.</param>
/// <param name="httpClient">Allows you to inject your own HttpClient. This is useful, for example, to setup the HtppClient with a proxy.</param>
/// <param name="disposeClient">Indicates if the http client should be dispose when this instance of BaseClient is disposed.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public BaseClient(Parameter<string> apiKey, Parameter<string> username, Parameter<string> password, HttpClient httpClient, bool disposeClient, StrongGridClientOptions options, ILogger logger = null)
public BaseClient(string apiKey, HttpClient httpClient, bool disposeClient, StrongGridClientOptions options, ILogger logger = null)
{
_mustDisposeHttpClient = disposeClient;
_httpClient = httpClient;
Expand All @@ -293,20 +291,8 @@ public BaseClient(Parameter<string> apiKey, Parameter<string> username, Paramete
_fluentClient.Filters.Add(new DiagnosticHandler(_options.LogLevelSuccessfulCalls, _options.LogLevelFailedCalls, _logger));
_fluentClient.Filters.Add(new SendGridErrorHandler());

if (apiKey.HasValue)
{
if (string.IsNullOrEmpty(apiKey)) throw new ArgumentNullException(apiKey);
else _fluentClient.SetBearerAuthentication(apiKey);
}
else if (username.HasValue)
{
if (string.IsNullOrEmpty(username)) throw new ArgumentNullException(username);
else _fluentClient.SetBasicAuthentication(username, password);
}
else
{
throw new ArgumentException("You must provide either an API key or a username and a password.");
}
if (string.IsNullOrEmpty(apiKey)) throw new ArgumentNullException(apiKey);
_fluentClient.SetBearerAuthentication(apiKey);

AccessManagement = new AccessManagement(FluentClient);
Alerts = new Alerts(FluentClient);
Expand Down
63 changes: 4 additions & 59 deletions Source/StrongGrid/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Client : BaseClient, IClient
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public Client(string apiKey, StrongGridClientOptions options = null, ILogger logger = null)
: base(apiKey, default, default, null, false, options, logger)
: base(apiKey, null, false, options, logger)
{
Init();
}
Expand All @@ -85,7 +85,7 @@ public Client(string apiKey, StrongGridClientOptions options = null, ILogger log
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public Client(string apiKey, IWebProxy proxy, StrongGridClientOptions options = null, ILogger logger = null)
: base(apiKey, default, default, new HttpClient(new HttpClientHandler { Proxy = proxy, UseProxy = proxy != null }), true, options, logger)
: base(apiKey, new HttpClient(new HttpClientHandler { Proxy = proxy, UseProxy = proxy != null }), true, options, logger)
{
Init();
}
Expand All @@ -98,7 +98,7 @@ public Client(string apiKey, IWebProxy proxy, StrongGridClientOptions options =
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public Client(string apiKey, HttpMessageHandler handler, StrongGridClientOptions options = null, ILogger logger = null)
: base(apiKey, default, default, new HttpClient(handler), true, options, logger)
: base(apiKey, new HttpClient(handler), true, options, logger)
{
Init();
}
Expand All @@ -111,62 +111,7 @@ public Client(string apiKey, HttpMessageHandler handler, StrongGridClientOptions
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public Client(string apiKey, HttpClient httpClient, StrongGridClientOptions options = null, ILogger logger = null)
: base(apiKey, default, default, httpClient, false, options, logger)
{
Init();
}

/// <summary>
/// Initializes a new instance of the <see cref="Client"/> class.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public Client(string username, string password, StrongGridClientOptions options = null, ILogger logger = null)
: base(default, username, password, null, false, options, logger)
{
Init();
}

/// <summary>
/// Initializes a new instance of the <see cref="Client"/> class.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <param name="proxy">Allows you to specify a proxy.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public Client(string username, string password, IWebProxy proxy, StrongGridClientOptions options = null, ILogger logger = null)
: base(default, username, password, new HttpClient(new HttpClientHandler { Proxy = proxy, UseProxy = proxy != null }), true, options, logger)
{
Init();
}

/// <summary>
/// Initializes a new instance of the <see cref="Client"/> class.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <param name="handler">TThe HTTP handler stack to use for sending requests.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public Client(string username, string password, HttpMessageHandler handler, StrongGridClientOptions options = null, ILogger logger = null)
: base(default, username, password, new HttpClient(handler), true, options, logger)
{
Init();
}

/// <summary>
/// Initializes a new instance of the <see cref="Client" /> class.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <param name="httpClient">Allows you to inject your own HttpClient. This is useful, for example, to setup the HtppClient with a proxy.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public Client(string username, string password, HttpClient httpClient, StrongGridClientOptions options = null, ILogger logger = null)
: base(default, username, password, httpClient, false, options, logger)
: base(apiKey, httpClient, false, options, logger)
{
Init();
}
Expand Down
63 changes: 4 additions & 59 deletions Source/StrongGrid/LegacyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class LegacyClient : BaseClient, ILegacyClient
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public LegacyClient(string apiKey, StrongGridClientOptions options = null, ILogger logger = null)
: base(apiKey, default, default, null, false, options, logger)
: base(apiKey, null, false, options, logger)
{
Init();
}
Expand All @@ -92,7 +92,7 @@ public LegacyClient(string apiKey, StrongGridClientOptions options = null, ILogg
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public LegacyClient(string apiKey, IWebProxy proxy, StrongGridClientOptions options = null, ILogger logger = null)
: base(apiKey, default, default, new HttpClient(new HttpClientHandler { Proxy = proxy, UseProxy = proxy != null }), true, options, logger)
: base(apiKey, new HttpClient(new HttpClientHandler { Proxy = proxy, UseProxy = proxy != null }), true, options, logger)
{
Init();
}
Expand All @@ -105,7 +105,7 @@ public LegacyClient(string apiKey, IWebProxy proxy, StrongGridClientOptions opti
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public LegacyClient(string apiKey, HttpMessageHandler handler, StrongGridClientOptions options = null, ILogger logger = null)
: base(apiKey, default, default, new HttpClient(handler), true, options, logger)
: base(apiKey, new HttpClient(handler), true, options, logger)
{
Init();
}
Expand All @@ -118,62 +118,7 @@ public LegacyClient(string apiKey, HttpMessageHandler handler, StrongGridClientO
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public LegacyClient(string apiKey, HttpClient httpClient, StrongGridClientOptions options = null, ILogger logger = null)
: base(apiKey, default, default, httpClient, false, options, logger)
{
Init();
}

/// <summary>
/// Initializes a new instance of the <see cref="LegacyClient"/> class.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public LegacyClient(string username, string password, StrongGridClientOptions options = null, ILogger logger = null)
: base(default, username, password, null, false, options, logger)
{
Init();
}

/// <summary>
/// Initializes a new instance of the <see cref="LegacyClient"/> class.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <param name="proxy">Allows you to specify a proxy.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public LegacyClient(string username, string password, IWebProxy proxy, StrongGridClientOptions options = null, ILogger logger = null)
: base(default, username, password, new HttpClient(new HttpClientHandler { Proxy = proxy, UseProxy = proxy != null }), true, options, logger)
{
Init();
}

/// <summary>
/// Initializes a new instance of the <see cref="LegacyClient"/> class.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <param name="handler">TThe HTTP handler stack to use for sending requests.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public LegacyClient(string username, string password, HttpMessageHandler handler, StrongGridClientOptions options = null, ILogger logger = null)
: base(default, username, password, new HttpClient(handler), true, options, logger)
{
Init();
}

/// <summary>
/// Initializes a new instance of the <see cref="LegacyClient" /> class.
/// </summary>
/// <param name="username">Your username.</param>
/// <param name="password">Your password.</param>
/// <param name="httpClient">Allows you to inject your own HttpClient. This is useful, for example, to setup the HtppClient with a proxy.</param>
/// <param name="options">Options for the SendGrid client.</param>
/// <param name="logger">Logger.</param>
public LegacyClient(string username, string password, HttpClient httpClient, StrongGridClientOptions options = null, ILogger logger = null)
: base(default, username, password, httpClient, false, options, logger)
: base(apiKey, httpClient, false, options, logger)
{
Init();
}
Expand Down
21 changes: 13 additions & 8 deletions Source/StrongGrid/Resources/ISenderAuthentication.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using StrongGrid.Models;
using StrongGrid.Models;
using StrongGrid.Utilities;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -41,17 +43,20 @@ public interface ISenderAuthentication
/// <summary>
/// Create a new authenticated domain.
/// </summary>
/// <param name="domain">The domain.</param>
/// <param name="subdomain">The subdomain.</param>
/// <param name="automaticSecurity">if set to <c>true</c> [automatic security].</param>
/// <param name="customSpf">if set to <c>true</c> [custom SPF].</param>
/// <param name="isDefault">if set to <c>true</c> [is default].</param>
/// <param name="domain">The domain being authenticated..</param>
/// <param name="subdomain">The subdomain to use for this authenticated domain.</param>
/// <param name="username">The username associated with this domain.</param>
/// <param name="ips">The IP addresses that will be included in the custom SPF record for this authenticated domain.</param>
/// <param name="automaticSecurity">Whether to allow SendGrid to manage your SPF records, DKIM keys, and DKIM key rotation.</param>
/// <param name="customSpf">Specify whether to use a custom SPF or allow SendGrid to manage your SPF. This option is only available to authenticated domains set up for manual security.</param>
/// <param name="isDefault">Whether to use this authenticated domain as the fallback if no authenticated domains match the sender's domain.</param>
/// <param name="customDkimSelector">Add a custom DKIM selector. Accepts three letters or numbers. </param>
/// <param name="onBehalfOf">The user to impersonate.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>
/// The <see cref="AuthenticatedDomain" />.
/// </returns>
Task<AuthenticatedDomain> CreateDomainAsync(string domain, string subdomain, bool automaticSecurity = false, bool customSpf = false, bool isDefault = false, string onBehalfOf = null, CancellationToken cancellationToken = default);
Task<AuthenticatedDomain> CreateDomainAsync(string domain, string subdomain = null, string username = null, IEnumerable<string> ips = null, bool automaticSecurity = false, bool customSpf = false, bool isDefault = false, string customDkimSelector = null, string onBehalfOf = null, CancellationToken cancellationToken = default);

/// <summary>
/// Update an authenticated domain.
Expand All @@ -64,7 +69,7 @@ public interface ISenderAuthentication
/// <returns>
/// The <see cref="AuthenticatedDomain" />.
/// </returns>
Task<AuthenticatedDomain> UpdateDomainAsync(long domainId, bool isDefault = false, bool customSpf = false, string onBehalfOf = null, CancellationToken cancellationToken = default);
Task<AuthenticatedDomain> UpdateDomainAsync(long domainId, Parameter<bool> isDefault = default, Parameter<bool> customSpf = default, string onBehalfOf = null, CancellationToken cancellationToken = default);

/// <summary>
/// Delete an authenticated whitelabel.
Expand Down
Loading

0 comments on commit 282eeed

Please sign in to comment.