Skip to content

Commit

Permalink
DX-2037 Add TN Lookup API (#26)
Browse files Browse the repository at this point in the history
* Initial commit for tn lookup

* Additional tn lookup changes

* Update phone number default lookup

* Add create lookup test

* Remove Guid object for a string

* Add integration test stubs

* Update to the latest OpenAPI spec

* Update test stubs to the latest OpenAPI spec

* Fill out tn lookup integration tests

* Grab the number for testing

* Remove test assertion

* Bump up sleep to avoid rate limit

* Remove hardcoded number and test assert

* Move sleep to before the status lookup

* Add additional sleeps to avoid rate limiting

* Test request id assert

* Remove test assert

* Remove forgotten test assert

* Update tests for rate limiting

* Simplify tests and monitor rate limiting

* Skip tests due to rate limiting
  • Loading branch information
hamermike authored Jun 23, 2021
1 parent 85fa444 commit adc7ce5
Show file tree
Hide file tree
Showing 13 changed files with 2,130 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <copyright file="IPhoneNumberLookupBasicAuthCredentials.cs" company="APIMatic">
// Copyright (c) APIMatic. All rights reserved.
// </copyright>
namespace Bandwidth.Standard.Authentication
{
using System;

public interface IPhoneNumberLookupBasicAuthCredentials
{
/// <summary>
/// Gets basicAuthUserName.
/// </summary>
string BasicAuthUserName { get; }

/// <summary>
/// Gets basicAuthPassword.
/// </summary>
string BasicAuthPassword { get; }

/// <summary>
/// Returns true if credentials matched.
/// </summary>
bool Equals(string basicAuthUserName, string basicAuthPassword);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// <copyright file="PhoneNumberLookupBasicAuthManager.cs" company="APIMatic">
// Copyright (c) APIMatic. All rights reserved.
// </copyright>
namespace Bandwidth.Standard.Authentication
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Bandwidth.Standard.Http.Request;

/// <summary>
/// PhoneNumberLookupBasicAuthManager Class.
/// </summary>
internal class PhoneNumberLookupBasicAuthManager : IPhoneNumberLookupBasicAuthCredentials, IAuthManager
{
/// <summary>
/// Initializes a new instance of the <see cref="IrisBasicAuthManager"/> class.
/// </summary>
/// <param name="username"> Username.</param>
/// <param name="password"> Password.</param>
public PhoneNumberLookupBasicAuthManager(string username, string password)
{
this.BasicAuthUserName = username;
this.BasicAuthPassword = password;
}

/// <summary>
/// Gets basicAuthUserName.
/// </summary>
public string BasicAuthUserName { get; }

/// <summary>
/// Gets basicAuthPassword.
/// </summary>
public string BasicAuthPassword { get; }

/// <summary>
/// Check if credentials match.
/// </summary>
/// <param name="basicAuthUserName"> BasicAuthUserName.</param>
/// <param name="basicAuthPassword"> BasicAuthPassword.</param>
/// <returns> The boolean value.</returns>
public bool Equals(string basicAuthUserName, string basicAuthPassword)
{
return basicAuthUserName.Equals(this.BasicAuthUserName)
&& basicAuthPassword.Equals(this.BasicAuthPassword);
}

/// <summary>
/// Adds authentication to the given HttpRequest.
/// </summary>
/// <param name="httpRequest">Http Request.</param>
/// <returns>Returns the httpRequest after adding authentication.</returns>
public HttpRequest Apply(HttpRequest httpRequest)
{
string authCredentials = this.BasicAuthUserName + ":" + this.BasicAuthPassword;
byte[] data = Encoding.ASCII.GetBytes(authCredentials);
httpRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(data);
return httpRequest;
}

/// <summary>
/// Adds authentication to the given HttpRequest.
/// </summary>
/// <param name="httpRequest">Http Request.</param>
/// <returns>Returns the httpRequest after adding authentication.</returns>
public Task<HttpRequest> ApplyAsync(HttpRequest httpRequest)
{
string authCredentials = this.BasicAuthUserName + ":" + this.BasicAuthPassword;
byte[] data = Encoding.ASCII.GetBytes(authCredentials);
httpRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(data);
return Task.FromResult(httpRequest);
}
}
}
55 changes: 54 additions & 1 deletion Bandwidth.Standard/BandwidthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Bandwidth.Standard.Authentication;
using Bandwidth.Standard.Http.Client;
using Bandwidth.Standard.Messaging;
using Bandwidth.Standard.PhoneNumberLookup;
using Bandwidth.Standard.TwoFactorAuth;
using Bandwidth.Standard.Voice;
using Bandwidth.Standard.WebRtc;
Expand All @@ -30,10 +31,12 @@ public sealed class BandwidthClient: IConfiguration
internal readonly HttpCallBack httpCallBack;
private readonly MessagingBasicAuthManager messagingBasicAuthManager;
private readonly TwoFactorAuthBasicAuthManager twoFactorAuthBasicAuthManager;
private readonly PhoneNumberLookupBasicAuthManager phoneNumberLookupBasicAuthManager;
private readonly VoiceBasicAuthManager voiceBasicAuthManager;
private readonly WebRtcBasicAuthManager webRtcBasicAuthManager;
private readonly Lazy<MessagingClient> messaging;
private readonly Lazy<TwoFactorAuthClient> twoFactorAuth;
private readonly Lazy<PhoneNumberLookupClient> phoneNumberLookup;
private readonly Lazy<VoiceClient> voice;
private readonly Lazy<WebRtcClient> webRtc;

Expand All @@ -47,6 +50,11 @@ public sealed class BandwidthClient: IConfiguration
/// </summary>
public TwoFactorAuthClient TwoFactorAuth => twoFactorAuth.Value;

/// <summary>
/// Gets PhoneNumberLookupClient controller.
/// </summary>
public PhoneNumberLookupClient PhoneNumberLookup => this.phoneNumberLookup.Value;

/// <summary>
/// Provides access to VoiceClient controller.
/// </summary>
Expand All @@ -68,6 +76,8 @@ internal static BandwidthClient CreateFromEnvironment()
string messagingBasicAuthPassword = System.Environment.GetEnvironmentVariable("BANDWIDTH_STANDARD_MESSAGING_BASIC_AUTH_PASSWORD");
string twoFactorAuthBasicAuthUserName = System.Environment.GetEnvironmentVariable("BANDWIDTH_STANDARD_TWO_FACTOR_AUTH_BASIC_AUTH_USER_NAME");
string twoFactorAuthBasicAuthPassword = System.Environment.GetEnvironmentVariable("BANDWIDTH_STANDARD_TWO_FACTOR_AUTH_BASIC_AUTH_PASSWORD");
string phoneNumberLookupBasicAuthUserName = System.Environment.GetEnvironmentVariable("BANDWIDTH_STANDARD_PHONE_NUMBER_LOOKUP_BASIC_AUTH_USER_NAME");
string phoneNumberLookupBasicAuthPassword = System.Environment.GetEnvironmentVariable("BANDWIDTH_STANDARD_PHONE_NUMBER_LOOKUP_BASIC_AUTH_PASSWORD");
string voiceBasicAuthUserName = System.Environment.GetEnvironmentVariable("BANDWIDTH_STANDARD_VOICE_BASIC_AUTH_USER_NAME");
string voiceBasicAuthPassword = System.Environment.GetEnvironmentVariable("BANDWIDTH_STANDARD_VOICE_BASIC_AUTH_PASSWORD");
string webRtcBasicAuthUserName = System.Environment.GetEnvironmentVariable("BANDWIDTH_STANDARD_WEB_RTC_BASIC_AUTH_USER_NAME");
Expand Down Expand Up @@ -98,6 +108,11 @@ internal static BandwidthClient CreateFromEnvironment()
builder.TwoFactorAuthBasicAuthCredentials(twoFactorAuthBasicAuthUserName, twoFactorAuthBasicAuthPassword);
}

if (phoneNumberLookupBasicAuthUserName != null && phoneNumberLookupBasicAuthPassword != null)
{
builder.PhoneNumberLookupBasicAuthCredentials(phoneNumberLookupBasicAuthUserName, phoneNumberLookupBasicAuthPassword);
}

if (voiceBasicAuthUserName != null && voiceBasicAuthPassword != null)
{
builder.VoiceBasicAuthCredentials(voiceBasicAuthUserName, voiceBasicAuthPassword);
Expand All @@ -114,6 +129,7 @@ internal static BandwidthClient CreateFromEnvironment()
private BandwidthClient(TimeSpan timeout, Environment environment, string baseUrl,
string messagingBasicAuthUserName, string messagingBasicAuthPassword,
string twoFactorAuthBasicAuthUserName, string twoFactorAuthBasicAuthPassword,
string phoneNumberLookupBasicAuthUserName, string phoneNumberLookupBasicAuthPassword,
string voiceBasicAuthUserName, string voiceBasicAuthPassword,
string webRtcBasicAuthUserName, string webRtcBasicAuthPassword,
IDictionary<string, IAuthManager> authManagers, IHttpClient httpClient,
Expand Down Expand Up @@ -151,6 +167,18 @@ private BandwidthClient(TimeSpan timeout, Environment environment, string baseUr
this.authManagers["twoFactorAuth"] = twoFactorAuthBasicAuthManager;
}

if (this.authManagers.ContainsKey("phoneNumberLookup"))
{
this.phoneNumberLookupBasicAuthManager = (PhoneNumberLookupBasicAuthManager)this.authManagers["phoneNumberLookup"];
}

if (!this.authManagers.ContainsKey("phoneNumberLookup")
|| !this.PhoneNumberLookupBasicAuthCredentials.Equals(phoneNumberLookupBasicAuthUserName, phoneNumberLookupBasicAuthPassword))
{
this.phoneNumberLookupBasicAuthManager = new PhoneNumberLookupBasicAuthManager(phoneNumberLookupBasicAuthUserName, phoneNumberLookupBasicAuthPassword);
this.authManagers["phoneNumberLookup"] = this.phoneNumberLookupBasicAuthManager;
}

if (this.authManagers.ContainsKey("voice"))
{
voiceBasicAuthManager = (VoiceBasicAuthManager) this.authManagers["voice"];
Expand All @@ -177,6 +205,7 @@ private BandwidthClient(TimeSpan timeout, Environment environment, string baseUr

messaging = new Lazy<MessagingClient>(() => new MessagingClient(this));
twoFactorAuth = new Lazy<TwoFactorAuthClient>(() => new TwoFactorAuthClient(this));
phoneNumberLookup = new Lazy<PhoneNumberLookupClient>(() => new PhoneNumberLookupClient(this));
voice = new Lazy<VoiceClient>(() => new VoiceClient(this));
webRtc = new Lazy<WebRtcClient>(() => new WebRtcClient(this));
}
Expand All @@ -196,6 +225,11 @@ private BandwidthClient(TimeSpan timeout, Environment environment, string baseUr
/// </summary>
public ITwoFactorAuthBasicAuthCredentials TwoFactorAuthBasicAuthCredentials { get => twoFactorAuthBasicAuthManager; }

/// <summary>
/// Gets the credentials to use with PhoneNumberLookupBasicAuth.
/// </summary>
public IPhoneNumberLookupBasicAuthCredentials PhoneNumberLookupBasicAuthCredentials { get => phoneNumberLookupBasicAuthManager; }

/// <summary>
/// The credentials to use with VoiceBasicAuth
/// </summary>
Expand Down Expand Up @@ -231,6 +265,7 @@ private BandwidthClient(TimeSpan timeout, Environment environment, string baseUr
{ Server.Default, "api.bandwidth.com" },
{ Server.MessagingDefault, "https://messaging.bandwidth.com/api/v2" },
{ Server.TwoFactorAuthDefault, "https://mfa.bandwidth.com/api/v1" },
{ Server.PhoneNumberLookupDefault, "https://numbers.bandwidth.com/api/v1" },
{ Server.VoiceDefault, "https://voice.bandwidth.com" },
{ Server.WebRtcDefault, "https://api.webrtc.bandwidth.com/v1" },
}
Expand All @@ -241,6 +276,7 @@ private BandwidthClient(TimeSpan timeout, Environment environment, string baseUr
{ Server.Default, "{base_url}" },
{ Server.MessagingDefault, "{base_url}" },
{ Server.TwoFactorAuthDefault, "{base_url}" },
{ Server.PhoneNumberLookupDefault, "{base_url}" },
{ Server.VoiceDefault, "{base_url}" },
{ Server.WebRtcDefault, "{base_url}" },
}
Expand Down Expand Up @@ -284,6 +320,7 @@ public Builder ToBuilder()
.BaseUrl(BaseUrl)
.MessagingBasicAuthCredentials(messagingBasicAuthManager.BasicAuthUserName, messagingBasicAuthManager.BasicAuthPassword)
.TwoFactorAuthBasicAuthCredentials(twoFactorAuthBasicAuthManager.BasicAuthUserName, twoFactorAuthBasicAuthManager.BasicAuthPassword)
.PhoneNumberLookupBasicAuthCredentials(phoneNumberLookupBasicAuthManager.BasicAuthUserName, phoneNumberLookupBasicAuthManager.BasicAuthPassword)
.VoiceBasicAuthCredentials(voiceBasicAuthManager.BasicAuthUserName, voiceBasicAuthManager.BasicAuthPassword)
.WebRtcBasicAuthCredentials(webRtcBasicAuthManager.BasicAuthUserName, webRtcBasicAuthManager.BasicAuthPassword)
.HttpCallBack(httpCallBack)
Expand All @@ -302,6 +339,8 @@ public class Builder
private string messagingBasicAuthPassword = "TODO: Replace";
private string twoFactorAuthBasicAuthUserName = "TODO: Replace";
private string twoFactorAuthBasicAuthPassword = "TODO: Replace";
private string phoneNumberLookupBasicAuthUserName = "TODO: Replace";
private string phoneNumberLookupBasicAuthPassword = "TODO: Replace";
private string voiceBasicAuthUserName = "TODO: Replace";
private string voiceBasicAuthPassword = "TODO: Replace";
private string webRtcBasicAuthUserName = "TODO: Replace";
Expand Down Expand Up @@ -332,6 +371,19 @@ public Builder TwoFactorAuthBasicAuthCredentials(string twoFactorAuthBasicAuthUs
return this;
}

/// <summary>
/// Sets credentials for PhoneNumberLookupBasicAuth.
/// </summary>
/// <param name="phoneNumberLookupBasicAuthUserName">PhoneNumberLookupBasicAuthUserName.</param>
/// <param name="phoneNumberLookupBasicAuthPassword">PhoneNumberLookupBasicAuthPassword.</param>
/// <returns>Builder.</returns>
public Builder PhoneNumberLookupBasicAuthCredentials(string phoneNumberLookupBasicAuthUserName, string phoneNumberLookupBasicAuthPassword)
{
this.phoneNumberLookupBasicAuthUserName = phoneNumberLookupBasicAuthUserName ?? throw new ArgumentNullException(nameof(phoneNumberLookupBasicAuthUserName));
this.phoneNumberLookupBasicAuthPassword = phoneNumberLookupBasicAuthPassword ?? throw new ArgumentNullException(nameof(phoneNumberLookupBasicAuthPassword));
return this;
}

/// <summary>
/// Credentials setter for VoiceBasicAuth
/// </summary>
Expand Down Expand Up @@ -422,7 +474,8 @@ public BandwidthClient Build()
}

return new BandwidthClient(timeout, environment, baseUrl, messagingBasicAuthUserName, messagingBasicAuthPassword,
twoFactorAuthBasicAuthUserName, twoFactorAuthBasicAuthPassword, voiceBasicAuthUserName,
twoFactorAuthBasicAuthUserName, twoFactorAuthBasicAuthPassword,
phoneNumberLookupBasicAuthUserName, phoneNumberLookupBasicAuthPassword, voiceBasicAuthUserName,
voiceBasicAuthPassword, webRtcBasicAuthUserName, webRtcBasicAuthPassword, authManagers, httpClient,
httpCallBack, httpClientConfig);
}
Expand Down
5 changes: 5 additions & 0 deletions Bandwidth.Standard/IConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public interface IConfiguration
/// </summary>
ITwoFactorAuthBasicAuthCredentials TwoFactorAuthBasicAuthCredentials { get; }

/// <summary>
/// Gets the credentials to use with PhoneNumberLookupBasicAuth.
/// </summary>
IPhoneNumberLookupBasicAuthCredentials PhoneNumberLookupBasicAuthCredentials { get; }

/// <summary>
/// The credentials to use with VoiceBasicAuth
/// </summary>
Expand Down
Loading

0 comments on commit adc7ce5

Please sign in to comment.