Class ApiClient


  • public class ApiClient
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      ApiClient()  
      ApiClient​(int connectionPoolSize)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ApiClient addDefaultHeader​(java.lang.String key, java.lang.String value)
      Add a default header.
      <T> T deserialize​(jakarta.ws.rs.core.Response response, jakarta.ws.rs.core.GenericType<T> returnType)
      Deserialize response body to Java object according to the Content-Type.
      java.io.File downloadFileFromResponse​(jakarta.ws.rs.core.Response response)
      Download file from the given response.
      java.lang.String escapeString​(java.lang.String str)
      Escape the given string to be used as URL query value.
      java.lang.String formatDate​(java.util.Date date)
      Format the given Date object into string.
      Authentication getAuthentication​(java.lang.String authName)
      Get authentication for the given name.
      java.util.Map<java.lang.String,​Authentication> getAuthentications()
      Get authentications (key: authentication name, value: authentication).
      java.lang.String getBasePath()  
      java.text.DateFormat getDateFormat()
      Get the date format used to parse/format date parameters.
      jakarta.ws.rs.client.Client getHttpClient()  
      JSON getJSON()
      Gets the JSON instance to do JSON serialization and deserialization.
      java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getResponseHeaders()
      Gets the response headers of the previous request
      int getStatusCode()
      Gets the status code of the previous request
      java.lang.String getTempFolderPath()
      The path of temporary folder used to store downloaded files from endpoints with file response.
      <T> T invokeAPI​(java.lang.String path, java.lang.String method, java.util.List<Pair> queryParams, java.lang.Object body, java.util.Map<java.lang.String,​java.lang.String> headerParams, java.util.Map<java.lang.String,​java.lang.String> cookieParams, java.util.Map<java.lang.String,​java.lang.Object> formParams, java.lang.String accept, java.lang.String contentType, java.lang.String[] authNames, jakarta.ws.rs.core.GenericType<T> returnType)
      Invoke API by sending HTTP request with the given options.
      boolean isDebugging()
      Check that whether debugging is enabled for this API client.
      boolean isJsonMime​(java.lang.String mime)
      Check if the given MIME is a JSON MIME.
      java.util.List<Pair> parameterToPairs​(java.lang.String collectionFormat, java.lang.String name, java.lang.Object value)  
      java.lang.String parameterToString​(java.lang.Object param)
      Format the given parameter object into string.
      java.util.Date parseDate​(java.lang.String str)
      Parse the given string into Date object.
      java.io.File prepareDownloadFile​(jakarta.ws.rs.core.Response response)  
      java.lang.String selectHeaderAccept​(java.lang.String[] accepts)
      Select the Accept header's value from the given accepts array: if JSON exists in the given array, use it; otherwise use all of them (joining into a string)
      java.lang.String selectHeaderContentType​(java.lang.String[] contentTypes)
      Select the Content-Type header's value from the given array: if JSON exists in the given array, use it; otherwise use the first one of the array.
      jakarta.ws.rs.client.Entity<?> serialize​(java.lang.Object obj, java.util.Map<java.lang.String,​java.lang.Object> formParams, java.lang.String contentType)
      Serialize the given Java object into string entity according the given Content-Type (only JSON is supported for now).
      <T> java.util.List<Pair> serializeDeepObjectParameter​(java.util.List<Pair> params, java.lang.String objectName, java.lang.String fieldName, T fieldValue)  
      void setApiKey​(java.lang.String apiKey)
      Helper method to set API key value for the first API key authentication.
      void setApiKeyPrefix​(java.lang.String apiKeyPrefix)
      Helper method to set API key prefix for the first API key authentication.
      ApiClient setBasePath​(java.lang.String basePath)  
      ApiClient setDateFormat​(java.text.DateFormat dateFormat)
      Set the date format used to parse/format date parameters.
      ApiClient setDebugging​(boolean debugging)
      Enable/disable debugging for this API client.
      ApiClient setHttpClient​(jakarta.ws.rs.client.Client httpClient)  
      void setPassword​(java.lang.String password)
      Helper method to set password for the first HTTP basic authentication.
      ApiClient setTempFolderPath​(java.lang.String tempFolderPath)  
      ApiClient setUserAgent​(java.lang.String userAgent)
      Set the User-Agent header's value (by adding to the default header map).
      void setUsername​(java.lang.String username)
      Helper method to set username for the first HTTP basic authentication.
      • Methods inherited from class java.lang.Object

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

      • ApiClient

        public ApiClient()
      • ApiClient

        public ApiClient​(int connectionPoolSize)
    • Method Detail

      • getJSON

        public JSON getJSON()
        Gets the JSON instance to do JSON serialization and deserialization.
        Returns:
        the JSON utility class
      • getHttpClient

        public jakarta.ws.rs.client.Client getHttpClient()
      • setHttpClient

        public ApiClient setHttpClient​(jakarta.ws.rs.client.Client httpClient)
      • getBasePath

        public java.lang.String getBasePath()
      • setBasePath

        public ApiClient setBasePath​(java.lang.String basePath)
      • getStatusCode

        public int getStatusCode()
        Gets the status code of the previous request
        Returns:
        the status code of the previous request
      • getResponseHeaders

        public java.util.Map<java.lang.String,​java.util.List<java.lang.String>> getResponseHeaders()
        Gets the response headers of the previous request
        Returns:
        the response headers of the previous request
      • getAuthentications

        public java.util.Map<java.lang.String,​Authentication> getAuthentications()
        Get authentications (key: authentication name, value: authentication).
        Returns:
        the authentications
      • getAuthentication

        public Authentication getAuthentication​(java.lang.String authName)
        Get authentication for the given name.
        Parameters:
        authName - The authentication name
        Returns:
        The authentication, null if not found
      • setUsername

        public void setUsername​(java.lang.String username)
        Helper method to set username for the first HTTP basic authentication.
        Parameters:
        username - the username
      • setPassword

        public void setPassword​(java.lang.String password)
        Helper method to set password for the first HTTP basic authentication.
        Parameters:
        password - the password
      • setApiKey

        public void setApiKey​(java.lang.String apiKey)
        Helper method to set API key value for the first API key authentication.
        Parameters:
        apiKey - the API key
      • setApiKeyPrefix

        public void setApiKeyPrefix​(java.lang.String apiKeyPrefix)
        Helper method to set API key prefix for the first API key authentication.
        Parameters:
        apiKeyPrefix - the API key prefix
      • setUserAgent

        public ApiClient setUserAgent​(java.lang.String userAgent)
        Set the User-Agent header's value (by adding to the default header map).
        Parameters:
        userAgent - the User-Agent header value
        Returns:
        this ApiClient
      • addDefaultHeader

        public ApiClient addDefaultHeader​(java.lang.String key,
                                          java.lang.String value)
        Add a default header.
        Parameters:
        key - The header's key
        value - The header's value
        Returns:
        this ApiClient
      • isDebugging

        public boolean isDebugging()
        Check that whether debugging is enabled for this API client.
        Returns:
        true if debugging is enabled for this API client
      • setDebugging

        public ApiClient setDebugging​(boolean debugging)
        Enable/disable debugging for this API client.
        Parameters:
        debugging - To enable (true) or disable (false) debugging
        Returns:
        this ApiClient
      • getTempFolderPath

        public java.lang.String getTempFolderPath()
        The path of temporary folder used to store downloaded files from endpoints with file response. The default value is null, i.e. using the system's default tempopary folder.
        Returns:
        the temporary folder path
        See Also:
      • setTempFolderPath

        public ApiClient setTempFolderPath​(java.lang.String tempFolderPath)
      • getDateFormat

        public java.text.DateFormat getDateFormat()
        Get the date format used to parse/format date parameters.
        Returns:
        the date format used to parse/format date parameters
      • setDateFormat

        public ApiClient setDateFormat​(java.text.DateFormat dateFormat)
        Set the date format used to parse/format date parameters.
        Parameters:
        dateFormat - a date format used to parse/format date parameters
        Returns:
        this ApiClient
      • parseDate

        public java.util.Date parseDate​(java.lang.String str)
        Parse the given string into Date object.
        Parameters:
        str - a string to parse
        Returns:
        a Date object
      • formatDate

        public java.lang.String formatDate​(java.util.Date date)
        Format the given Date object into string.
        Parameters:
        date - a Date object to format
        Returns:
        the String version of the Date object
      • parameterToString

        public java.lang.String parameterToString​(java.lang.Object param)
        Format the given parameter object into string.
        Parameters:
        param - an object to format
        Returns:
        the String version of the object
      • serializeDeepObjectParameter

        public <T> java.util.List<Pair> serializeDeepObjectParameter​(java.util.List<Pair> params,
                                                                     java.lang.String objectName,
                                                                     java.lang.String fieldName,
                                                                     T fieldValue)
      • parameterToPairs

        public java.util.List<Pair> parameterToPairs​(java.lang.String collectionFormat,
                                                     java.lang.String name,
                                                     java.lang.Object value)
      • isJsonMime

        public boolean isJsonMime​(java.lang.String mime)
        Check if the given MIME is a JSON MIME. JSON MIME examples: application/json application/json; charset=UTF8 APPLICATION/JSON application/vnd.company+json
        Parameters:
        mime - MIME (Multipurpose Internet Mail Extensions)
        Returns:
        True if the given MIME is JSON, false otherwise.
      • selectHeaderAccept

        public java.lang.String selectHeaderAccept​(java.lang.String[] accepts)
        Select the Accept header's value from the given accepts array: if JSON exists in the given array, use it; otherwise use all of them (joining into a string)
        Parameters:
        accepts - The accepts array to select from
        Returns:
        The Accept header to use. If the given array is empty, null will be returned (not to set the Accept header explicitly).
      • selectHeaderContentType

        public java.lang.String selectHeaderContentType​(java.lang.String[] contentTypes)
        Select the Content-Type header's value from the given array: if JSON exists in the given array, use it; otherwise use the first one of the array.
        Parameters:
        contentTypes - The Content-Type array to select from
        Returns:
        The Content-Type header to use. If the given array is empty, JSON will be used.
      • escapeString

        public java.lang.String escapeString​(java.lang.String str)
        Escape the given string to be used as URL query value.
        Parameters:
        str - a String to escape
        Returns:
        the escaped version of the String
      • serialize

        public jakarta.ws.rs.client.Entity<?> serialize​(java.lang.Object obj,
                                                        java.util.Map<java.lang.String,​java.lang.Object> formParams,
                                                        java.lang.String contentType)
                                                 throws ApiException
        Serialize the given Java object into string entity according the given Content-Type (only JSON is supported for now).
        Parameters:
        obj - the object to serialize
        formParams - the form parameters
        contentType - the content type
        Returns:
        an Entity
        Throws:
        ApiException - on failure to serialize
      • deserialize

        public <T> T deserialize​(jakarta.ws.rs.core.Response response,
                                 jakarta.ws.rs.core.GenericType<T> returnType)
                          throws ApiException
        Deserialize response body to Java object according to the Content-Type.
        Type Parameters:
        T - a Java type parameter
        Parameters:
        response - the response body to deserialize
        returnType - a Java type to deserialize into
        Returns:
        a deserialized Java object
        Throws:
        ApiException - on failure to deserialize
      • downloadFileFromResponse

        public java.io.File downloadFileFromResponse​(jakarta.ws.rs.core.Response response)
                                              throws ApiException
        Download file from the given response.
        Parameters:
        response - a response
        Returns:
        a file from the given response
        Throws:
        ApiException - If fail to read file content from response and write to disk
      • prepareDownloadFile

        public java.io.File prepareDownloadFile​(jakarta.ws.rs.core.Response response)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • invokeAPI

        public <T> T invokeAPI​(java.lang.String path,
                               java.lang.String method,
                               java.util.List<Pair> queryParams,
                               java.lang.Object body,
                               java.util.Map<java.lang.String,​java.lang.String> headerParams,
                               java.util.Map<java.lang.String,​java.lang.String> cookieParams,
                               java.util.Map<java.lang.String,​java.lang.Object> formParams,
                               java.lang.String accept,
                               java.lang.String contentType,
                               java.lang.String[] authNames,
                               jakarta.ws.rs.core.GenericType<T> returnType)
                        throws ApiException
        Invoke API by sending HTTP request with the given options.
        Type Parameters:
        T - a Java type parameter
        Parameters:
        path - The sub-path of the HTTP URL
        method - The request method, one of "GET", "POST", "PUT", "HEAD" and "DELETE"
        queryParams - The query parameters
        body - The request body object
        headerParams - The header parameters
        cookieParams - The cookie parameters
        formParams - The form parameters
        accept - The request's Accept header
        contentType - The request's Content-Type header
        authNames - The authentications to apply
        returnType - The return type into which to deserialize the response
        Returns:
        The response body in type of string
        Throws:
        ApiException - if the invocation failed