public class ApiClient extends Object
Constructor and Description |
---|
ApiClient() |
Modifier and Type | Method and Description |
---|---|
ApiClient |
addDefaultHeader(String key,
String value)
Add a default header.
|
okhttp3.Call |
buildCall(String path,
String method,
List<Pair> queryParams,
Object body,
Map<String,String> headerParams,
Map<String,Object> formParams,
String[] authNames,
ProgressRequestBody.ProgressRequestListener progressRequestListener)
Build HTTP call with the given options.
|
okhttp3.RequestBody |
buildRequestBodyFormEncoding(Map<String,Object> formParams)
Build a form-encoding request body with the given form parameters.
|
okhttp3.RequestBody |
buildRequestBodyMultipart(Map<String,Object> formParams)
Build a multipart (file uploading) request body with the given form parameters,
which could contain text fields and file fields.
|
String |
buildUrl(String path,
List<Pair> queryParams)
Build full URL by concatenating base path, the given sub path and query parameters.
|
<T> Optional<T> |
deserialize(okhttp3.Response response,
Class<T> returnType)
Deserialize response body to Java object, according to the return type and
the Content-Type response header.
|
File |
downloadFileFromResponse(okhttp3.Response response)
Download file from the given response.
|
String |
escapeString(String str)
Escape the given string to be used as URL query value.
|
<T> ApiResponse<T> |
execute(okhttp3.Call call)
|
<T> ApiResponse<T> |
execute(okhttp3.Call call,
Class<T> returnType)
Execute HTTP call and deserialize the HTTP response body into the given return type.
|
<T> void |
executeAsync(okhttp3.Call call,
ApiCallback<T> callback)
|
<T> void |
executeAsync(okhttp3.Call call,
Class<T> returnType,
ApiCallback<T> callback)
Execute HTTP call asynchronously.
|
<T extends QueryResultData<R>,R extends QueryResult> |
executeWithResultSet(okhttp3.Call call,
Class<T> resultSetClass) |
String |
formatDate(Date date)
Format the given Date object into string (Date format).
|
String |
formatDatetime(Date date)
Format the given Date object into string (Datetime format).
|
Authentication |
getAuthentication(String authName)
Get authentication for the given name.
|
Map<String,Authentication> |
getAuthentications()
Get authentications (key: authentication name, value: authentication).
|
String |
getBasePath()
Get base path.
|
int |
getConnectTimeout()
Get connection timeout (in milliseconds).
|
DateFormat |
getDateFormat() |
DateFormat |
getDatetimeFormat() |
okhttp3.OkHttpClient |
getHttpClient()
Get HTTP client.
|
int |
getReadTimeout()
Gets the current read timeout for a call.
|
InputStream |
getSslCaCert()
Get SSL CA cert.
|
String |
getTempFolderPath()
The path of temporary folder used to store downloaded files from endpoints
with file response.
|
int |
getWriteTimeout()
Gets the current write timeout for a call.
|
String |
guessContentTypeFromFile(File file)
Guess Content-Type header from the given file (defaults to "APPLICATION/octet-stream").
|
<T> Optional<T> |
handleResponse(okhttp3.Response response,
Class<T> returnType)
Handle the given response, return the deserialized object when the response is successful.
|
<T extends QueryResultData<R>,R extends QueryResult> |
handleResponseWithResultSet(okhttp3.Response response,
Class<T> resultSetClass)
Handle the given response, return the deserialized object
with the given result set classes when the response is successful.
|
boolean |
isDebugging()
Check that whether debugging is enabled for this API client.
|
boolean |
isJsonMime(String mime)
Check if the given MIME is a JSON MIME.
|
boolean |
isLenientDatetimeFormat()
Whether to allow various ISO 8601 datetime formats when parsing a datetime string.
|
boolean |
isVerifyingSsl()
True if isVerifyingSsl flag is on.
|
String |
parameterToString(Object param)
Format the given parameter object into string.
|
Date |
parseDate(String str)
Parse the given date string into Date object.
|
Date |
parseDateOrDatetime(String str) |
Date |
parseDatetime(String str)
Parse the given datetime string into Date object.
|
File |
prepareDownloadFile(okhttp3.Response response)
Prepare file for download.
|
void |
processHeaderParams(Map<String,String> headerParams,
okhttp3.Request.Builder reqBuilder)
Set header parameters to the request builder, including default headers.
|
String |
sanitizeFilename(String filename)
Sanitize filename by removing path.
|
String |
selectHeaderAccept(String[] accepts)
Select the Accept header's value from the given accepts array.
|
String |
selectHeaderContentType(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.
|
okhttp3.RequestBody |
serialize(Object obj,
String contentType)
Serialize the given Java object into request body according to the object's
class and the request Content-Type.
|
void |
setApiKey(String apiKey)
Helper method to set API key value for the first API key authentication.
|
void |
setApiKeyPrefix(String apiKeyPrefix)
Helper method to set API key prefix for the first API key authentication.
|
ApiClient |
setBasePath(String path)
Set base path.
|
ApiClient |
setConnectTimeout(int connectionTimeout)
Sets the connect timeout (in milliseconds).
|
ApiClient |
setDateFormat(DateFormat format) |
ApiClient |
setDatetimeFormat(DateFormat format) |
ApiClient |
setDebugging(boolean debugOn)
Enable/disable debugging for this API client.
|
ApiClient |
setHttpClient(okhttp3.OkHttpClient client)
Set HTTP client.
|
ApiClient |
setLenientDatetimeFormat(boolean lenientFormat) |
ApiClient |
setMaxIdleConnections(int maxIdleConnections)
Sets the maximum number of idle connections in the pool.
|
void |
setPassword(String password)
Helper method to set PASSWORD for the first HTTP basic authentication.
|
ApiClient |
setReadTimeout(int readTimeout)
Sets the read time out.
|
ApiClient |
setSslCaCert(InputStream cert)
Configure the CA certificate to be trusted when making https requests.
|
ApiClient |
setTempFolderPath(String folderPath)
Set the temporary folder path (for downloading files).
|
ApiClient |
setUserAgent(String userAgent)
Set the User-Agent header's value (by adding to the default header map).
|
void |
setUsername(String username)
Helper method to set username for the first HTTP basic authentication.
|
ApiClient |
setVerifyingSsl(boolean sslOn)
Configure whether to verify certificate and hostname when making https requests.
|
ApiClient |
setWriteTimeout(int writeTimeout)
Sets the write time out.
|
void |
updateParamsForAuth(String[] authNames,
List<Pair> queryParams,
Map<String,String> headerParams)
Update query and header parameters based on authentication settings.
|
public String getBasePath()
public ApiClient setBasePath(String path)
path
- Base path of the URL (e.g https://localhost:8080/v1public okhttp3.OkHttpClient getHttpClient()
public ApiClient setHttpClient(okhttp3.OkHttpClient client)
client
- An instance of OkHttpClientpublic boolean isVerifyingSsl()
public ApiClient setVerifyingSsl(boolean sslOn) throws ApiException
sslOn
- True to verify TLS/SSL connectionApiException
public InputStream getSslCaCert()
public ApiClient setSslCaCert(InputStream cert) throws ApiException
cert
- input stream for SSL CA certApiException
public DateFormat getDateFormat()
public ApiClient setDateFormat(DateFormat format)
public DateFormat getDatetimeFormat()
public ApiClient setDatetimeFormat(DateFormat format)
public boolean isLenientDatetimeFormat()
parseDatetime(String)
public ApiClient setLenientDatetimeFormat(boolean lenientFormat)
public Date parseDate(String str)
dateFormat
supports these ISO 8601 date formats:
2015-08-16
2015-8-16str
- String to be parsedpublic Date parseDatetime(String str)
str
- Date time string to be parsedpublic String formatDate(Date date)
date
- Date objectpublic String formatDatetime(Date date)
date
- Date objectpublic Map<String,Authentication> getAuthentications()
public Authentication getAuthentication(String authName)
authName
- The authentication namepublic void setUsername(String username) throws ApiException
username
- UsernameApiException
public void setPassword(String password) throws ApiException
password
- PasswordApiException
public void setApiKey(String apiKey) throws ApiException
apiKey
- API keyApiException
public void setApiKeyPrefix(String apiKeyPrefix) throws ApiException
apiKeyPrefix
- API key prefixApiException
public ApiClient setUserAgent(String userAgent)
userAgent
- HTTP request's user agentpublic ApiClient addDefaultHeader(String key, String value)
key
- The header's keyvalue
- The header's valuepublic boolean isDebugging()
public ApiClient setDebugging(boolean debugOn)
debugOn
- To enable (true) or disable (false) debuggingpublic String getTempFolderPath()
null
, i.e. using
the system's default tempopary folder.public ApiClient setTempFolderPath(String folderPath)
folderPath
- Temporary folder pathpublic int getConnectTimeout()
public ApiClient setConnectTimeout(int connectionTimeout)
connectionTimeout
- connection timeout in millisecondspublic ApiClient setMaxIdleConnections(int maxIdleConnections)
maxIdleConnections
- The number of max idle connectionspublic int getReadTimeout()
public ApiClient setReadTimeout(int readTimeout)
readTimeout
- int in millisecondspublic int getWriteTimeout()
public ApiClient setWriteTimeout(int writeTimeout)
writeTimeout
- int in millisecondspublic String parameterToString(Object param)
param
- Parameterpublic String sanitizeFilename(String filename)
filename
- The filename to be sanitizedpublic boolean isJsonMime(String mime)
mime
- MIME (Multipurpose Internet Mail Extensions)public String selectHeaderAccept(String[] accepts)
accepts
- The accepts array to select frompublic String selectHeaderContentType(String[] contentTypes)
contentTypes
- The Content-Type array to select frompublic String escapeString(String str)
str
- String to be escapedpublic <T> Optional<T> deserialize(okhttp3.Response response, Class<T> returnType) throws ApiException
T
- Typeresponse
- HTTP responsereturnType
- The type of the Java objectApiException
- If fail to deserialize response body, i.e. cannot read response body
or the Content-Type of the response is not supported.public okhttp3.RequestBody serialize(Object obj, String contentType) throws ApiException
obj
- The Java objectcontentType
- The request Content-TypeApiException
- If fail to serialize the given objectpublic File downloadFileFromResponse(okhttp3.Response response) throws ApiException
response
- An instance of the Response objectApiException
- If fail to read file content from response and write to diskpublic File prepareDownloadFile(okhttp3.Response response) throws IOException
response
- An instance of the Response objectIOException
- If fail to prepare file for downloadpublic <T> ApiResponse<T> execute(okhttp3.Call call) throws ApiException
T
- Typecall
- An instance of the Call objectApiException
- If fail to execute the callpublic <T> ApiResponse<T> execute(okhttp3.Call call, Class<T> returnType) throws ApiException
T
- The return type corresponding to (same with) returnTypereturnType
- The return type used to deserialize HTTP response bodycall
- CallApiException
- If fail to execute the callpublic <T extends QueryResultData<R>,R extends QueryResult> ApiResponse<T> executeWithResultSet(okhttp3.Call call, Class<T> resultSetClass) throws ApiException
ApiException
public <T> void executeAsync(okhttp3.Call call, ApiCallback<T> callback)
T
- Typecall
- An instance of the Call objectcallback
- ApiCallback<T>public <T> void executeAsync(okhttp3.Call call, Class<T> returnType, ApiCallback<T> callback)
T
- Typecall
- The callback to be executed when the API call finishesreturnType
- Return typecallback
- ApiCallbackexecute(Call, Class)
public <T> Optional<T> handleResponse(okhttp3.Response response, Class<T> returnType) throws ApiException
T
- Typeresponse
- ResponsereturnType
- Return typeApiException
- If the response has a unsuccessful status code or
fail to deserialize the response bodypublic <T extends QueryResultData<R>,R extends QueryResult> Optional<T> handleResponseWithResultSet(okhttp3.Response response, Class<T> resultSetClass) throws ApiException
T
- Type extending QueryResultData.R
- Type extending QueryResultresponse
- ResponseresultSetClass
- Container result set classApiException
- If the response has a unsuccessful status code or
fail to deserialize the response bodypublic okhttp3.Call buildCall(String path, String method, List<Pair> queryParams, Object body, Map<String,String> headerParams, Map<String,Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException
path
- The sub-path of the HTTP URLmethod
- The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and
"DELETE"queryParams
- The query parametersbody
- The request body objectheaderParams
- The header parametersformParams
- The form parametersauthNames
- The authentications to applyprogressRequestListener
- Progress request listenerApiException
- If fail to serialize the request body objectpublic String buildUrl(String path, List<Pair> queryParams)
path
- The sub pathqueryParams
- The query parameterspublic void processHeaderParams(Map<String,String> headerParams, okhttp3.Request.Builder reqBuilder)
headerParams
- Header parameters in the ofrm of MapreqBuilder
- Reqeust.Builderpublic void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String,String> headerParams) throws ApiException
authNames
- The authentications to applyqueryParams
- List of query parametersheaderParams
- Map of header parametersApiException
public okhttp3.RequestBody buildRequestBodyFormEncoding(Map<String,Object> formParams)
formParams
- Form parameters in the form of Mappublic okhttp3.RequestBody buildRequestBodyMultipart(Map<String,Object> formParams)
formParams
- Form parameters in the form of MapCopyright (c) 2014-2021 All Rights Reserved by the RWS Group for and on behalf of its affiliates and subsidiaries