Skip to content

Commit

Permalink
Remove constructors that accept username + password
Browse files Browse the repository at this point in the history
Resolves #352
  • Loading branch information
Jericho committed Nov 25, 2020
1 parent db9ca3a commit 481941a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Source/StrongGrid.IntegrationTests/TestsRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task<int> RunAsync()
// -----------------------------------------------------------------------------
// Do you want to proxy requests through Fiddler? Can be useful for debugging.
var useFiddler = true;
var fiddlerPort = 8866; // By default Fiddler4 uses port 8888 and Fiddler Everywhere uses port 8866
var fiddlerPort = 8888; // By default Fiddler4 uses port 8888 and Fiddler Everywhere uses port 8866

// Change the default values in the legacy client.
var optionsToCorrectLegacyDefaultValues = new StrongGridClientOptions()
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));
}
}
}
18 changes: 3 additions & 15 deletions Source/StrongGrid/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ internal Pathoschild.Http.Client.IClient FluentClient
/// <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 +293,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

0 comments on commit 481941a

Please sign in to comment.