Skip to content

Commit

Permalink
DX-2052 phone number lookup sdk support (#33)
Browse files Browse the repository at this point in the history
* DX-2052 phone number lookup sdk support

* fixed syntax

* added tn lookup tests

* fixed test syntax

* reverted json serialization issue

* review changes

* fixed more timeout things
  • Loading branch information
jmulford-bw authored Jun 23, 2021
1 parent ee09881 commit d5490c9
Show file tree
Hide file tree
Showing 38 changed files with 3,318 additions and 321 deletions.
157 changes: 125 additions & 32 deletions src/main/java/com/bandwidth/BandwidthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.AbstractMap.SimpleEntry;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
* Gateway class for the library.
Expand All @@ -27,6 +28,7 @@ public final class BandwidthClient implements Configuration {
*/
private MessagingClient messagingClient;
private TwoFactorAuthClient twoFactorAuthClient;
private PhoneNumberLookupClient phoneNumberLookupClient;
private VoiceClient voiceClient;
private WebRtcClient webRtcClient;

Expand Down Expand Up @@ -65,6 +67,11 @@ public final class BandwidthClient implements Configuration {
*/
private TwoFactorAuthBasicAuthManager twoFactorAuthBasicAuthManager;

/**
* PhoneNumberLookupBasicAuthManager.
*/
private PhoneNumberLookupBasicAuthManager phoneNumberLookupBasicAuthManager;

/**
* VoiceBasicAuthManager.
*/
Expand All @@ -85,17 +92,18 @@ public final class BandwidthClient implements Configuration {
*/
private final HttpCallback httpCallback;

private BandwidthClient(Environment environment, String baseUrl, HttpClient httpClient,
long timeout, ReadonlyHttpClientConfiguration httpClientConfig,
String messagingBasicAuthUserName, String messagingBasicAuthPassword,
String twoFactorAuthBasicAuthUserName, String twoFactorAuthBasicAuthPassword,
String voiceBasicAuthUserName, String voiceBasicAuthPassword,
String webRtcBasicAuthUserName, String webRtcBasicAuthPassword,
Map<String, AuthManager> authManagers, HttpCallback httpCallback) {
private BandwidthClient(Environment environment, String baseUrl, HttpClient httpClient, long timeout,
ReadonlyHttpClientConfiguration httpClientConfig, String messagingBasicAuthUserName,
String messagingBasicAuthPassword, String twoFactorAuthBasicAuthUserName,
String twoFactorAuthBasicAuthPassword, String phoneNumberLookupBasicAuthUserName,
String phoneNumberLookupBasicAuthPassword, String voiceBasicAuthUserName,
String voiceBasicAuthPassword, String webRtcBasicAuthUserName,
String webRtcBasicAuthPassword, Map<String, AuthManager> authManagers,
HttpCallback httpCallback) {
this.environment = environment;
this.baseUrl = baseUrl;
this.httpClient = httpClient;
this.timeout = timeout;
this.httpClient = httpClient;
this.httpClientConfig = httpClientConfig;
this.httpCallback = httpCallback;

Expand All @@ -119,13 +127,26 @@ private BandwidthClient(Environment environment, String baseUrl, HttpClient http
}

if (!this.authManagers.containsKey("twoFactorAuth")
|| !getTwoFactorAuthBasicAuthCredentials().equals(twoFactorAuthBasicAuthUserName,
twoFactorAuthBasicAuthPassword)) {
|| !getTwoFactorAuthBasicAuthCredentials().equals(
twoFactorAuthBasicAuthUserName, twoFactorAuthBasicAuthPassword)) {
this.twoFactorAuthBasicAuthManager = new TwoFactorAuthBasicAuthManager(
twoFactorAuthBasicAuthUserName, twoFactorAuthBasicAuthPassword);
this.authManagers.put("twoFactorAuth", twoFactorAuthBasicAuthManager);
}

if (this.authManagers.containsKey("phoneNumberLookup")) {
this.phoneNumberLookupBasicAuthManager =
(PhoneNumberLookupBasicAuthManager) this.authManagers.get("phoneNumberLookup");
}

if (!this.authManagers.containsKey("phoneNumberLookup")
|| !getPhoneNumberLookupBasicAuthCredentials().equals(
phoneNumberLookupBasicAuthUserName, phoneNumberLookupBasicAuthPassword)) {
this.phoneNumberLookupBasicAuthManager = new PhoneNumberLookupBasicAuthManager(
phoneNumberLookupBasicAuthUserName, phoneNumberLookupBasicAuthPassword);
this.authManagers.put("phoneNumberLookup", phoneNumberLookupBasicAuthManager);
}

if (this.authManagers.containsKey("voice")) {
this.voiceBasicAuthManager = (VoiceBasicAuthManager) this.authManagers.get("voice");
}
Expand All @@ -151,10 +172,11 @@ private BandwidthClient(Environment environment, String baseUrl, HttpClient http
}


messagingClient = new MessagingClient(this);
messagingClient = new MessagingClient(this, httpCallback);
twoFactorAuthClient = new TwoFactorAuthClient(this);
voiceClient = new VoiceClient(this);
webRtcClient = new WebRtcClient(this);
phoneNumberLookupClient = new PhoneNumberLookupClient(this, httpCallback);
voiceClient = new VoiceClient(this, httpCallback);
webRtcClient = new WebRtcClient(this, httpCallback);
}

/**
Expand All @@ -180,6 +202,14 @@ public TwoFactorAuthClient getTwoFactorAuthClient() {
return twoFactorAuthClient;
}

/**
* Provides access to phoneNumberLookupClient Client.
* @return Returns the PhoneNumberLookupClient instance
*/
public PhoneNumberLookupClient getPhoneNumberLookupClient() {
return phoneNumberLookupClient;
}

/**
* Provides access to voiceClient Client.
* @return Returns the VoiceClient instance
Expand Down Expand Up @@ -220,14 +250,6 @@ public HttpClient getHttpClient() {
return httpClient;
}

/**
* The timeout to use for making HTTP requests.
* @return timeout
*/
public long getTimeout() {
return timeout;
}

/**
* Http Client Configuration instance.
* @return httpClientConfig
Expand All @@ -252,6 +274,14 @@ public TwoFactorAuthBasicAuthCredentials getTwoFactorAuthBasicAuthCredentials()
return twoFactorAuthBasicAuthManager;
}

/**
* The credentials to use with PhoneNumberLookupBasicAuth.
* @return phoneNumberLookupBasicAuthCredentials
*/
public PhoneNumberLookupBasicAuthCredentials getPhoneNumberLookupBasicAuthCredentials() {
return phoneNumberLookupBasicAuthManager;
}

/**
* The credentials to use with VoiceBasicAuth.
* @return voiceBasicAuthCredentials
Expand All @@ -276,6 +306,26 @@ public Map<String, AuthManager> getAuthManagers() {
return authManagers;
}

/**
* The timeout to use for making HTTP requests.
* @deprecated This method will be removed in a future version. Use
* {@link #getHttpClientConfig()} instead.
*
* @return timeout
*/
@Deprecated
public long timeout() {
return httpClientConfig.getTimeout();
}

/**
* The timeout to use for making HTTP requests.
* @return timeout
*/
public long getTimeout() {
return timeout;
}

/**
* Get base URI by current environment.
* @param server Server for which to get the base URI
Expand Down Expand Up @@ -315,6 +365,9 @@ private static String environmentMapper(Environment environment, Server server)
if (server.equals(Server.TWOFACTORAUTHDEFAULT)) {
return "https://mfa.bandwidth.com/api/v1";
}
if (server.equals(Server.PHONENUMBERLOOKUPDEFAULT)) {
return "https://numbers.bandwidth.com/api/v1";
}
if (server.equals(Server.VOICEDEFAULT)) {
return "https://voice.bandwidth.com";
}
Expand All @@ -332,6 +385,9 @@ private static String environmentMapper(Environment environment, Server server)
if (server.equals(Server.TWOFACTORAUTHDEFAULT)) {
return "{base_url}";
}
if (server.equals(Server.PHONENUMBERLOOKUPDEFAULT)) {
return "{base_url}";
}
if (server.equals(Server.VOICEDEFAULT)) {
return "{base_url}";
}
Expand Down Expand Up @@ -371,20 +427,26 @@ public Builder newBuilder() {
getTwoFactorAuthBasicAuthCredentials().getBasicAuthUserName();
builder.twoFactorAuthBasicAuthPassword =
getTwoFactorAuthBasicAuthCredentials().getBasicAuthPassword();
builder.phoneNumberLookupBasicAuthUserName =
getPhoneNumberLookupBasicAuthCredentials().getBasicAuthUserName();
builder.phoneNumberLookupBasicAuthPassword =
getPhoneNumberLookupBasicAuthCredentials().getBasicAuthPassword();
builder.voiceBasicAuthUserName = getVoiceBasicAuthCredentials().getBasicAuthUserName();
builder.voiceBasicAuthPassword = getVoiceBasicAuthCredentials().getBasicAuthPassword();
builder.webRtcBasicAuthUserName = getWebRtcBasicAuthCredentials().getBasicAuthUserName();
builder.webRtcBasicAuthPassword = getWebRtcBasicAuthCredentials().getBasicAuthPassword();
builder.authManagers = authManagers;
builder.httpCallback = httpCallback;
builder.setHttpClientConfig(httpClientConfig);
builder.httpClientConfig(configBldr -> configBldr =
((HttpClientConfiguration) httpClientConfig).newBuilder());
return builder;
}

/**
* Class to build instances of {@link BandwidthClient}.
*/
public static class Builder {

private Environment environment = Environment.PRODUCTION;
private String baseUrl = "https://www.example.com";
private HttpClient httpClient;
Expand All @@ -393,13 +455,17 @@ public static 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";
private String webRtcBasicAuthPassword = "TODO: Replace";
private Map<String, AuthManager> authManagers = null;
private HttpCallback httpCallback = null;
private HttpClientConfiguration httpClientConfig;
private HttpClientConfiguration.Builder httpClientConfigBuilder =
new HttpClientConfiguration.Builder();


/**
* Credentials setter for MessagingBasicAuth.
Expand Down Expand Up @@ -439,6 +505,25 @@ public Builder twoFactorAuthBasicAuthCredentials(String basicAuthUserName,
return this;
}

/**
* Credentials setter for PhoneNumberLookupBasicAuth.
* @param basicAuthUserName String value for phoneNumberLookupBasicAuthUserName.
* @param basicAuthPassword String value for phoneNumberLookupBasicAuthPassword.
* @return Builder
*/
public Builder phoneNumberLookupBasicAuthCredentials(String basicAuthUserName,
String basicAuthPassword) {
if (basicAuthUserName == null) {
throw new NullPointerException("BasicAuthUserName cannot be null.");
}
if (basicAuthPassword == null) {
throw new NullPointerException("BasicAuthPassword cannot be null.");
}
this.phoneNumberLookupBasicAuthUserName = basicAuthUserName;
this.phoneNumberLookupBasicAuthPassword = basicAuthPassword;
return this;
}

/**
* Credentials setter for VoiceBasicAuth.
* @param basicAuthUserName String value for voiceBasicAuthUserName.
Expand Down Expand Up @@ -499,13 +584,14 @@ public Builder baseUrl(String baseUrl) {

/**
* The timeout to use for making HTTP requests.
* @deprecated This method will be removed in a future version. Use
* {@link #httpClientConfig(Consumer) httpClientConfig} instead.
* @param timeout must be greater then 0.
* @return Builder
*/
@Deprecated
public Builder timeout(long timeout) {
if (timeout > 0) {
this.timeout = timeout;
}
this.httpClientConfigBuilder.timeout(timeout);
return this;
}

Expand All @@ -519,25 +605,32 @@ public Builder httpCallback(HttpCallback httpCallback) {
return this;
}


private void setHttpClientConfig(ReadonlyHttpClientConfiguration httpClientConfig) {
this.timeout = httpClientConfig.getTimeout();
/**
* Setter for the Builder of httpClientConfiguration, takes in an operation to be performed
* on the builder instance of HTTP client configuration.
*
* @param action Consumer for the builder of httpClientConfiguration.
* @return Builder
*/
public Builder httpClientConfig(Consumer<HttpClientConfiguration.Builder> action) {
action.accept(httpClientConfigBuilder);
return this;
}

/**
* Builds a new BandwidthClient object using the set fields.
* @return BandwidthClient
*/
public BandwidthClient build() {
httpClientConfig = new HttpClientConfiguration();
httpClientConfig.setTimeout(timeout);
HttpClientConfiguration httpClientConfig = httpClientConfigBuilder.build();
httpClient = new OkClient(httpClientConfig);

return new BandwidthClient(environment, baseUrl, httpClient, timeout, httpClientConfig,
messagingBasicAuthUserName, messagingBasicAuthPassword,
twoFactorAuthBasicAuthUserName, twoFactorAuthBasicAuthPassword,
phoneNumberLookupBasicAuthUserName, phoneNumberLookupBasicAuthPassword,
voiceBasicAuthUserName, voiceBasicAuthPassword, webRtcBasicAuthUserName,
webRtcBasicAuthPassword, authManagers, httpCallback);
}
}
}
}
19 changes: 13 additions & 6 deletions src/main/java/com/bandwidth/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ public interface Configuration {
String getBaseUrl();

/**
* The timeout to use for making HTTP requests.
* @return a copy of timeout
* Http Client Configuration instance.
* See available [builder methods here](#httpclientconfiguration.builder-class).
* @return a copy of httpClientConfig
*/
long getTimeout();
ReadonlyHttpClientConfiguration getHttpClientConfig();

/**
* Http Client Configuration instance.
* @return a copy of httpClientConfig
* The timeout to use for making HTTP requests. The timeout to use for making HTTP requests.
* @return a copy of timeout
*/
ReadonlyHttpClientConfiguration getHttpClientConfig();
long timeout();

/**
* The credentials to use with MessagingBasicAuth.
Expand All @@ -49,6 +50,12 @@ public interface Configuration {
*/
TwoFactorAuthBasicAuthCredentials getTwoFactorAuthBasicAuthCredentials();

/**
* The credentials to use with PhoneNumberLookupBasicAuth.
* @return phoneNumberLookupBasicAuthCredentials
*/
PhoneNumberLookupBasicAuthCredentials getPhoneNumberLookupBasicAuthCredentials();

/**
* The credentials to use with VoiceBasicAuth.
* @return voiceBasicAuthCredentials
Expand Down
Loading

0 comments on commit d5490c9

Please sign in to comment.