Class OkHttpClientBuilder

java.lang.Object
com.amilesend.client.connection.http.OkHttpClientBuilder

public class OkHttpClientBuilder extends Object
Utility to configure and build a OkHttpClient with the option of default configuration for this client. This supports configuring a client that:
  • Customizes the SSL trust manager and hostname verifier
  • Configures a proxy with username and password
  • Configures support to follow redirects
  • Configures connection and read/write timeouts (default is disabled)
  • Constructor Details

    • OkHttpClientBuilder

      public OkHttpClientBuilder()
  • Method Details

    • trustManager

      public OkHttpClientBuilder trustManager(X509TrustManager trustManager)
      Sets the SSL/TLS trust manager to use with the HTTP client.
      Parameters:
      trustManager - the trust manager
      Returns:
      the builder instance
      See Also:
    • hostnameVerifier

      public OkHttpClientBuilder hostnameVerifier(HostnameVerifier hostnameVerifier)
      Sets the hostname verifier to use with the HTTP client.
      Parameters:
      hostnameVerifier - the trust manager
      Returns:
      the builder instance
      See Also:
    • proxy

      public OkHttpClientBuilder proxy(Proxy proxy, String username, String password)
      Sets the proxy and associated username plus password to use with the HTTP client. Note: if username and password is undefined, then no credential-based authentication will be configured.
      Parameters:
      proxy - the proxy settings
      username - the username
      password - the password
      Returns:
      the builder instance
    • proxy

      public OkHttpClientBuilder proxy(Proxy proxy, okhttp3.Authenticator proxyAuthenticator)
      Sets the proxy and associated authenticator to use with the HTTP client.
      Parameters:
      proxy - the proxy settings
      proxyAuthenticator - the proxy authenticator
      Returns:
      the builder instance
    • connectTimeout

      public OkHttpClientBuilder connectTimeout(Duration connectTimeout)
      Sets the connection timeout for the HTTP client.
      Parameters:
      connectTimeout - the connection timeout
      Returns:
      the builder instance
    • readTimeout

      public OkHttpClientBuilder readTimeout(Duration readTimeout)
      Sets the read timeout for the HTTP client.
      Parameters:
      readTimeout - the read timeout
      Returns:
      the builder instance
    • writeTimeout

      public OkHttpClientBuilder writeTimeout(Duration writeTimeout)
      Sets the write timeout for the HTTP client.
      Parameters:
      writeTimeout - the write timeout.
      Returns:
      the builder instance
    • isRedirectsAllowed

      public OkHttpClientBuilder isRedirectsAllowed(boolean isRedirectsAllowed)
      Sets the flag to allow for automatic URL redirects when responses return 300-based HTTP responses.
      Parameters:
      isRedirectsAllowed - If true, then the client will automatically invoke the redirected URL; else, false
      Returns:
      the builder instance
    • isForTest

      public OkHttpClientBuilder isForTest(boolean isForTest)
      Sets the flag to allow for non-SSL/TLS based requests used for testing. Note: Should not be set for normal use with real calls to the Graph API.
      Parameters:
      isForTest - If true, then the client will allow HTTP-based invocations; else, false.
      Returns:
      the builder instance
    • addInterceptor

      public OkHttpClientBuilder addInterceptor(okhttp3.Interceptor interceptor)
      Adds an interceptor for the client.
      Parameters:
      interceptor - the interceptor
      Returns:
      the builder instance
    • build

      public okhttp3.OkHttpClient build()
      Builds a new OkHttpClient instance.
      Returns:
      the configured HTTP client