All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bandwidth.BandwidthClient Maven / Gradle / Ivy

Go to download

The official client SDK for Bandwidth's Voice, Messaging, MFA, and WebRTC APIs

There is a newer version: 12.0.0
Show newest version
/*
 * BandwidthLib
 *
 * This file was automatically generated by APIMATIC v3.0 ( https://www.apimatic.io ).
 */

package com.bandwidth;

import com.bandwidth.http.client.HttpCallback;
import com.bandwidth.http.client.HttpClient;
import com.bandwidth.http.client.HttpClientConfiguration;
import com.bandwidth.http.client.OkClient;
import com.bandwidth.http.client.ReadonlyHttpClientConfiguration;
import java.util.AbstractMap.SimpleEntry;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Gateway class for the library.
 * This class acts as a factory for Controllers.
 * It holds the state of the SDK.
 */
public final class BandwidthClient implements Configuration {

    /**
     * Private store for clients.
     */
    private MessagingClient messagingClient;
    private MultiFactorAuthClient multiFactorAuthClient;
    private PhoneNumberLookupClient phoneNumberLookupClient;
    private VoiceClient voiceClient;
    private WebRtcClient webRtcClient;

    /**
     * Current API environment.
     */
    private final Environment environment;

    /**
     * baseUrl value.
     */
    private final String baseUrl;

    /**
     * The HTTP Client instance to use for making HTTP requests.
     */
    private final HttpClient httpClient;

    /**
     * Http Client Configuration instance.
     */
    private final ReadonlyHttpClientConfiguration httpClientConfig;

    /**
     * MessagingBasicAuthManager.
     */
    private MessagingBasicAuthManager messagingBasicAuthManager;

    /**
     * MultiFactorAuthBasicAuthManager.
     */
    private MultiFactorAuthBasicAuthManager multiFactorAuthBasicAuthManager;

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

    /**
     * VoiceBasicAuthManager.
     */
    private VoiceBasicAuthManager voiceBasicAuthManager;

    /**
     * WebRtcBasicAuthManager.
     */
    private WebRtcBasicAuthManager webRtcBasicAuthManager;

    /**
     * Map of authentication Managers.
     */
    private Map authManagers;

    /**
     * Callback to be called before and after the HTTP call for an endpoint is made.
     */
    private final HttpCallback httpCallback;

    private BandwidthClient(Environment environment, String baseUrl, HttpClient httpClient,
            ReadonlyHttpClientConfiguration httpClientConfig, String messagingBasicAuthUserName,
            String messagingBasicAuthPassword, String multiFactorAuthBasicAuthUserName,
            String multiFactorAuthBasicAuthPassword, String phoneNumberLookupBasicAuthUserName,
            String phoneNumberLookupBasicAuthPassword, String voiceBasicAuthUserName,
            String voiceBasicAuthPassword, String webRtcBasicAuthUserName,
            String webRtcBasicAuthPassword, Map authManagers,
            HttpCallback httpCallback) {
        this.environment = environment;
        this.baseUrl = baseUrl;
        this.httpClient = httpClient;
        this.httpClientConfig = httpClientConfig;
        this.httpCallback = httpCallback;

        this.authManagers = (authManagers == null) ? new HashMap<>() : new HashMap<>(authManagers);
        if (this.authManagers.containsKey("messaging")) {
            this.messagingBasicAuthManager =
                    (MessagingBasicAuthManager) this.authManagers.get("messaging");
        }

        if (!this.authManagers.containsKey("messaging")
                || !getMessagingBasicAuthCredentials().equals(messagingBasicAuthUserName,
                        messagingBasicAuthPassword)) {
            this.messagingBasicAuthManager = new MessagingBasicAuthManager(
                    messagingBasicAuthUserName, messagingBasicAuthPassword);
            this.authManagers.put("messaging", messagingBasicAuthManager);
        }

        if (this.authManagers.containsKey("multiFactorAuth")) {
            this.multiFactorAuthBasicAuthManager =
                    (MultiFactorAuthBasicAuthManager) this.authManagers.get("multiFactorAuth");
        }

        if (!this.authManagers.containsKey("multiFactorAuth")
                || !getMultiFactorAuthBasicAuthCredentials().equals(
                        multiFactorAuthBasicAuthUserName, multiFactorAuthBasicAuthPassword)) {
            this.multiFactorAuthBasicAuthManager = new MultiFactorAuthBasicAuthManager(
                    multiFactorAuthBasicAuthUserName, multiFactorAuthBasicAuthPassword);
            this.authManagers.put("multiFactorAuth", multiFactorAuthBasicAuthManager);
        }

        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");
        }

        if (!this.authManagers.containsKey("voice")
                || !getVoiceBasicAuthCredentials().equals(voiceBasicAuthUserName,
                        voiceBasicAuthPassword)) {
            this.voiceBasicAuthManager = new VoiceBasicAuthManager(voiceBasicAuthUserName,
                    voiceBasicAuthPassword);
            this.authManagers.put("voice", voiceBasicAuthManager);
        }

        if (this.authManagers.containsKey("webRtc")) {
            this.webRtcBasicAuthManager = (WebRtcBasicAuthManager) this.authManagers.get("webRtc");
        }

        if (!this.authManagers.containsKey("webRtc")
                || !getWebRtcBasicAuthCredentials().equals(webRtcBasicAuthUserName,
                        webRtcBasicAuthPassword)) {
            this.webRtcBasicAuthManager = new WebRtcBasicAuthManager(webRtcBasicAuthUserName,
                    webRtcBasicAuthPassword);
            this.authManagers.put("webRtc", webRtcBasicAuthManager);
        }


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

    /**
     * Shutdown the underlying HttpClient instance.
     */
    public static void shutdown() {
        OkClient.shutdown();
    }

    /**
     * Provides access to messagingClient Client.
     * @return Returns the MessagingClient instance
     */
    public MessagingClient getMessagingClient() {
        return messagingClient;
    }

    /**
     * Provides access to multiFactorAuthClient Client.
     * @return Returns the MultiFactorAuthClient instance
     */
    public MultiFactorAuthClient getMultiFactorAuthClient() {
        return multiFactorAuthClient;
    }

    /**
     * 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
     */
    public VoiceClient getVoiceClient() {
        return voiceClient;
    }

    /**
     * Provides access to webRtcClient Client.
     * @return Returns the WebRtcClient instance
     */
    public WebRtcClient getWebRtcClient() {
        return webRtcClient;
    }

    /**
     * Current API environment.
     * @return environment
     */
    public Environment getEnvironment() {
        return environment;
    }

    /**
     * baseUrl value.
     * @return baseUrl
     */
    public String getBaseUrl() {
        return baseUrl;
    }

    /**
     * The HTTP Client instance to use for making HTTP requests.
     * @return httpClient
     */
    public HttpClient getHttpClient() {
        return httpClient;
    }

    /**
     * Http Client Configuration instance.
     * @return httpClientConfig
     */
    public ReadonlyHttpClientConfiguration getHttpClientConfig() {
        return httpClientConfig;
    }

    /**
     * The credentials to use with MessagingBasicAuth.
     * @return messagingBasicAuthCredentials
     */
    public MessagingBasicAuthCredentials getMessagingBasicAuthCredentials() {
        return messagingBasicAuthManager;
    }

    /**
     * The credentials to use with MultiFactorAuthBasicAuth.
     * @return multiFactorAuthBasicAuthCredentials
     */
    public MultiFactorAuthBasicAuthCredentials getMultiFactorAuthBasicAuthCredentials() {
        return multiFactorAuthBasicAuthManager;
    }

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

    /**
     * The credentials to use with VoiceBasicAuth.
     * @return voiceBasicAuthCredentials
     */
    public VoiceBasicAuthCredentials getVoiceBasicAuthCredentials() {
        return voiceBasicAuthManager;
    }

    /**
     * The credentials to use with WebRtcBasicAuth.
     * @return webRtcBasicAuthCredentials
     */
    public WebRtcBasicAuthCredentials getWebRtcBasicAuthCredentials() {
        return webRtcBasicAuthManager;
    }

    /**
     * The list of auth managers.
     * @return authManagers
     */
    public Map 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();
    }

    /**
     * Get base URI by current environment.
     * @param server Server for which to get the base URI
     * @return Processed base URI
     */
    public String getBaseUri(Server server) {
        Map> parameters = new HashMap<>();
        parameters.put("base_url",
                new SimpleEntry(this.baseUrl, false));
        StringBuilder baseUrl = new StringBuilder(environmentMapper(environment, server));
        ApiHelper.appendUrlWithTemplateParameters(baseUrl, parameters);
        return baseUrl.toString();
    }

    /**
     * Get base URI by current environment.
     * @return Processed base URI
     */
    public String getBaseUri() {
        return getBaseUri(Server.ENUM_DEFAULT);
    }

    /**
     * Base URLs by environment and server aliases.
     * @param environment Environment for which to get the base URI
     * @param server Server for which to get the base URI
     * @return base URL
     */
    private static String environmentMapper(Environment environment, Server server) {
        if (environment.equals(Environment.PRODUCTION)) {
            if (server.equals(Server.ENUM_DEFAULT)) {
                return "api.bandwidth.com";
            }
            if (server.equals(Server.MESSAGINGDEFAULT)) {
                return "https://messaging.bandwidth.com/api/v2";
            }
            if (server.equals(Server.MULTIFACTORAUTHDEFAULT)) {
                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";
            }
            if (server.equals(Server.WEBRTCDEFAULT)) {
                return "https://api.webrtc.bandwidth.com/v1";
            }
        }
        if (environment.equals(Environment.CUSTOM)) {
            if (server.equals(Server.ENUM_DEFAULT)) {
                return "{base_url}";
            }
            if (server.equals(Server.MESSAGINGDEFAULT)) {
                return "{base_url}";
            }
            if (server.equals(Server.MULTIFACTORAUTHDEFAULT)) {
                return "{base_url}";
            }
            if (server.equals(Server.PHONENUMBERLOOKUPDEFAULT)) {
                return "{base_url}";
            }
            if (server.equals(Server.VOICEDEFAULT)) {
                return "{base_url}";
            }
            if (server.equals(Server.WEBRTCDEFAULT)) {
                return "{base_url}";
            }
        }
        return "api.bandwidth.com";
    }

    /**
     * Converts this BandwidthClient into string format.
     * @return String representation of this class
     */
    @Override
    public String toString() {
        return "BandwidthClient [" + "environment=" + environment + ", baseUrl=" + baseUrl
                + ", httpClientConfig=" + httpClientConfig + ", authManagers=" + authManagers + "]";
    }

    /**
     * Builds a new {@link BandwidthClient.Builder} object.
     * Creates the instance with the state of the current client.
     * @return a new {@link BandwidthClient.Builder} object
     */
    public Builder newBuilder() {
        Builder builder = new Builder();
        builder.environment = getEnvironment();
        builder.baseUrl = getBaseUrl();
        builder.httpClient = getHttpClient();
        builder.messagingBasicAuthUserName =
                getMessagingBasicAuthCredentials().getBasicAuthUserName();
        builder.messagingBasicAuthPassword =
                getMessagingBasicAuthCredentials().getBasicAuthPassword();
        builder.multiFactorAuthBasicAuthUserName =
                getMultiFactorAuthBasicAuthCredentials().getBasicAuthUserName();
        builder.multiFactorAuthBasicAuthPassword =
                getMultiFactorAuthBasicAuthCredentials().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.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;
        private String messagingBasicAuthUserName = "TODO: Replace";
        private String messagingBasicAuthPassword = "TODO: Replace";
        private String multiFactorAuthBasicAuthUserName = "TODO: Replace";
        private String multiFactorAuthBasicAuthPassword = "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 authManagers = null;
        private HttpCallback httpCallback = null;
        private HttpClientConfiguration.Builder httpClientConfigBuilder =
                new HttpClientConfiguration.Builder();


        /**
         * Credentials setter for MessagingBasicAuth.
         * @param basicAuthUserName String value for messagingBasicAuthUserName.
         * @param basicAuthPassword String value for messagingBasicAuthPassword.
         * @return Builder
         */
        public Builder messagingBasicAuthCredentials(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.messagingBasicAuthUserName = basicAuthUserName;
            this.messagingBasicAuthPassword = basicAuthPassword;
            return this;
        }

        /**
         * Credentials setter for MultiFactorAuthBasicAuth.
         * @param basicAuthUserName String value for multiFactorAuthBasicAuthUserName.
         * @param basicAuthPassword String value for multiFactorAuthBasicAuthPassword.
         * @return Builder
         */
        public Builder multiFactorAuthBasicAuthCredentials(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.multiFactorAuthBasicAuthUserName = basicAuthUserName;
            this.multiFactorAuthBasicAuthPassword = basicAuthPassword;
            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.
         * @param basicAuthPassword String value for voiceBasicAuthPassword.
         * @return Builder
         */
        public Builder voiceBasicAuthCredentials(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.voiceBasicAuthUserName = basicAuthUserName;
            this.voiceBasicAuthPassword = basicAuthPassword;
            return this;
        }

        /**
         * Credentials setter for WebRtcBasicAuth.
         * @param basicAuthUserName String value for webRtcBasicAuthUserName.
         * @param basicAuthPassword String value for webRtcBasicAuthPassword.
         * @return Builder
         */
        public Builder webRtcBasicAuthCredentials(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.webRtcBasicAuthUserName = basicAuthUserName;
            this.webRtcBasicAuthPassword = basicAuthPassword;
            return this;
        }

        /**
         * Current API environment.
         * @param environment The environment for client.
         * @return Builder
         */
        public Builder environment(Environment environment) {
            this.environment = environment;
            return this;
        }

        /**
         * baseUrl value.
         * @param baseUrl The baseUrl for client.
         * @return Builder
         */
        public Builder baseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
            return this;
        }

        /**
         * 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) {
            this.httpClientConfigBuilder.timeout(timeout);
            return this;
        }

        /**
         * HttpCallback.
         * @param httpCallback Callback to be called before and after the HTTP call.
         * @return Builder
         */
        public Builder httpCallback(HttpCallback httpCallback) {
            this.httpCallback = httpCallback;
            return this;
        }

        /**
         * 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 action) {
            action.accept(httpClientConfigBuilder);
            return this;
        }

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

            return new BandwidthClient(environment, baseUrl, httpClient, httpClientConfig,
                    messagingBasicAuthUserName, messagingBasicAuthPassword,
                    multiFactorAuthBasicAuthUserName, multiFactorAuthBasicAuthPassword,
                    phoneNumberLookupBasicAuthUserName, phoneNumberLookupBasicAuthPassword,
                    voiceBasicAuthUserName, voiceBasicAuthPassword, webRtcBasicAuthUserName,
                    webRtcBasicAuthPassword, authManagers, httpCallback);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy