Package com.amilesend.tvdb.client
Class ApiClient
java.lang.Object
com.amilesend.tvdb.client.ApiClient
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddDefaultHeader
(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.downloadFileFromResponse
(okhttp3.Response response) Download file from the given response.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 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.getAuthentication
(String authName) Get authentication for the given name.Get authentications (key: authentication name, value: authentication).Get base pathokhttp3.OkHttpClient
Get HTTP client Use to update with modification for e.g. setHttpClient(getHttpClient.newBuilder().readTimeout(5, TimeUnit.SECONDS).build())getJSON()
Get JSONThe path of temporary folder used to store downloaded files from endpoints with file response.boolean
Check that whether debugging is enabled for this API client.boolean
isJsonMime
(String mime) Check if the given MIME is a JSON MIME.parameterToPair
(String name, Object value) Formats the specified query parameter to a list containing a singlePair
object.parameterToPairs
(String collectionFormat, String name, Collection value) Formats the specified collection query parameters to a list ofPair
objects.parameterToString
(Object param) Format the given parameter object into string.prepareDownloadFile
(okhttp3.Response response) Prepare file for downloadsanitizeFilename
(String filename) Sanitize filename by removing path.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)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.void
setAccessToken
(String accessToken) Helper method to set access token for the first OAuth2 authentication.void
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.setBasePath
(String basePath) Set base pathsetDateFormat
(DateFormat dateFormat) setDebugging
(boolean debugging) Enable/disable debugging for this API client.setHttpClient
(okhttp3.OkHttpClient httpClient) Set HTTP client Update with a modified instance using for e.g. setHttpClient(getHttpClient.newBuilder().readTimeout(5, TimeUnit.SECONDS).build())Set JSONsetLenientOnJson
(boolean lenientOnJson) void
setPassword
(String password) Helper method to set password for the first HTTP basic authentication.setSqlDateFormat
(DateFormat dateFormat) setTempFolderPath
(String tempFolderPath) Set the temporary folder path (for downloading files)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.
-
Constructor Details
-
ApiClient
public ApiClient()
-
-
Method Details
-
getBasePath
Get base path- Returns:
- Baes path
-
setBasePath
Set base path- Parameters:
basePath
- Base path of the URL (e.g https://api4.thetvdb.com/v4- Returns:
- An instance of OkHttpClient
-
getHttpClient
public okhttp3.OkHttpClient getHttpClient()Get HTTP client Use to update with modification for e.g. setHttpClient(getHttpClient.newBuilder().readTimeout(5, TimeUnit.SECONDS).build())- Returns:
- An instance of OkHttpClient
-
setHttpClient
Set HTTP client Update with a modified instance using for e.g. setHttpClient(getHttpClient.newBuilder().readTimeout(5, TimeUnit.SECONDS).build())- Parameters:
httpClient
- An instance of OkHttpClient- Returns:
- Api Client
-
getJSON
Get JSON- Returns:
- JSON object
-
setJSON
Set JSON- Parameters:
json
- JSON object- Returns:
- Api client
-
getDateFormat
-
setDateFormat
-
setSqlDateFormat
-
setLenientOnJson
-
getAuthentications
Get authentications (key: authentication name, value: authentication).- Returns:
- Map of authentication objects
-
getAuthentication
Get authentication for the given name.- Parameters:
authName
- The authentication name- Returns:
- The authentication, null if not found
-
setUsername
Helper method to set username for the first HTTP basic authentication.- Parameters:
username
- Username
-
setPassword
Helper method to set password for the first HTTP basic authentication.- Parameters:
password
- Password
-
setApiKey
Helper method to set API key value for the first API key authentication.- Parameters:
apiKey
- API key
-
setApiKeyPrefix
Helper method to set API key prefix for the first API key authentication.- Parameters:
apiKeyPrefix
- API key prefix
-
setAccessToken
Helper method to set access token for the first OAuth2 authentication.- Parameters:
accessToken
- Access token
-
setUserAgent
Set the User-Agent header's value (by adding to the default header map).- Parameters:
userAgent
- HTTP request's user agent- Returns:
- ApiClient
-
addDefaultHeader
Add a default header.- Parameters:
key
- The header's keyvalue
- The header's value- Returns:
- ApiClient
-
isDebugging
public boolean isDebugging()Check that whether debugging is enabled for this API client.- Returns:
- True if debugging is enabled, false otherwise.
-
setDebugging
Enable/disable debugging for this API client.- Parameters:
debugging
- To enable (true) or disable (false) debugging- Returns:
- ApiClient
-
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:
-
setTempFolderPath
Set the temporary folder path (for downloading files)- Parameters:
tempFolderPath
- Temporary folder path- Returns:
- ApiClient
-
parameterToString
Format the given parameter object into string.- Parameters:
param
- Parameter- Returns:
- String representation of the parameter
-
parameterToPair
Formats the specified query parameter to a list containing a singlePair
object. Note thatvalue
must not be a collection.- Parameters:
name
- The name of the parameter.value
- The value of the parameter.- Returns:
- A list containing a single
Pair
object.
-
parameterToPairs
Formats the specified collection query parameters to a list ofPair
objects. 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
Pair
objects.
-
sanitizeFilename
Sanitize filename by removing path. e.g. ../../sun.gif becomes sun.gif- Parameters:
filename
- The filename to be sanitized- Returns:
- The sanitized filename
-
isJsonMime
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
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
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
Escape the given string to be used as URL query value.- Parameters:
str
- String to be escaped- Returns:
- Escaped string
-
downloadFileFromResponse
Download 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
Prepare file for download- Parameters:
response
- An instance of the Response object- Returns:
- Prepared file for the download
- Throws:
IOException
- If fail to prepare file for download
-
execute
- Type Parameters:
T
- Type- Parameters:
call
- An instance of the Call object- Returns:
- ApiResponse<T>
- Throws:
ApiException
- If fail to execute the call
-
execute
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:
call
- CallreturnType
- The return type used to deserialize HTTP response body- 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
- Type Parameters:
T
- Type- Parameters:
call
- An instance of the Call objectcallback
- ApiCallback<T>
-
executeAsync
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:
-
buildCall
public okhttp3.Call buildCall(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, throws ApiExceptionObject> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) Build 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 parametersauthNames
- The authentications to applyprogressRequestListener
- Progress request listener- Returns:
- The HTTP call
- Throws:
ApiException
- If fail to serialize the request body object
-
buildRequest
public okhttp3.Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, throws ApiExceptionObject> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) Build 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 parametersauthNames
- The authentications to applyprogressRequestListener
- Progress request listener- Returns:
- The HTTP request
- Throws:
ApiException
- If fail to serialize the request body object
-