Class ApiClient


  • public class ApiClient
    extends java.lang.Object
    Configuration and utility class for API clients.

    This class can be constructed and modified, then used to instantiate the various API classes. The API classes use the settings in this class to configure themselves, but otherwise do not store a link to this class.

    This class is mutable and not synchronized, so it is not thread-safe. The API classes generated from this are immutable and thread-safe.

    The setter methods of this class return the current object to facilitate a fluent style of configuration.

    • Constructor Summary

      Constructors 
      Constructor Description
      ApiClient()
      Create an instance of ApiClient.
      ApiClient​(java.net.http.HttpClient.Builder builder, com.fasterxml.jackson.databind.ObjectMapper mapper)
      Create an instance of ApiClient.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addRequestInterceptor​(java.util.function.Consumer<java.net.http.HttpRequest.Builder> interceptor)
      Add a custom request interceptor.
      protected java.net.http.HttpClient.Builder createDefaultHttpClientBuilder()  
      protected com.fasterxml.jackson.databind.ObjectMapper createDefaultObjectMapper()  
      static java.net.http.HttpRequest.Builder formRequestBuilder​(java.lang.String method, java.lang.String path, java.lang.String body, Configuration configuration)
      Creates a HttpRequest.Builder for a x-www-form-urlencoded request.
      java.util.function.Consumer<java.net.http.HttpResponse<java.lang.String>> getAsyncResponseInterceptor()
      Get the custom async response interceptor.
      protected java.lang.String getDefaultBaseUri()  
      java.net.http.HttpClient getHttpClient()
      Get an HttpClient based on the current HttpClient.Builder.
      java.net.http.HttpClient.Builder getHttpClientBuilder()
      Get the current HttpClient.Builder.
      com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      Get a copy of the current ObjectMapper.
      java.util.function.Consumer<java.net.http.HttpRequest.Builder> getRequestInterceptor()
      Get the custom interceptor.
      java.util.function.Consumer<java.net.http.HttpResponse<java.io.InputStream>> getResponseInterceptor()
      Get the custom response interceptor.
      static java.net.http.HttpRequest.Builder requestBuilder​(java.lang.String method, java.lang.String path, byte[] body, Configuration configuration)  
      static java.net.http.HttpRequest.Builder requestBuilder​(java.lang.String method, java.lang.String path, Configuration configuration)  
      ApiClient setAsyncResponseInterceptor​(java.util.function.Consumer<java.net.http.HttpResponse<java.lang.String>> interceptor)
      Set a custom async response interceptor.
      ApiClient setHttpClientBuilder​(java.net.http.HttpClient.Builder builder)
      Set a custom HttpClient.Builder object to use when creating the HttpClient that is used by the API client.
      ApiClient setObjectMapper​(com.fasterxml.jackson.databind.ObjectMapper mapper)
      Set a custom ObjectMapper to serialize and deserialize the request and response bodies.
      ApiClient setRequestInterceptor​(java.util.function.Consumer<java.net.http.HttpRequest.Builder> interceptor)
      Set a custom request interceptor.
      ApiClient setResponseInterceptor​(java.util.function.Consumer<java.net.http.HttpResponse<java.io.InputStream>> interceptor)
      Set a custom response interceptor.
      static java.lang.String urlEncode​(java.lang.String s)
      URL encode a string in the UTF-8 encoding.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ApiClient

        public ApiClient()
        Create an instance of ApiClient.
      • ApiClient

        public ApiClient​(java.net.http.HttpClient.Builder builder,
                         com.fasterxml.jackson.databind.ObjectMapper mapper)
        Create an instance of ApiClient.

        In other contexts, note that any settings in a Configuration will take precedence over equivalent settings in the HttpClient.Builder here.

        Parameters:
        builder - Http client builder.
        mapper - Object mapper.
    • Method Detail

      • formRequestBuilder

        public static java.net.http.HttpRequest.Builder formRequestBuilder​(java.lang.String method,
                                                                           java.lang.String path,
                                                                           java.lang.String body,
                                                                           Configuration configuration)
                                                                    throws FgaInvalidParameterException
        Creates a HttpRequest.Builder for a x-www-form-urlencoded request.
        Parameters:
        method - the HTTP method to be make.
        path - the URL path.
        body - the request body. It must be URL-encoded.
        configuration - the client configuration.
        Returns:
        a configured builder.
        Throws:
        FgaInvalidParameterException
      • urlEncode

        public static java.lang.String urlEncode​(java.lang.String s)
        URL encode a string in the UTF-8 encoding.
        Parameters:
        s - String to encode.
        Returns:
        URL-encoded representation of the input string.
      • createDefaultObjectMapper

        protected com.fasterxml.jackson.databind.ObjectMapper createDefaultObjectMapper()
      • getDefaultBaseUri

        protected java.lang.String getDefaultBaseUri()
      • createDefaultHttpClientBuilder

        protected java.net.http.HttpClient.Builder createDefaultHttpClientBuilder()
      • setHttpClientBuilder

        public ApiClient setHttpClientBuilder​(java.net.http.HttpClient.Builder builder)
        Set a custom HttpClient.Builder object to use when creating the HttpClient that is used by the API client.

        In other contexts, note that any settings in a Configuration will take precedence over equivalent settings in the HttpClient.Builder here.

        Parameters:
        builder - Custom client builder.
        Returns:
        This object.
      • getHttpClient

        public java.net.http.HttpClient getHttpClient()
        Get an HttpClient based on the current HttpClient.Builder.

        The returned object is immutable and thread-safe.

        Returns:
        The HTTP client.
      • getHttpClientBuilder

        public java.net.http.HttpClient.Builder getHttpClientBuilder()
        Get the current HttpClient.Builder.

        The returned object is immutable and thread-safe.

        Returns:
        The HTTP client.
      • setObjectMapper

        public ApiClient setObjectMapper​(com.fasterxml.jackson.databind.ObjectMapper mapper)
        Set a custom ObjectMapper to serialize and deserialize the request and response bodies.
        Parameters:
        mapper - Custom object mapper.
        Returns:
        This object.
      • getObjectMapper

        public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
        Get a copy of the current ObjectMapper.
        Returns:
        A copy of the current object mapper.
      • setRequestInterceptor

        public ApiClient setRequestInterceptor​(java.util.function.Consumer<java.net.http.HttpRequest.Builder> interceptor)
        Set a custom request interceptor.

        A request interceptor is a mechanism for altering each request before it is sent. After the request has been fully configured but not yet built, the request builder is passed into this function for further modification, after which it is sent out.

        This is useful for altering the requests in a custom manner, such as adding headers. It could also be used for logging and monitoring.

        Parameters:
        interceptor - A function invoked before creating each request. A value of null resets the interceptor to a no-op.
        Returns:
        This object.
      • addRequestInterceptor

        public void addRequestInterceptor​(java.util.function.Consumer<java.net.http.HttpRequest.Builder> interceptor)
        Add a custom request interceptor. This interceptor will be run after any other interceptor(s) already in place.

        For details on request interceptors, see setRequestInterceptor(Consumer)

        Parameters:
        interceptor - A function invoked before creating each request. A value of null resets the interceptor to a no-op.
      • getRequestInterceptor

        public java.util.function.Consumer<java.net.http.HttpRequest.Builder> getRequestInterceptor()
        Get the custom interceptor.
        Returns:
        The custom interceptor that was set, or null if there isn't any.
      • setResponseInterceptor

        public ApiClient setResponseInterceptor​(java.util.function.Consumer<java.net.http.HttpResponse<java.io.InputStream>> interceptor)
        Set a custom response interceptor.

        This is useful for logging, monitoring or extraction of header variables

        Parameters:
        interceptor - A function invoked before creating each request. A value of null resets the interceptor to a no-op.
        Returns:
        This object.
      • getResponseInterceptor

        public java.util.function.Consumer<java.net.http.HttpResponse<java.io.InputStream>> getResponseInterceptor()
        Get the custom response interceptor.
        Returns:
        The custom interceptor that was set, or null if there isn't any.
      • setAsyncResponseInterceptor

        public ApiClient setAsyncResponseInterceptor​(java.util.function.Consumer<java.net.http.HttpResponse<java.lang.String>> interceptor)
        Set a custom async response interceptor. Use this interceptor when asyncNative is set to 'true'.

        This is useful for logging, monitoring or extraction of header variables

        Parameters:
        interceptor - A function invoked before creating each request. A value of null resets the interceptor to a no-op.
        Returns:
        This object.
      • getAsyncResponseInterceptor

        public java.util.function.Consumer<java.net.http.HttpResponse<java.lang.String>> getAsyncResponseInterceptor()
        Get the custom async response interceptor. Use this interceptor when asyncNative is set to 'true'.
        Returns:
        The custom interceptor that was set, or null if there isn't any.