public class ApiClient extends Object
Modifier and Type | Field and Description |
---|---|
static int |
ANDROID_SDK_VERSION |
static boolean |
IS_ANDROID |
static double |
JAVA_VERSION |
static String |
LENIENT_DATETIME_FORMAT
The datetime format to be used when
lenientDatetimeFormat is enabled. |
Constructor and Description |
---|
ApiClient() |
Modifier and Type | Method and Description |
---|---|
ApiClient |
addDefaultHeader(String key,
String value)
Add a default header.
|
com.squareup.okhttp.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.
|
com.squareup.okhttp.RequestBody |
buildRequestBodyFormEncoding(Map<String,Object> formParams)
Build a form-encoding request body with the given form parameters.
|
com.squareup.okhttp.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> T |
deserialize(com.squareup.okhttp.Response response,
Type returnType)
Deserialize response body to Java object, according to the Content-Type
response header.
|
File |
downloadFileFromResponse(com.squareup.okhttp.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(com.squareup.okhttp.Call call) |
<T> ApiResponse<T> |
execute(com.squareup.okhttp.Call call,
Type returnType)
Execute HTTP call and deserialize the HTTP response body into the given return type.
|
<T> void |
executeAsync(com.squareup.okhttp.Call call,
ApiCallback<T> callback)
#see executeAsync(Call, Type, ApiCallback)
|
<T> void |
executeAsync(com.squareup.okhttp.Call call,
Type returnType,
ApiCallback<T> callback)
Execute HTTP call asynchronously.
|
String |
formatDate(Date date)
Format the given Date object into string.
|
String |
formatDatetime(Date date)
Format the given Date object into string.
|
Authentication |
getAuthentication(String authName)
Get authentication for the given name.
|
Map<String,Authentication> |
getAuthentications()
Get authentications (key: authentication name, value: authentication).
|
String |
getBasePath() |
int |
getConnectTimeout()
Connect timeout (in milliseconds).
|
DateFormat |
getDateFormat() |
DateFormat |
getDatetimeFormat() |
com.squareup.okhttp.OkHttpClient |
getHttpClient() |
JSON |
getJSON() |
InputStream |
getSslCaCert() |
String |
getTempFolderPath()
The path of temporary folder used to store downloaded files from endpoints
with file response.
|
String |
guessContentTypeFromFile(File file)
Guess Content-Type header from the given file (defaults to "application/octet-stream").
|
<T> T |
handleResponse(com.squareup.okhttp.Response response,
Type returnType)
Handle the given response, return the deserialized object 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 |
isLenientOnJson() |
boolean |
isVerifyingSsl() |
List<Pair> |
parameterToPairs(String collectionFormat,
String name,
Object value) |
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(com.squareup.okhttp.Response response) |
void |
processHeaderParams(Map<String,String> headerParams,
com.squareup.okhttp.Request.Builder reqBuilder)
Set header parameters to the request builder, including default headers.
|
String |
selectHeaderAccept(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)
|
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.
|
String |
serialize(Object obj,
String contentType)
Serialize the given Java object into request body string, according to the
request Content-Type.
|
void |
setAccessToken(String accessToken)
Helper method to set access token for the first OAuth2 authentication.
|
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 basePath) |
ApiClient |
setConnectTimeout(int connectionTimeout)
Sets the connect timeout (in milliseconds).
|
ApiClient |
setDateFormat(DateFormat dateFormat) |
ApiClient |
setDatetimeFormat(DateFormat datetimeFormat) |
ApiClient |
setDebugging(boolean debugging)
Enable/disable debugging for this API client.
|
ApiClient |
setHttpClient(com.squareup.okhttp.OkHttpClient httpClient) |
ApiClient |
setJSON(JSON json) |
ApiClient |
setLenientDatetimeFormat(boolean lenientDatetimeFormat) |
ApiClient |
setLenientOnJson(boolean lenient) |
void |
setPassword(String password)
Helper method to set password for the first HTTP basic authentication.
|
ApiClient |
setSslCaCert(InputStream sslCaCert)
Configure the CA certificate to be trusted when making https requests.
|
ApiClient |
setTempFolderPath(String tempFolderPath) |
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 verifyingSsl)
Configure whether to verify certificate and hostname when making https requests.
|
void |
updateParamsForAuth(String[] authNames,
List<Pair> queryParams,
Map<String,String> headerParams)
Update query and header parameters based on authentication settings.
|
public static final double JAVA_VERSION
public static final boolean IS_ANDROID
public static final int ANDROID_SDK_VERSION
public static final String LENIENT_DATETIME_FORMAT
lenientDatetimeFormat
is enabled.public String getBasePath()
public com.squareup.okhttp.OkHttpClient getHttpClient()
public ApiClient setHttpClient(com.squareup.okhttp.OkHttpClient httpClient)
public JSON getJSON()
public boolean isVerifyingSsl()
public ApiClient setVerifyingSsl(boolean verifyingSsl)
public InputStream getSslCaCert()
public ApiClient setSslCaCert(InputStream sslCaCert)
public DateFormat getDateFormat()
public ApiClient setDateFormat(DateFormat dateFormat)
public DateFormat getDatetimeFormat()
public ApiClient setDatetimeFormat(DateFormat datetimeFormat)
public boolean isLenientDatetimeFormat()
parseDatetime(String)
public ApiClient setLenientDatetimeFormat(boolean lenientDatetimeFormat)
public Date parseDate(String str)
dateFormat
supports these ISO 8601 date formats:
2015-08-16
2015-8-16public Date parseDatetime(String str)
lenientDatetimeFormat
is enabled, the following ISO 8601 datetime formats are supported:
2015-08-16T08:20:05Z
2015-8-16T8:20:05Z
2015-08-16T08:20:05+00:00
2015-08-16T08:20:05+0000
2015-08-16T08:20:05.376Z
2015-08-16T08:20:05.376+00:00
2015-08-16T08:20:05.376+00
Note: The 3-digit milli-seconds is optional. Time zone is required and can be in one of
these formats:
Z (same with +0000)
+08:00 (same with +0800)
-02 (same with -0200)
-0200https://en.wikipedia.org/wiki/ISO_8601
public Map<String,Authentication> getAuthentications()
public Authentication getAuthentication(String authName)
authName
- The authentication namepublic void setUsername(String username)
public void setPassword(String password)
public void setApiKey(String apiKey)
public void setApiKeyPrefix(String apiKeyPrefix)
public void setAccessToken(String accessToken)
public ApiClient setUserAgent(String userAgent)
public ApiClient addDefaultHeader(String key, String value)
key
- The header's keyvalue
- The header's valuepublic boolean isLenientOnJson()
https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
public ApiClient setLenientOnJson(boolean lenient)
public boolean isDebugging()
public ApiClient setDebugging(boolean debugging)
debugging
- To enable (true) or disable (false) debuggingpublic String getTempFolderPath()
null
, i.e. using
the system's default tempopary folder.https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File)
public int getConnectTimeout()
public ApiClient setConnectTimeout(int connectionTimeout)
Integer.MAX_VALUE
.public String parameterToString(Object param)
public List<Pair> parameterToPairs(String collectionFormat, String name, Object value)
public boolean isJsonMime(String mime)
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)
public <T> T deserialize(com.squareup.okhttp.Response response, Type returnType) throws ApiException
response
- 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 String serialize(Object obj, String contentType) throws ApiException
obj
- The Java objectcontentType
- The request Content-TypeApiException
- If fail to serialize the given objectpublic File downloadFileFromResponse(com.squareup.okhttp.Response response) throws ApiException
ApiException
- If fail to read file content from response and write to diskpublic File prepareDownloadFile(com.squareup.okhttp.Response response) throws IOException
IOException
public <T> ApiResponse<T> execute(com.squareup.okhttp.Call call) throws ApiException
ApiException
execute(Call, Type)
public <T> ApiResponse<T> execute(com.squareup.okhttp.Call call, Type returnType) throws ApiException
T
- The return type corresponding to (same with) returnTypereturnType
- The return type used to deserialize HTTP response bodyApiResponse
object containing response status, headers and
data, which is a Java object deserialized from response body and would be null
when returnType is null.ApiException
- If fail to execute the callpublic <T> void executeAsync(com.squareup.okhttp.Call call, ApiCallback<T> callback)
public <T> void executeAsync(com.squareup.okhttp.Call call, Type returnType, ApiCallback<T> callback)
The
- callback to be executed when the API call finishesexecute(Call, Type)
public <T> T handleResponse(com.squareup.okhttp.Response response, Type returnType) throws ApiException
ApiException
- If the response has a unsuccessful status code or
fail to deserialize the response bodypublic com.squareup.okhttp.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 applyApiException
- 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, com.squareup.okhttp.Request.Builder reqBuilder)
public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<String,String> headerParams)
authNames
- The authentications to applypublic com.squareup.okhttp.RequestBody buildRequestBodyFormEncoding(Map<String,Object> formParams)
public com.squareup.okhttp.RequestBody buildRequestBodyMultipart(Map<String,Object> formParams)
Copyright © 2019 DocDoku. All rights reserved.