public class ApiClient
extends java.lang.Object
ApiClient gives you easy or built in:
doRequest(Request)
Intercepting and transforming requests and responses examples:
ApiClient client = new ApiClient().withRequestListener(req -> return new Response())//-- edit the request or return your own response to short circuits the remote call
.withResponseListener(res -> res.setStatus(200))//-- edit anything you want about the response
//-- you can also override "doRequest" to control everything before and after the actual HttpClient call.
client = new ApiClient("myservice")
{
protected Response doRequest(Request request)
{
if(checkMyCondition(request))
{
//-- short circuit the remote call "faking" a
//-- remote 200 response (the default status code for a Response)
return new Response(request.getUrl());
}
else
{
doMyRequestTransformation(request);
Response response = super.doRequest(request);
doMyResponseTransformation(response);
return response;
}
}
}
client.get("/some/relative/path")
.onSuccess(response -@gt; System.out.println("Success:" + res.toString()))
.onFailure(response -@gt; System.out.println("Failure:" + res.toString()))
.onResponse(response -@gt; System.out.println(res.getStatus()));
//-- instead of using the success/failure callbacks as above
//-- you can wait for the async process to complete by calling 'get()'
FutureResponse future = client.post("/some/relative/path", new JSMap("hello", "world"));
//-- request is asynchronously executing here
//-- the call to get() blocks indefinitely until the async execution completes
//-- the fact that this method is called 'get()' is not related to HTTP get.
Response response = future.get();
//-- now do whatever you want with the Response
| Modifier and Type | Class and Description |
|---|---|
static class |
ApiClient.Executor
An asynchronous thread pool task runner.
|
class |
ApiClient.FutureResponse
A RunnableFuture that blocks on get() until the execution of the Request has returned the Response.
|
static interface |
ApiClient.RequestListener |
| Modifier and Type | Field and Description |
|---|---|
protected int |
compressionMinSize
If
useCompression is true, anything over this size in bytes will be compressed. |
protected int |
connectionRequestTimeout
Parameter for default HttpClient configuration
|
protected int |
connectTimeout
Parameter for default HttpClient configuration
|
boolean |
evictExpiredConnections
Parameter for default HttpClient configuration
|
int |
evictIdleConnectionsAfterTimeMillis
Parameter for default HttpClient configuration
|
protected java.util.Set |
excludeForwardHeaders
Never forward these headers.
|
protected java.util.Set<java.lang.String> |
excludeParams
Never forward these params.
|
protected ApiClient.Executor |
executor
The thread pool executor used to make asynchronous requests.
|
protected org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> |
forcedHeaders
Headers that are always sent regardless of
forwardHeaders, includeForwardHeaders and excludeForwardHeaders state. |
protected boolean |
forwardHeaders
Indicates the headers from the root inbound Request being handled on this Chain should be included on this request minus any
excludeForwardHeaders. |
protected boolean |
forwardParams
Indicates the params from the root inbound Request being handled on this Chain should be included on this request minus any
excludeParams. |
protected org.apache.http.client.HttpClient |
httpClient |
protected org.apache.http.impl.client.HttpClientBuilder |
httpClientBuilder |
protected java.util.Set |
includeForwardHeaders
Forward these headers when forwardHeaders is true.
|
protected java.util.Set<java.lang.String> |
includeParams
Forward these params when forwardParams is true.
|
int |
maxConPerRoute
Parameter for default HttpClient configuration
|
int |
maxConTotal
Parameter for default HttpClient configuration
|
protected long |
maxMemoryBuffer
Responses over this size will be written to a temp file that will be deleted
when the Response inputStream is closed (or Response is finalized which closes the stream)
|
protected java.lang.String |
name
The ApiClient name that will be used for property decoding.
|
protected java.util.List<ApiClient.RequestListener> |
requestListeners |
protected java.util.List<java.util.function.Consumer<Response>> |
responseListeners |
protected int |
socketTimeout
Parameter for default HttpClient configuration
|
protected int |
threadsMax
The number of background executor threads.
|
protected java.lang.String |
url
Optional base url that will be prepended to the url arg of any calls assuming that the url arg supplied is a relative path and not an absolute url.
|
protected boolean |
useCompression
Indicates that a request body should be gzipped and the content-encoding header should be sent with value "gzip".
|
| Constructor and Description |
|---|
ApiClient() |
ApiClient(java.lang.String name) |
| Modifier and Type | Method and Description |
|---|---|
org.apache.http.impl.client.HttpClientBuilder |
buildDefaultHttpClientBuilder() |
protected ApiClient.Executor |
buildExecutor()
Build an executor if one was not wired in.
|
protected org.apache.http.client.HttpClient |
buildHttpClient(org.apache.http.impl.client.HttpClientBuilder builder) |
Request |
buildRequest(java.lang.String method,
java.lang.String fullUrlOrRelativePath,
java.util.Map<java.lang.String,java.lang.String> params,
java.lang.Object body,
org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> headers)
Builds a request with the supplied information merged with the url, query param, and header options configured
on this reset client and potentially pulled from the Chain.first() root caller request.
|
ApiClient.FutureResponse |
call(Request request)
Executes the Request as provided without modification ignoring forwardHeaders/forwardParams etc.
|
ApiClient.FutureResponse |
call(java.lang.String method,
java.lang.String fullUrlOrRelativePath,
java.util.Map<java.lang.String,java.lang.String> params,
java.lang.Object body,
org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> headers)
Makes an HTTP request.
|
ApiClient.FutureResponse |
delete(java.lang.String fullUrlOrRelativePath)
Convenience overloading of
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a DELETE request. |
protected Response |
doRequest(Request request)
The work of executing the remote call is done here.
|
ApiClient.FutureResponse |
get(java.lang.String fullUrlOrRelativePath)
Convenience overloading of
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a GET request. |
ApiClient.FutureResponse |
get(java.lang.String fullUrlOrRelativePath,
java.util.Map<java.lang.String,java.lang.String> params)
Convenience overloading of
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a GET request. |
ApiClient.FutureResponse |
get(java.lang.String fullUrlOrRelativePath,
java.lang.String... queryStringNameValuePairs)
Convenience overloading of
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a GET request. |
ApiClient.FutureResponse |
get(java.lang.String fullUrlOrRelativePath,
java.lang.String queryString)
Convenience overloading of
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a GET request. |
int |
getCompressionMinSize() |
int |
getConnectionRequestTimeout() |
int |
getConnectTimeout() |
int |
getEvictIdleConnectionsAfterTimeMillis() |
java.util.Set |
getExcludeForwardHeaders() |
java.util.Set |
getExcludeParams() |
ApiClient.Executor |
getExecutor() |
org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> |
getForcedHeaders() |
org.apache.http.client.HttpClient |
getHttpClient() |
org.apache.http.impl.client.HttpClientBuilder |
getHttpClientBuilder() |
java.util.Set<java.lang.String> |
getIncludeForwardHeaders() |
java.util.Set |
getIncludeParams() |
int |
getMaxConPerRoute() |
int |
getMaxConTotal() |
long |
getMaxMemoryBuffer() |
java.lang.String |
getName() |
int |
getSocketTimeout() |
int |
getThreadsMax() |
boolean |
isEvictExpiredConnections() |
boolean |
isForwardHeaders() |
boolean |
isForwardParams() |
boolean |
isUseCompression() |
ApiClient |
onRequest(ApiClient.RequestListener requestListener)
Requests listeners can modify the Request.
|
ApiClient |
onResponse(java.util.function.Consumer<Response> responseListener) |
ApiClient.FutureResponse |
patch(java.lang.String fullUrlOrRelativePath,
java.lang.Object body)
Convenience overloading of
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a PATCH request. |
ApiClient.FutureResponse |
post(java.lang.String fullUrlOrRelativePath,
java.lang.Object body)
Convenience overloading of
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a POST request. |
ApiClient.FutureResponse |
put(java.lang.String fullUrlOrRelativePath,
java.lang.Object body)
Convenience overloading of
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a PUT request. |
ApiClient |
removeExcludeForwardHeader(java.lang.String headerKey) |
ApiClient |
removeExcludeParam(java.lang.String param) |
ApiClient |
removeIncludeForwardHeader(java.lang.String headerKey) |
ApiClient |
removeIncludeParam(java.lang.String param) |
java.lang.String |
replaceVars(Request parentRequest,
java.lang.String url)
Replaces path parameters with their corresponding request params
|
protected boolean |
shouldForwardHeader(java.lang.String headerKey) |
protected boolean |
shouldForwardParam(java.lang.String param) |
ApiClient |
withCompressionMinSize(int compressionMinSize) |
ApiClient |
withConnectionRequestTimeout(int connectionRequestTimeout) |
ApiClient |
withConnectTimeout(int connectTimeout) |
ApiClient |
withEvictExpiredConnections(boolean evictExpiredConnections) |
ApiClient |
withEvictIdleConnectionsAfterTimeMillis(int evictIdleConnectionsAfterTimeMillis) |
ApiClient |
withExcludeForwardHeaders(java.lang.String... headerKeys) |
ApiClient |
withExcludeParams(java.lang.String... paramNames) |
ApiClient |
withExecutor(ApiClient.Executor executor) |
ApiClient |
withForcedHeader(java.lang.String name,
java.lang.String value) |
ApiClient |
withForcedHeaders(java.lang.String... headers) |
ApiClient |
withForwardedHeaders(boolean forwardHeaders) |
ApiClient |
withForwardedParams(boolean forwardParams) |
ApiClient |
withForwardHeaders(boolean forwardHeaders) |
ApiClient |
withForwardParams(boolean forwardParams) |
ApiClient |
withHttpClient(org.apache.http.client.HttpClient httpClient) |
ApiClient |
withHttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder httpClientBuilder) |
ApiClient |
withIncludeForwardHeaders(java.lang.String... headerKeys) |
ApiClient |
withIncludeParams(java.lang.String... paramNames) |
ApiClient |
withMaxConPerRoute(int maxConPerRoute) |
ApiClient |
withMaxConTotal(int maxConTotal) |
ApiClient |
withMaxMemoryBuffer(long maxMemoryBuffer) |
ApiClient |
withName(java.lang.String name) |
ApiClient |
withSocketTimeout(int socketTimeout) |
ApiClient |
withThreadsMax(int threadsMax) |
ApiClient |
withUrl(java.lang.String url) |
ApiClient |
withUseCompression(boolean useCompression) |
protected final org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> forcedHeaders
forwardHeaders, includeForwardHeaders and excludeForwardHeaders state.
These headers will overwrite any caller supplied or forwarded header with the same key, not simply appending to the value list.
This list is initially empty.
protected final java.util.List<ApiClient.RequestListener> requestListeners
protected final java.util.List<java.util.function.Consumer<Response>> responseListeners
protected java.lang.String name
Contextprotected java.lang.String url
protected boolean forwardHeaders
excludeForwardHeaders.
Default value is true.
protected final java.util.Set includeForwardHeaders
protected final java.util.Set excludeForwardHeaders
shouldForwardHeader(String)protected boolean forwardParams
excludeParams.
Default value is false.
protected final java.util.Set<java.lang.String> includeParams
shouldForwardParam(String)protected final java.util.Set<java.lang.String> excludeParams
shouldForwardParam(String)protected boolean useCompression
Default value is true.
protected int compressionMinSize
useCompression is true, anything over this size in bytes will be compressed.
Default value is 1024.
protected long maxMemoryBuffer
protected ApiClient.Executor executor
threadsMax worker threads.protected int threadsMax
A value < 1 will cause all tasks to execute synchronously on the calling thread meaning the FutureResponse will always be complete upon return.
The default value is 0.
protected int socketTimeout
Default value is 30000ms
RequestConfig.Builder.setSocketTimeout(int)protected int connectTimeout
Default value is 30000ms
RequestConfig.Builder.setConnectTimeout(int)protected int connectionRequestTimeout
Default value is 30000ms
RequestConfig.Builder.setConnectionRequestTimeout(int)public int maxConPerRoute
Default value is 10
HttpClientBuilder.setMaxConnPerRoute(int)public int maxConTotal
Default value is 50ms
HttpClientBuilder.setMaxConnTotal(int)public boolean evictExpiredConnections
HttpClientBuilder.evictExpiredConnections()public int evictIdleConnectionsAfterTimeMillis
HttpClientBuilder.evictIdleConnections(long, TimeUnit)protected org.apache.http.impl.client.HttpClientBuilder httpClientBuilder
protected org.apache.http.client.HttpClient httpClient
public ApiClient()
public ApiClient(java.lang.String name)
name - the prefix used to look up property values from the environment if they have not already been wiredpublic ApiClient.FutureResponse get(java.lang.String fullUrlOrRelativePath)
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a GET request.fullUrlOrRelativePath - may be a full url or relative to the url property if set, can have a query string or notpublic ApiClient.FutureResponse get(java.lang.String fullUrlOrRelativePath, java.lang.String queryString)
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a GET request.fullUrlOrRelativePath - may be a full url or relative to the url property if set, can have a query string or notqueryString - additional query string params in name=value@amp;name2=value2 stylepublic ApiClient.FutureResponse get(java.lang.String fullUrlOrRelativePath, java.util.Map<java.lang.String,java.lang.String> params)
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a GET request.fullUrlOrRelativePath - may be a full url or relative to the url property if set, can have a query string or notparams - query strings passed in as a mappublic ApiClient.FutureResponse get(java.lang.String fullUrlOrRelativePath, java.lang.String... queryStringNameValuePairs)
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a GET request.fullUrlOrRelativePath - may be a full url or relative to the url property if set, can have a query string or notqueryStringNameValuePairs - additional query string name/value pairspublic ApiClient.FutureResponse post(java.lang.String fullUrlOrRelativePath, java.lang.Object body)
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a POST request.fullUrlOrRelativePath - may be a full url or relative to the url property if setbody - the optional JSON to postpublic ApiClient.FutureResponse put(java.lang.String fullUrlOrRelativePath, java.lang.Object body)
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a PUT request.fullUrlOrRelativePath - may be a full url or relative to the url property if setbody - the optional JSON to putpublic ApiClient.FutureResponse patch(java.lang.String fullUrlOrRelativePath, java.lang.Object body)
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a PATCH request.fullUrlOrRelativePath - may be a full url or relative to the url property if setbody - the optional JSON patchpublic ApiClient.FutureResponse delete(java.lang.String fullUrlOrRelativePath)
call(String, String, Map, Object, ArrayListValuedHashMap) to perform a DELETE request.fullUrlOrRelativePath - may be a full url or relative to the url property if setpublic ApiClient.FutureResponse call(java.lang.String method, java.lang.String fullUrlOrRelativePath, java.util.Map<java.lang.String,java.lang.String> params, java.lang.Object body, org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> headers)
method - the HTTP method to invokefullUrlOrRelativePath - optional may be a full url or only additional relative path parts if the url property if set, may contain a query stringparams - optional additional query string params that will overwrite any that may be on url as composed from buildUrl(String)body - optional json bodyheaders - headers that will always be sent regardless of includeForwardHeaders, excludeForwardHeaders but may be overwritten by forcedHeaderspublic ApiClient.FutureResponse call(Request request)
All of the other 'get/post/put/patch/delete/call' methods will use buildRequest() to construct a Request based on the configured properties of this ApiClient and optionally the data in Request on the top of the Chain if operating inside an Engine.
Those methods ultimately delegate to this method and no further modification of the Request is made from here out.
request - public Request buildRequest(java.lang.String method, java.lang.String fullUrlOrRelativePath, java.util.Map<java.lang.String,java.lang.String> params, java.lang.Object body, org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> headers)
method - - the http methodfullUrlOrRelativePath - - a full url or a relative path that will be appended to this.urlparams - - query params to passbody - - the request body to passheaders - - request headers to passprotected Response doRequest(Request request)
Override this method to intercept the remote call and change anything about the Request or Response that you want.
All of the Request url/header/param configuration has already been done on the Request.
You don't need to do anything related to threading here. This method is already executing asynchronously within the Executor's thread pool. Simply handle/transform the Request/Response as desired. Simply returning a Response will cause the FutureResponse to transition to done and allow calls blocking on FutureResponse.get() to receive the Response.
Overriding this method can be really helpful when you what your ApiClient calling algorithm to say clean, hiding some of the Request/Response customization details or otherwise need to make sure Requests/Responses are always handled in a specific way.
A typical override of this method might look like the following:
protected ApiClient client = new ApiClient("myservice"){
protected Response doRequest(Request request)
{
if(checkMyCondition(request))
{
//-- short circuit the remote call "faking" a
//-- remote 200 response (the default status code for a Response)
return new Response(request.getUrl());
}
else
{
doMyRequestTransformation(request);
Response response = super.doRequest(request);
doMyResponseTransformation(response);
return response;
}
}
}
request - the request to makepublic ApiClient onRequest(ApiClient.RequestListener requestListener)
requestListener - public java.lang.String replaceVars(Request parentRequest, java.lang.String url)
public ApiClient withUrl(java.lang.String url)
public org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> getForcedHeaders()
public ApiClient withForcedHeader(java.lang.String name, java.lang.String value)
public ApiClient withForcedHeaders(java.lang.String... headers)
public ApiClient withForwardedHeaders(boolean forwardHeaders)
public ApiClient withForwardedParams(boolean forwardParams)
public java.lang.String getName()
public ApiClient withName(java.lang.String name)
public boolean isUseCompression()
public ApiClient withUseCompression(boolean useCompression)
public int getCompressionMinSize()
public ApiClient withCompressionMinSize(int compressionMinSize)
public ApiClient withHttpClient(org.apache.http.client.HttpClient httpClient)
public int getSocketTimeout()
public ApiClient withSocketTimeout(int socketTimeout)
public int getConnectTimeout()
public ApiClient withConnectTimeout(int connectTimeout)
public int getConnectionRequestTimeout()
public ApiClient withConnectionRequestTimeout(int connectionRequestTimeout)
public int getMaxConPerRoute()
public ApiClient withMaxConPerRoute(int maxConPerRoute)
public int getMaxConTotal()
public ApiClient withMaxConTotal(int maxConTotal)
public boolean isEvictExpiredConnections()
public ApiClient withEvictExpiredConnections(boolean evictExpiredConnections)
public int getEvictIdleConnectionsAfterTimeMillis()
public ApiClient withEvictIdleConnectionsAfterTimeMillis(int evictIdleConnectionsAfterTimeMillis)
public org.apache.http.client.HttpClient getHttpClient()
protected org.apache.http.client.HttpClient buildHttpClient(org.apache.http.impl.client.HttpClientBuilder builder)
throws java.lang.Exception
java.lang.Exceptionpublic ApiClient withHttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder httpClientBuilder)
public org.apache.http.impl.client.HttpClientBuilder getHttpClientBuilder()
public org.apache.http.impl.client.HttpClientBuilder buildDefaultHttpClientBuilder()
public ApiClient withExecutor(ApiClient.Executor executor)
public ApiClient.Executor getExecutor()
executor if necessary.protected ApiClient.Executor buildExecutor()
You can dependency inject your Executor or override this method to provide advanced customizations.
As a convenience ApiClient.threadsMax is configured on the default executor.
public boolean isForwardHeaders()
protected boolean shouldForwardHeader(java.lang.String headerKey)
public ApiClient withForwardHeaders(boolean forwardHeaders)
public java.util.Set<java.lang.String> getIncludeForwardHeaders()
public ApiClient withIncludeForwardHeaders(java.lang.String... headerKeys)
public ApiClient removeIncludeForwardHeader(java.lang.String headerKey)
public java.util.Set getExcludeForwardHeaders()
public ApiClient withExcludeForwardHeaders(java.lang.String... headerKeys)
public ApiClient removeExcludeForwardHeader(java.lang.String headerKey)
public boolean isForwardParams()
protected boolean shouldForwardParam(java.lang.String param)
public ApiClient withForwardParams(boolean forwardParams)
public java.util.Set getIncludeParams()
public ApiClient withIncludeParams(java.lang.String... paramNames)
public ApiClient removeIncludeParam(java.lang.String param)
public java.util.Set getExcludeParams()
public ApiClient withExcludeParams(java.lang.String... paramNames)
public ApiClient removeExcludeParam(java.lang.String param)
public long getMaxMemoryBuffer()
public ApiClient withMaxMemoryBuffer(long maxMemoryBuffer)
public int getThreadsMax()
public ApiClient withThreadsMax(int threadsMax)
Copyright © 2023 Rocket Partners, LLC. All rights reserved.