Package software.amazon.spapi
Class ApiClient
- java.lang.Object
-
- software.amazon.spapi.ApiClient
-
public class ApiClient extends java.lang.Object
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ApiClientaddDefaultHeader(java.lang.String key, java.lang.String value)Add a default header.okhttp3.CallbuildCall(java.lang.String path, java.lang.String method, java.util.List<Pair> queryParams, java.util.List<Pair> collectionQueryParams, java.lang.Object body, java.util.Map<java.lang.String,java.lang.String> headerParams, java.util.Map<java.lang.String,java.lang.Object> formParams, ProgressRequestBody.ProgressRequestListener progressRequestListener)Build HTTP call with the given options.okhttp3.RequestbuildRequest(java.lang.String path, java.lang.String method, java.util.List<Pair> queryParams, java.util.List<Pair> collectionQueryParams, java.lang.Object body, java.util.Map<java.lang.String,java.lang.String> headerParams, java.util.Map<java.lang.String,java.lang.Object> formParams, ProgressRequestBody.ProgressRequestListener progressRequestListener)Build an HTTP request with the given options.okhttp3.RequestBodybuildRequestBodyFormEncoding(java.util.Map<java.lang.String,java.lang.Object> formParams)Build a form-encoding request body with the given form parameters.okhttp3.RequestBodybuildRequestBodyMultipart(java.util.Map<java.lang.String,java.lang.Object> formParams)Build a multipart (file uploading) request body with the given form parameters, which could contain text fields and file fields.java.lang.StringbuildUrl(java.lang.String path, java.util.List<Pair> queryParams, java.util.List<Pair> collectionQueryParams)Build full URL by concatenating base path, the given sub path and query parameters.<T> Tdeserialize(okhttp3.Response response, java.lang.reflect.Type returnType)Deserialize response body to Java object, according to the return type and the Content-Type response header.java.io.FiledownloadFileFromResponse(okhttp3.Response response)Download file from the given response.java.lang.StringescapeString(java.lang.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, java.lang.reflect.Type returnType)Execute HTTP call and deserialize the HTTP response body into the given return type.<T> voidexecuteAsync(okhttp3.Call call, java.lang.reflect.Type returnType, ApiCallback<T> callback)Execute HTTP call asynchronously.<T> voidexecuteAsync(okhttp3.Call call, ApiCallback<T> callback)java.lang.StringgetBasePath()Get base pathintgetConnectTimeout()Get connection timeout (in milliseconds).okhttp3.OkHttpClientgetHttpClient()Get HTTP clientJSONgetJSON()Get JSONintgetReadTimeout()Get read timeout (in milliseconds).java.lang.StringgetTempFolderPath()The path of temporary folder used to store downloaded files from endpoints with file response.intgetWriteTimeout()Get write timeout (in milliseconds).java.lang.StringguessContentTypeFromFile(java.io.File file)Guess Content-Type header from the given file (defaults to "application/octet-stream").<T> ThandleResponse(okhttp3.Response response, java.lang.reflect.Type returnType)Handle the given response, return the deserialized object when the response is successful.booleanisJsonMime(java.lang.String mime)Check if the given MIME is a JSON MIME.java.util.List<Pair>parameterToPair(java.lang.String name, java.lang.Object value)Formats the specified query parameter to a list containing a singlePairobject.java.util.List<Pair>parameterToPairs(java.lang.String collectionFormat, java.lang.String name, java.util.Collection value)Formats the specified collection query parameters to a list ofPairobjects.java.lang.StringparameterToString(java.lang.Object param)Format the given parameter object into string.java.io.FileprepareDownloadFile(okhttp3.Response response)Prepare file for downloadvoidprocessHeaderParams(java.util.Map<java.lang.String,java.lang.String> headerParams, okhttp3.Request.Builder reqBuilder)Set header parameters to the request builder, including default headers.java.lang.StringsanitizeFilename(java.lang.String filename)Sanitize filename by removing path.java.lang.StringselectHeaderAccept(java.lang.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)java.lang.StringselectHeaderContentType(java.lang.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.RequestBodyserialize(java.lang.Object obj, java.lang.String contentType)Serialize the given Java object into request body according to the object's class and the request Content-Type.ApiClientsetBasePath(java.lang.String basePath)Set base pathApiClientsetDateFormat(java.text.DateFormat dateFormat)ApiClientsetHttpClient(okhttp3.OkHttpClient httpClient)Set HTTP clientApiClientsetJSON(JSON json)Set JSONApiClientsetLenientOnJson(boolean lenientOnJson)ApiClientsetLocalDateFormat(org.threeten.bp.format.DateTimeFormatter dateFormat)ApiClientsetLWAAuthorizationSigner(LWAAuthorizationSigner lwaAuthorizationSigner)Sets the LWAAuthorizationSignerApiClientsetOffsetDateTimeFormat(org.threeten.bp.format.DateTimeFormatter dateFormat)ApiClientsetSqlDateFormat(java.text.DateFormat dateFormat)ApiClientsetTempFolderPath(java.lang.String tempFolderPath)Set the temporary folder path (for downloading files)ApiClientsetUserAgent(java.lang.String userAgent)Set the User-Agent header's value (by adding to the default header map).
-
-
-
Method Detail
-
getBasePath
public java.lang.String getBasePath()
Get base path- Returns:
- Baes path
-
setBasePath
public ApiClient setBasePath(java.lang.String basePath)
Set base path- Parameters:
basePath- Base path of the URL (e.g https://sellingpartnerapi-na.amazon.com/- Returns:
- An instance of OkHttpClient
-
getHttpClient
public okhttp3.OkHttpClient getHttpClient()
Get HTTP client- Returns:
- An instance of OkHttpClient
-
setHttpClient
public ApiClient setHttpClient(okhttp3.OkHttpClient httpClient)
Set HTTP client- Parameters:
httpClient- An instance of OkHttpClient- Returns:
- Api Client
-
getJSON
public JSON getJSON()
Get JSON- Returns:
- JSON object
-
setJSON
public ApiClient setJSON(JSON json)
Set JSON- Parameters:
json- JSON object- Returns:
- Api client
-
setDateFormat
public ApiClient setDateFormat(java.text.DateFormat dateFormat)
-
setSqlDateFormat
public ApiClient setSqlDateFormat(java.text.DateFormat dateFormat)
-
setOffsetDateTimeFormat
public ApiClient setOffsetDateTimeFormat(org.threeten.bp.format.DateTimeFormatter dateFormat)
-
setLocalDateFormat
public ApiClient setLocalDateFormat(org.threeten.bp.format.DateTimeFormatter dateFormat)
-
setLenientOnJson
public ApiClient setLenientOnJson(boolean lenientOnJson)
-
setUserAgent
public ApiClient setUserAgent(java.lang.String userAgent)
Set the User-Agent header's value (by adding to the default header map).- Parameters:
userAgent- HTTP request's user agent- Returns:
- ApiClient
-
addDefaultHeader
public ApiClient addDefaultHeader(java.lang.String key, java.lang.String value)
Add a default header.- Parameters:
key- The header's keyvalue- The header's value- Returns:
- ApiClient
-
getTempFolderPath
public java.lang.String getTempFolderPath()
The path of temporary folder used to store downloaded files from endpoints with file response. The default value isnull, i.e. using the system's default tempopary folder.- Returns:
- Temporary folder path
- See Also:
- createTempFile
-
setTempFolderPath
public ApiClient setTempFolderPath(java.lang.String tempFolderPath)
Set the temporary folder path (for downloading files)- Parameters:
tempFolderPath- Temporary folder path- Returns:
- ApiClient
-
getConnectTimeout
public int getConnectTimeout()
Get connection timeout (in milliseconds).- Returns:
- Timeout in milliseconds
-
getReadTimeout
public int getReadTimeout()
Get read timeout (in milliseconds).- Returns:
- Timeout in milliseconds
-
getWriteTimeout
public int getWriteTimeout()
Get write timeout (in milliseconds).- Returns:
- Timeout in milliseconds
-
setLWAAuthorizationSigner
public ApiClient setLWAAuthorizationSigner(LWAAuthorizationSigner lwaAuthorizationSigner)
Sets the LWAAuthorizationSigner- Parameters:
lwaAuthorizationSigner- LWAAuthorizationSigner instance- Returns:
- Api client
-
parameterToString
public java.lang.String parameterToString(java.lang.Object param)
Format the given parameter object into string.- Parameters:
param- Parameter- Returns:
- String representation of the parameter
-
parameterToPair
public java.util.List<Pair> parameterToPair(java.lang.String name, java.lang.Object value)
Formats the specified query parameter to a list containing a singlePairobject.Note that
valuemust not be a collection.- Parameters:
name- The name of the parameter.value- The value of the parameter.- Returns:
- A list containing a single
Pairobject.
-
parameterToPairs
public java.util.List<Pair> parameterToPairs(java.lang.String collectionFormat, java.lang.String name, java.util.Collection value)
Formats the specified collection query parameters to a list ofPairobjects.Note that the values of each of the returned Pair objects are percent-encoded.
- Parameters:
collectionFormat- The collection format of the parameter.name- The name of the parameter.value- The value of the parameter.- Returns:
- A list of
Pairobjects.
-
sanitizeFilename
public java.lang.String sanitizeFilename(java.lang.String filename)
Sanitize filename by removing path. e.g. ../../sun.gif becomes sun.gif- Parameters:
filename- The filename to be sanitized- Returns:
- The sanitized filename
-
isJsonMime
public boolean isJsonMime(java.lang.String mime)
Check if the given MIME is a JSON MIME. JSON MIME examples: application/json application/json; charset=UTF8 APPLICATION/JSON application/vnd.company+json "* / *" is also default to JSON- Parameters:
mime- MIME (Multipurpose Internet Mail Extensions)- Returns:
- True if the given MIME is JSON, false otherwise.
-
selectHeaderAccept
public java.lang.String selectHeaderAccept(java.lang.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)- Parameters:
accepts- The accepts array to select from- Returns:
- The Accept header to use. If the given array is empty, null will be returned (not to set the Accept header explicitly).
-
selectHeaderContentType
public java.lang.String selectHeaderContentType(java.lang.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.- Parameters:
contentTypes- The Content-Type array to select from- Returns:
- The Content-Type header to use. If the given array is empty, or matches "any", JSON will be used.
-
escapeString
public java.lang.String escapeString(java.lang.String str)
Escape the given string to be used as URL query value.- Parameters:
str- String to be escaped- Returns:
- Escaped string
-
deserialize
public <T> T deserialize(okhttp3.Response response, java.lang.reflect.Type returnType) throws ApiExceptionDeserialize response body to Java object, according to the return type and the Content-Type response header.- Type Parameters:
T- Type- Parameters:
response- HTTP responsereturnType- The type of the Java object- Returns:
- The deserialized Java object
- Throws:
ApiException- If fail to deserialize response body, i.e. cannot read response body or the Content-Type of the response is not supported.
-
serialize
public okhttp3.RequestBody serialize(java.lang.Object obj, java.lang.String contentType) throws ApiExceptionSerialize the given Java object into request body according to the object's class and the request Content-Type.- Parameters:
obj- The Java objectcontentType- The request Content-Type- Returns:
- The serialized request body
- Throws:
ApiException- If fail to serialize the given object
-
downloadFileFromResponse
public java.io.File downloadFileFromResponse(okhttp3.Response response) throws ApiExceptionDownload file from the given response.- Parameters:
response- An instance of the Response object- Returns:
- Downloaded file
- Throws:
ApiException- If fail to read file content from response and write to disk
-
prepareDownloadFile
public java.io.File prepareDownloadFile(okhttp3.Response response) throws java.io.IOExceptionPrepare file for download- Parameters:
response- An instance of the Response object- Returns:
- Prepared file for the download
- Throws:
java.io.IOException- If fail to prepare file for download
-
execute
public <T> ApiResponse<T> execute(okhttp3.Call call) throws ApiException
- Type Parameters:
T- Type- Parameters:
call- An instance of the Call object- Returns:
- ApiResponse<T>
- Throws:
ApiException- If fail to execute the call
-
execute
public <T> ApiResponse<T> execute(okhttp3.Call call, java.lang.reflect.Type returnType) throws ApiException
Execute HTTP call and deserialize the HTTP response body into the given return type.- Type Parameters:
T- The return type corresponding to (same with) returnType- Parameters:
returnType- The return type used to deserialize HTTP response bodycall- Call- Returns:
- ApiResponse object containing response status, headers and data, which is a Java object deserialized from response body and would be null when returnType is null.
- Throws:
ApiException- If fail to execute the call
-
executeAsync
public <T> void executeAsync(okhttp3.Call call, ApiCallback<T> callback)- Type Parameters:
T- Type- Parameters:
call- An instance of the Call objectcallback- ApiCallback<T>
-
executeAsync
public <T> void executeAsync(okhttp3.Call call, java.lang.reflect.Type returnType, ApiCallback<T> callback)Execute HTTP call asynchronously.- Type Parameters:
T- Type- Parameters:
call- The callback to be executed when the API call finishesreturnType- Return typecallback- ApiCallback- See Also:
execute(Call, Type)
-
handleResponse
public <T> T handleResponse(okhttp3.Response response, java.lang.reflect.Type returnType) throws ApiExceptionHandle the given response, return the deserialized object when the response is successful.- Type Parameters:
T- Type- Parameters:
response- ResponsereturnType- Return type- Returns:
- Type
- Throws:
ApiException- If the response has a unsuccessful status code or fail to deserialize the response body
-
buildCall
public okhttp3.Call buildCall(java.lang.String path, java.lang.String method, java.util.List<Pair> queryParams, java.util.List<Pair> collectionQueryParams, java.lang.Object body, java.util.Map<java.lang.String,java.lang.String> headerParams, java.util.Map<java.lang.String,java.lang.Object> formParams, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException, LWAExceptionBuild HTTP call with the given options.- Parameters:
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 parametersprogressRequestListener- Progress request listener- Returns:
- The HTTP call
- Throws:
ApiException- If fail to serialize the request body objectLWAException- If calls to fetch LWA access token fails
-
buildRequest
public okhttp3.Request buildRequest(java.lang.String path, java.lang.String method, java.util.List<Pair> queryParams, java.util.List<Pair> collectionQueryParams, java.lang.Object body, java.util.Map<java.lang.String,java.lang.String> headerParams, java.util.Map<java.lang.String,java.lang.Object> formParams, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException, LWAExceptionBuild an HTTP request with the given options.- Parameters:
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 parametersprogressRequestListener- Progress request listener- Returns:
- The HTTP request
- Throws:
ApiException- If fail to serialize the request body objectLWAException- If calls to fetch LWA access token fails
-
buildUrl
public java.lang.String buildUrl(java.lang.String path, java.util.List<Pair> queryParams, java.util.List<Pair> collectionQueryParams)Build full URL by concatenating base path, the given sub path and query parameters.- Parameters:
path- The sub pathqueryParams- The query parameterscollectionQueryParams- The collection query parameters- Returns:
- The full URL
-
processHeaderParams
public void processHeaderParams(java.util.Map<java.lang.String,java.lang.String> headerParams, okhttp3.Request.Builder reqBuilder)Set header parameters to the request builder, including default headers.- Parameters:
headerParams- Header parameters in the ofrm of MapreqBuilder- Reqeust.Builder
-
buildRequestBodyFormEncoding
public okhttp3.RequestBody buildRequestBodyFormEncoding(java.util.Map<java.lang.String,java.lang.Object> formParams)
Build a form-encoding request body with the given form parameters.- Parameters:
formParams- Form parameters in the form of Map- Returns:
- RequestBody
-
buildRequestBodyMultipart
public okhttp3.RequestBody buildRequestBodyMultipart(java.util.Map<java.lang.String,java.lang.Object> formParams)
Build a multipart (file uploading) request body with the given form parameters, which could contain text fields and file fields.- Parameters:
formParams- Form parameters in the form of Map- Returns:
- RequestBody
-
guessContentTypeFromFile
public java.lang.String guessContentTypeFromFile(java.io.File file)
Guess Content-Type header from the given file (defaults to "application/octet-stream").- Parameters:
file- The given file- Returns:
- The guessed Content-Type
-
-