public class ApiClient extends Object
Constructor and Description |
---|
ApiClient() |
Modifier and Type | Method and Description |
---|---|
ApiClient |
addAuthentication(String name,
Authentication auth)
Add custom authentication
|
ApiClient |
addDefaultHeader(String key,
String value)
Add a default header.
|
okhttp3.Call |
buildCall(String path,
String method,
List<Pair> queryParams,
List<Pair> collectionQueryParams,
Object body,
Map<String,String> headerParams,
Map<String,Object> formParams,
String[] authNames,
ProgressRequestBody.ProgressRequestListener progressRequestListener)
Build HTTP call with the given options.
|
okhttp3.Request |
buildRequest(String path,
String method,
List<Pair> queryParams,
List<Pair> collectionQueryParams,
Object body,
Map<String,String> headerParams,
Map<String,Object> formParams,
String[] authNames,
ProgressRequestBody.ProgressRequestListener progressRequestListener)
Build an HTTP request with the given options.
|
String |
buildUrl(String path,
List<Pair> queryParams,
List<Pair> collectionQueryParams)
Build full URL by concatenating base path, the given sub path and query parameters.
|
<T> T |
deserialize(okhttp3.Response response,
Type returnType)
Deserialize response body to Java object, according to the return type and
the Content-Type response header.
|
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,
Type 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,
Type returnType,
ApiCallback<T> callback)
Execute HTTP call asynchronously.
|
String |
getBasePath()
Get base path
|
okhttp3.OkHttpClient |
getHttpClient()
Get HTTP client
|
<T> T |
handleResponse(okhttp3.Response response,
Type returnType)
Handle the given response, return the deserialized object when the response is successful.
|
boolean |
isJsonMime(String mime)
Check if the given MIME is a JSON MIME.
|
static boolean |
isTest() |
List<Pair> |
parameterToPair(String name,
Object value)
Formats the specified query parameter to a list containing a single
Pair object. |
String |
parameterToString(Object param)
Format the given parameter object into string.
|
void |
processHeaderParams(Map<String,String> headerParams,
okhttp3.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.
|
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.
|
ApiClient |
setBasePath(String basePath)
Set base path
|
ApiClient |
setHttpClient(okhttp3.OkHttpClient httpClient)
Set HTTP client
|
static void |
setTest(boolean test) |
void |
updateParamsForAuth(String[] authNames,
String url,
String method,
List<Pair> queryParams,
Map<String,String> headerParams,
Object body)
Update query and header parameters based on authentication settings.
|
public static void setTest(boolean test)
public static boolean isTest()
public String getBasePath()
public ApiClient setBasePath(String basePath)
basePath
- Base path of the URL (e.g http://localhostpublic ApiClient addAuthentication(String name, Authentication auth)
name
- The authentication nameauth
- The custom authenticationpublic okhttp3.OkHttpClient getHttpClient()
public ApiClient setHttpClient(okhttp3.OkHttpClient httpClient)
httpClient
- An instance of OkHttpClientpublic ApiClient addDefaultHeader(String key, String value)
key
- The header's keyvalue
- The header's valuepublic String parameterToString(Object param)
param
- Parameterpublic List<Pair> parameterToPair(String name, Object value)
Pair
object.
Note that value
must not be a collection.name
- The name of the parameter.value
- The value of the parameter.Pair
object.public 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> T deserialize(okhttp3.Response response, Type 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 <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, Type 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> 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, Type returnType, ApiCallback<T> callback)
T
- Typecall
- The callback to be executed when the API call finishesreturnType
- Return typecallback
- ApiCallbackexecute(Call, Type)
public <T> T handleResponse(okhttp3.Response response, Type returnType) throws ApiException
T
- Typeresponse
- ResponsereturnType
- Return typeApiException
- 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, List<Pair> collectionQueryParams, 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 parameterscollectionQueryParams
- The collection 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 okhttp3.Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, 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 parameterscollectionQueryParams
- The collection 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, List<Pair> collectionQueryParams)
path
- The sub pathqueryParams
- The query parameterscollectionQueryParams
- The collection 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, String url, String method, List<Pair> queryParams, Map<String,String> headerParams, Object body)
authNames
- The authentications to applyurl
- The request HTTP URLmethod
- The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE"queryParams
- List of query parametersheaderParams
- Map of header parametersbody
- The request body objectCopyright © 2023. All rights reserved.