Class OkHttpHttpClientBuilder

java.lang.Object
io.clientcore.http.okhttp3.OkHttpHttpClientBuilder

public class OkHttpHttpClientBuilder extends Object
Builder class responsible for creating instances of HttpClient backed by OkHttp.
  • Constructor Details

    • OkHttpHttpClientBuilder

      public OkHttpHttpClientBuilder()
      Creates OkHttpHttpClientBuilder.
    • OkHttpHttpClientBuilder

      public OkHttpHttpClientBuilder(okhttp3.OkHttpClient okHttpClient)
      Creates OkHttpHttpClientBuilder from the builder of an existing OkHttpClient.
      Parameters:
      okHttpClient - the httpclient
  • Method Details

    • followRedirects

      public OkHttpHttpClientBuilder followRedirects(boolean followRedirects)

      Sets the followRedirect flag on the underlying OkHttp-backed HttpClient.

      If this is set to 'true' redirects will be followed automatically, and if your HTTP pipeline is configured with a redirect policy it will not be called.

      Parameters:
      followRedirects - The followRedirects value to use.
      Returns:
      The updated OkHttpHttpClientBuilder object.
    • configuration

      public OkHttpHttpClientBuilder configuration(Configuration configuration)
      Sets the configuration store that is used during construction of the HTTP client.

      The default configuration store is a clone of the global configuration store.

      Parameters:
      configuration - The configuration store.
      Returns:
      The updated OkHttpHttpClientBuilder object.
    • connectionPool

      public OkHttpHttpClientBuilder connectionPool(okhttp3.ConnectionPool connectionPool)
      Sets the Http connection pool.
      Parameters:
      connectionPool - The OkHttp connection pool to use.
      Returns:
      The updated OkHttpHttpClientBuilder object.
      See Also:
      • OkHttpClient.Builder.connectionPool(ConnectionPool)
    • dispatcher

      public OkHttpHttpClientBuilder dispatcher(okhttp3.Dispatcher dispatcher)
      Sets the dispatcher that also composes the thread pool for executing HTTP requests.

      If this method is not invoked prior to building, handling for a default will be based on whether the builder was created with the default constructor or the constructor that accepts an existing OkHttpClient. If the default constructor was used, a new Dispatcher will be created with based on SharedExecutorService.getInstance()}. If the constructor that accepts an existing OkHttpClient was used, the dispatcher from the existing OkHttpClient will be used.

      Parameters:
      dispatcher - The dispatcher to use.
      Returns:
      The updated OkHttpHttpClientBuilder object.
      See Also:
      • OkHttpClient.Builder.dispatcher(Dispatcher)
    • connectionTimeout

      public OkHttpHttpClientBuilder connectionTimeout(Duration connectionTimeout)
      Sets the connection timeout for a request to be sent.

      The connection timeout begins once the request attempts to connect to the remote host and finishes once the connection is resolved.

      If connectTimeout is null either Configuration.REQUEST_CONNECT_TIMEOUT_IN_MS or a 10-second timeout will be used, if it is a Duration less than or equal to zero then no timeout will be applied. When applying the timeout the greatest of one millisecond and the value of connectTimeout will be used.

      By default, the connection timeout is 10 seconds.

      Parameters:
      connectionTimeout - Connect timeout duration.
      Returns:
      The updated OkHttpHttpClientBuilder object.
      See Also:
      • OkHttpClient.Builder.connectTimeout(Duration)
    • callTimeout

      public OkHttpHttpClientBuilder callTimeout(Duration callTimeout)
      Sets the default timeout for complete calls.

      The call timeout spans the entire call: resolving DNS, connecting, writing the request body, server processing, and reading the response body.

      Null or Duration.ZERO means no call timeout, otherwise values must be between 1 and Integer.MAX_VALUE when converted to milliseconds.

      By default, call timeout is not enabled.

      Parameters:
      callTimeout - Call timeout duration.
      Returns:
      The updated OkHttpHttpClientBuilder object.
      See Also:
      • OkHttpClient.Builder.callTimeout(Duration)
    • readTimeout

      public OkHttpHttpClientBuilder readTimeout(Duration readTimeout)
      Sets the read timeout duration used when reading the server response.

      The read timeout begins once the first response read is triggered after the server response is received. This timeout triggers periodically but won't fire its operation if another read operation has completed between when the timeout is triggered and completes.

      If readTimeout is null or Configuration.REQUEST_READ_TIMEOUT_IN_MS or a 60-second timeout will be used, if it is a Duration less than or equal to zero then no timeout period will be applied to response read. When applying the timeout the greatest of one millisecond and the value of readTimeout will be used.

      Parameters:
      readTimeout - Read timeout duration.
      Returns:
      The updated OkHttpHttpClientBuilder object.
      See Also:
      • OkHttpClient.Builder.readTimeout(Duration)
    • writeTimeout

      public OkHttpHttpClientBuilder writeTimeout(Duration writeTimeout)
      Sets the writing timeout for a request to be sent.

      The writing timeout does not apply to the entire request but to the request being sent over the wire. For example a request body which emits 10 8KB buffers will trigger 10 write operations, the last write tracker will update when each operation completes and the outbound buffer will be periodically checked to determine if it is still draining.

      If writeTimeout is null either Configuration.REQUEST_WRITE_TIMEOUT_IN_MS or a 60-second timeout will be used, if it is a Duration less than or equal to zero then no write timeout will be applied. When applying the timeout the greatest of one millisecond and the value of writeTimeout will be used.

      Parameters:
      writeTimeout - Write operation timeout duration.
      Returns:
      The updated OkHttpHttpClientBuilder object.
      See Also:
      • OkHttpClient.Builder.writeTimeout(Duration)
    • addNetworkInterceptor

      public OkHttpHttpClientBuilder addNetworkInterceptor(okhttp3.Interceptor networkInterceptor)
      Add a network layer interceptor to Http request pipeline.
      Parameters:
      networkInterceptor - the interceptor to add
      Returns:
      The updated OkHttpHttpClientBuilder object
    • networkInterceptors

      public OkHttpHttpClientBuilder networkInterceptors(List<okhttp3.Interceptor> networkInterceptors)
      Add network layer interceptors to Http request pipeline.

      This replaces all previously-set interceptors.

      Parameters:
      networkInterceptors - The interceptors to add.
      Returns:
      The updated OkHttpHttpClientBuilder object.
    • proxy

      public OkHttpHttpClientBuilder proxy(ProxyOptions proxyOptions)
      Sets the proxy.
      Parameters:
      proxyOptions - The proxy configuration to use.
      Returns:
      The updated OkHttpHttpClientBuilder object.
    • sslSocketFactory

      public OkHttpHttpClientBuilder sslSocketFactory(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager)
      Sets the SSLSocketFactory to use for HTTPS connections.

      If left unset, or set to null, HTTPS connections will use the default SSL socket factory (SSLSocketFactory.getDefault()).

      Parameters:
      sslSocketFactory - The SSLSocketFactory to use for HTTPS connections.
      trustManager - The X509TrustManager to use for HTTPS connections.
      Returns:
      The updated OkHttpHttpClientBuilder object.
    • hostnameVerifier

      public OkHttpHttpClientBuilder hostnameVerifier(HostnameVerifier hostnameVerifier)
      Sets the HostnameVerifier to use for HTTPS connections.

      If left unset, or set to null, HTTPS connections will use a default hostname verifier.

      Parameters:
      hostnameVerifier - The HostnameVerifier to use for HTTPS connections.
      Returns:
      The updated OkHttpHttpClientBuilder object.
    • build

      public HttpClient build()
      Creates a new OkHttp-backed HttpClient instance on every call, using the configuration set in this builder at the time of the build() method call.
      Returns:
      A new OkHttp-backed HttpClient instance.