Class RestClient.Builder

  • Enclosing class:
    RestClient

    public static class RestClient.Builder
    extends Object
    The builder class for building a REST client.
    • Constructor Detail

      • Builder

        public Builder()
        Creates an instance of the builder with a base URL to the service.
      • Builder

        public Builder​(okhttp3.OkHttpClient.Builder httpClientBuilder,
                       retrofit2.Retrofit.Builder retrofitBuilder)
        Creates an instance of the builder with a base URL and 2 custom builders.
        Parameters:
        httpClientBuilder - the builder to build an OkHttpClient.
        retrofitBuilder - the builder to build a Retrofit.
    • Method Detail

      • withBaseUrl

        public RestClient.Builder withBaseUrl​(String baseUrl)
        Sets the dynamic base URL.
        Parameters:
        baseUrl - the base URL to use.
        Returns:
        the builder itself for chaining.
      • withBaseUrl

        public RestClient.Builder withBaseUrl​(Environment environment,
                                              Environment.Endpoint endpoint)
        Sets the base URL with the default from the Environment.
        Parameters:
        environment - the environment to use
        endpoint - the environment endpoint the application is accessing
        Returns:
        the builder itself for chaining
      • withSerializerAdapter

        public RestClient.Builder withSerializerAdapter​(SerializerAdapter<?> serializerAdapter)
        Sets the serialization adapter.
        Parameters:
        serializerAdapter - the adapter to a serializer
        Returns:
        the builder itself for chaining
      • withResponseBuilderFactory

        public RestClient.Builder withResponseBuilderFactory​(ResponseBuilder.Factory responseBuilderFactory)
        Sets the response builder factory.
        Parameters:
        responseBuilderFactory - the response builder factory
        Returns:
        the builder itself for chaining
      • withCredentials

        public RestClient.Builder withCredentials​(ServiceClientCredentials credentials)
        Sets the credentials.
        Parameters:
        credentials - the credentials object.
        Returns:
        the builder itself for chaining.
      • withUserAgent

        public RestClient.Builder withUserAgent​(String userAgent)
        Sets the user agent header.
        Parameters:
        userAgent - the user agent header.
        Returns:
        the builder itself for chaining.
      • withLogLevel

        public RestClient.Builder withLogLevel​(LogLevel logLevel)
        Sets the HTTP log level.
        Parameters:
        logLevel - the LogLevel enum.
        Returns:
        the builder itself for chaining.
      • withInterceptor

        public RestClient.Builder withInterceptor​(okhttp3.Interceptor interceptor)
        Add an interceptor the Http client pipeline.
        Parameters:
        interceptor - the interceptor to add.
        Returns:
        the builder itself for chaining.
      • withNetworkInterceptor

        public RestClient.Builder withNetworkInterceptor​(okhttp3.Interceptor networkInterceptor)
        Add an interceptor the network layer of Http client pipeline.
        Parameters:
        networkInterceptor - the interceptor to add.
        Returns:
        the builder itself for chaining.
      • withReadTimeout

        public RestClient.Builder withReadTimeout​(long timeout,
                                                  TimeUnit unit)
        Set the read timeout on the HTTP client. Default is 10 seconds.
        Parameters:
        timeout - the timeout numeric value
        unit - the time unit for the numeric value
        Returns:
        the builder itself for chaining
      • withConnectionTimeout

        public RestClient.Builder withConnectionTimeout​(long timeout,
                                                        TimeUnit unit)
        Set the connection timeout on the HTTP client. Default is 10 seconds.
        Parameters:
        timeout - the timeout numeric value
        unit - the time unit for the numeric value
        Returns:
        the builder itself for chaining
      • withMaxIdleConnections

        @Deprecated
        public RestClient.Builder withMaxIdleConnections​(int maxIdleConnections)
        Deprecated.
        Set the maximum idle connections for the HTTP client. Default is 5.
        Parameters:
        maxIdleConnections - the maximum idle connections
        Returns:
        the builder itself for chaining
      • withConnectionPool

        public RestClient.Builder withConnectionPool​(okhttp3.ConnectionPool connectionPool)
        Sets the connection pool for the Http client.
        Parameters:
        connectionPool - the OkHttp 3 connection pool to use
        Returns:
        the builder itself for chaining
      • useHttpClientThreadPool

        public RestClient.Builder useHttpClientThreadPool​(boolean useHttpClientThreadPool)
        Sets whether to use the thread pool in OkHttp client or RxJava schedulers. If set to true, the thread pool in OkHttp client will be used. Default is false.
        Parameters:
        useHttpClientThreadPool - whether to use the thread pool in Okhttp client. Default is false.
        Returns:
        the builder itself for chaining
      • withDispatcher

        public RestClient.Builder withDispatcher​(okhttp3.Dispatcher dispatcher)
        Sets the dispatcher used in OkHttp client. This is also where to set the thread pool for executing HTTP requests.
        Parameters:
        dispatcher - the dispatcher to use
        Returns:
        the builder itself for chaining
      • withCallbackExecutor

        public RestClient.Builder withCallbackExecutor​(Executor executor)
        Sets the executor for async callbacks to run on.
        Parameters:
        executor - the executor to execute the callbacks.
        Returns:
        the builder itself for chaining
      • withProxy

        public RestClient.Builder withProxy​(Proxy proxy)
        Sets the proxy for the HTTP client.
        Parameters:
        proxy - the proxy to use
        Returns:
        the builder itself for chaining
      • withProxyAuthenticator

        public RestClient.Builder withProxyAuthenticator​(okhttp3.Authenticator proxyAuthenticator)
        Sets the proxy authenticator for the HTTP client.
        Parameters:
        proxyAuthenticator - the proxy authenticator to use
        Returns:
        the builder itself for chaining
      • withRetryStrategy

        public RestClient.Builder withRetryStrategy​(RetryStrategy strategy)
        Adds a retry strategy to the client.
        Parameters:
        strategy - the retry strategy to add
        Returns:
        the builder itself for chaining
      • build

        public RestClient build()
        Build a RestClient with all the current configurations.
        Returns:
        a RestClient.