Class ApiClient
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
ConstructorDescriptionCreate an instance of ApiClient.ApiClient
(HttpClient.Builder builder, com.fasterxml.jackson.databind.ObjectMapper mapper, String baseUri) Create an instance of ApiClient. -
Method Summary
Modifier and TypeMethodDescriptionprotected HttpClient.Builder
protected com.fasterxml.jackson.databind.ObjectMapper
Get the custom async response interceptor.Get the base URI to resolve the endpoint paths against.Get connection timeout (in milliseconds).protected String
Get anHttpClient
based on the currentHttpClient.Builder
.com.fasterxml.jackson.databind.ObjectMapper
Get a copy of the currentObjectMapper
.Get the read timeout that was set.Get the custom interceptor.Get the custom response interceptor.parameterToPairs
(String name, Object value) Convert a URL query name/value parameter to a list of encodedPair
objects.parameterToPairs
(String collectionFormat, String name, Collection<?> values) Convert a URL query name/collection parameter to a list of encodedPair
objects.setAsyncResponseInterceptor
(Consumer<HttpResponse<String>> interceptor) Set a custom async response interceptor.setBasePath
(String basePath) Set a custom base path for the target service, for example '/v2'.setConnectTimeout
(Duration connectTimeout) Sets the connect timeout (in milliseconds) for the http client.Set a custom host name for the target service.setHttpClientBuilder
(HttpClient.Builder builder) Set a customHttpClient.Builder
object to use when creating theHttpClient
that is used by the API client.setObjectMapper
(com.fasterxml.jackson.databind.ObjectMapper mapper) Set a customObjectMapper
to serialize and deserialize the request and response bodies.setPort
(int port) Set a custom port number for the target service.setReadTimeout
(Duration readTimeout) Set the read timeout for the http client.setRequestInterceptor
(Consumer<HttpRequest.Builder> interceptor) Set a custom request interceptor.setResponseInterceptor
(Consumer<HttpResponse<InputStream>> interceptor) Set a custom response interceptor.Set a custom scheme for the target service, for example 'https'.void
updateBaseUri
(String baseUri) static String
URL encode a string in the UTF-8 encoding.
-
Constructor Details
-
ApiClient
public ApiClient()Create an instance of ApiClient. -
ApiClient
public ApiClient(HttpClient.Builder builder, com.fasterxml.jackson.databind.ObjectMapper mapper, String baseUri) Create an instance of ApiClient.- Parameters:
builder
- Http client builder.mapper
- Object mapper.baseUri
- Base URI
-
-
Method Details
-
urlEncode
URL encode a string in the UTF-8 encoding.- Parameters:
s
- String to encode.- Returns:
- URL-encoded representation of the input string.
-
parameterToPairs
Convert a URL query name/value parameter to a list of encodedPair
objects.The value can be null, in which case an empty list is returned.
- Parameters:
name
- The query name parameter.value
- The query value, which may not be a collection but may be null.- Returns:
- A singleton list of the
Pair
objects representing the input parameters, which is encoded for use in a URL. If the value is null, an empty list is returned.
-
parameterToPairs
public static List<Pair> parameterToPairs(String collectionFormat, String name, Collection<?> values) Convert a URL query name/collection parameter to a list of encodedPair
objects.- Parameters:
collectionFormat
- The swagger collectionFormat string (csv, tsv, etc).name
- The query name parameter.values
- A collection of values for the given query name, which may be null.- Returns:
- A list of
Pair
objects representing the input parameters, which is encoded for use in a URL. If the values collection is null, an empty list is returned.
-
createDefaultObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper createDefaultObjectMapper() -
getDefaultBaseUri
-
createDefaultHttpClientBuilder
-
updateBaseUri
-
setHttpClientBuilder
Set a customHttpClient.Builder
object to use when creating theHttpClient
that is used by the API client.- Parameters:
builder
- Custom client builder.- Returns:
- This object.
-
getHttpClient
Get anHttpClient
based on the currentHttpClient.Builder
.The returned object is immutable and thread-safe.
- Returns:
- The HTTP client.
-
setObjectMapper
Set a customObjectMapper
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 currentObjectMapper
.- Returns:
- A copy of the current object mapper.
-
setHost
Set a custom host name for the target service.- Parameters:
host
- The host name of the target service.- Returns:
- This object.
-
setPort
Set a custom port number for the target service.- Parameters:
port
- The port of the target service. Set this to -1 to reset the value to the default for the scheme.- Returns:
- This object.
-
setBasePath
Set a custom base path for the target service, for example '/v2'.- Parameters:
basePath
- The base path against which the rest of the path is resolved.- Returns:
- This object.
-
getBaseUri
Get the base URI to resolve the endpoint paths against.- Returns:
- The complete base URI that the rest of the API parameters are resolved against.
-
setScheme
Set a custom scheme for the target service, for example 'https'.- Parameters:
scheme
- The scheme of the target service- Returns:
- This object.
-
setRequestInterceptor
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.
-
getRequestInterceptor
Get the custom interceptor.- Returns:
- The custom interceptor that was set, or null if there isn't any.
-
setResponseInterceptor
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
Get the custom response interceptor.- Returns:
- The custom interceptor that was set, or null if there isn't any.
-
setAsyncResponseInterceptor
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
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.
-
setReadTimeout
Set the read timeout for the http client.This is the value used by default for each request, though it can be overridden on a per-request basis with a request interceptor.
- Parameters:
readTimeout
- The read timeout used by default by the http client. Setting this value to null resets the timeout to an effectively infinite value.- Returns:
- This object.
-
getReadTimeout
Get the read timeout that was set.- Returns:
- The read timeout, or null if no timeout was set. Null represents an infinite wait time.
-
setConnectTimeout
Sets the connect timeout (in milliseconds) for the http client.In the case where a new connection needs to be established, if the connection cannot be established within the given
duration
, thenHttpClient::send
throws anHttpConnectTimeoutException
, orHttpClient::sendAsync
completes exceptionally with anHttpConnectTimeoutException
. If a new connection does not need to be established, for example if a connection can be reused from a previous request, then this timeout duration has no effect.- Parameters:
connectTimeout
- connection timeout in milliseconds- Returns:
- This object.
-
getConnectTimeout
Get connection timeout (in milliseconds).- Returns:
- Timeout in milliseconds
-