Package com.testdroid.api
Class AbstractAPIClient
java.lang.Object
com.testdroid.api.AbstractAPIClient
- All Implemented Interfaces:
APIClient
- Direct Known Subclasses:
APIKeyClient
,DefaultAPIClient
- Author:
- MichaĆ Szpruta <[email protected]>
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Calls DELETE request to APIprotected void
deleteOnce
(String uri) protected void
disconnectQuietly
(com.google.api.client.http.HttpResponse httpResponse) findDevicePropertyInLabelGroup
(String groupName, String labelName) fixMapParameters
(Map<String, Object> map) protected <T> T
fromJson
(InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> type) protected <T> T
Calls GET request to API.<T extends APIEntity>
TCalls GET request to APIprotected APIException
getAPIException
(com.google.api.client.http.HttpResponseException ex) return resource for accessing list of devices in Cloud using provided filtersgetDevices
(Context<APIDevice> context) protected abstract com.google.api.client.http.HttpHeaders
com.google.api.client.http.HttpResponse
getHttpResponse
(String uri, Context<?> context) getLabelGroups
(Context<APILabelGroup> context) protected <T extends APIEntity>
TTries to call API once.protected com.google.api.client.http.HttpRequestFactory
protected InputStream
me()
Fetch and return me - the authenticated user.<T extends APIEntity>
TCalls POST request to API<T extends APIEntity>
TpostFile
(String uri, String contentType, File file, Map<String, String> fileExtraParams, Class<T> type) Calls POST request to APIprotected <T extends APIEntity>
TpostOnce
(String uri, Object body, Map<String, String> fileExtraParams, String contentType, com.fasterxml.jackson.core.type.TypeReference<T> type) void
setConnectTimeout
(int timeout) Sets establish connection timeout for APIClient requests.void
setRequestTimeout
(int timeout) Sets timeout for read data from established connection for APIClient requests.
-
Field Details
-
API_URI
- See Also:
-
ACCEPT_HEADER
- See Also:
-
DEVICES_URI
- See Also:
-
LABEL_GROUPS_URI
- See Also:
-
clientConnectTimeout
protected int clientConnectTimeout -
clientRequestTimeout
protected int clientRequestTimeout -
httpTransport
protected com.google.api.client.http.HttpTransport httpTransport -
apiURL
-
-
Constructor Details
-
AbstractAPIClient
public AbstractAPIClient()
-
-
Method Details
-
getRequestFactory
- Throws:
APIException
- Overriding classes may throw this exception if they execute some API calls
-
setConnectTimeout
public void setConnectTimeout(int timeout) Description copied from interface:APIClient
Sets establish connection timeout for APIClient requests.- Specified by:
setConnectTimeout
in interfaceAPIClient
- Parameters:
timeout
- timeout is ms. Value 0 means infinite timeout.
-
setRequestTimeout
public void setRequestTimeout(int timeout) Description copied from interface:APIClient
Sets timeout for read data from established connection for APIClient requests.- Specified by:
setRequestTimeout
in interfaceAPIClient
- Parameters:
timeout
- timeout is ms. Value 0 means infinite timeout.
-
get
Description copied from interface:APIClient
Calls GET request to API- Specified by:
get
in interfaceAPIClient
- Type Parameters:
T
- expected result class to be parsed from response- Parameters:
uri
- context URI of the resource (without/api/v2
prefixtype
- expected result class to be parsed from response- Returns:
- object defined as
T
if succesfully returned and parsed - Throws:
APIException
- on any problem related to API communication
-
get
Description copied from interface:APIClient
Calls GET request to API. Used when result expected as stream.- Specified by:
get
in interfaceAPIClient
- Parameters:
uri
- context URI of the resource (without/api/v2
prefix- Returns:
- stream with response
- Throws:
APIException
- on any problem related to API communication
-
get
- Specified by:
get
in interfaceAPIClient
- Throws:
APIException
-
getHttpHeaders
protected abstract com.google.api.client.http.HttpHeaders getHttpHeaders() -
getOnce
protected <T extends APIEntity> T getOnce(String uri, Context<?> context, com.fasterxml.jackson.core.type.TypeReference<T> type) throws APIException Tries to call API once. Returns expected entity or throws exception.- Throws:
APIException
-
getStream
- Throws:
APIException
-
getHttpResponse
public com.google.api.client.http.HttpResponse getHttpResponse(String uri, Context<?> context) throws APIException - Specified by:
getHttpResponse
in interfaceAPIClient
- Throws:
APIException
-
post
Description copied from interface:APIClient
Calls POST request to API- Specified by:
post
in interfaceAPIClient
- Type Parameters:
T
- expected result class to be parsed from response- Parameters:
uri
- context URI of the resource (without/api/v2
prefixbody
- body - usually url encoded, to POST to APItype
- expected result class to be parsed from response- Returns:
- object defined as
T
if succesfully returned and parsed - Throws:
APIException
- on any problem related to API communication
-
postOnce
protected <T extends APIEntity> T postOnce(String uri, Object body, Map<String, String> fileExtraParams, String contentType, com.fasterxml.jackson.core.type.TypeReference<T> type) throws APIException- Throws:
APIException
-
postFile
public <T extends APIEntity> T postFile(String uri, String contentType, File file, Map<String, String> fileExtraParams, Class<T> type) throws APIExceptionDescription copied from interface:APIClient
Calls POST request to API- Specified by:
postFile
in interfaceAPIClient
- Type Parameters:
T
- expected result class to be parsed from response- Parameters:
uri
- context URI of the resource (without/api/v2
prefix)contentType
- content type of uploaded filefile
- file to be uploaded with that requestfileExtraParams
- extra parameters for form-data describing the filetype
- expected result class to be parsed from response- Returns:
- object defined as
T
if successfully returned and parsed - Throws:
APIException
- on any problem related to API communication
-
delete
Description copied from interface:APIClient
Calls DELETE request to API- Specified by:
delete
in interfaceAPIClient
- Parameters:
uri
- context URI of the resource (without/api/v2
prefix- Throws:
APIException
- on any problem related to API communication
-
deleteOnce
- Throws:
APIException
-
me
Description copied from interface:APIClient
Fetch and return me - the authenticated user.- Specified by:
me
in interfaceAPIClient
- Throws:
APIException
- on invalid username, password or API error.
-
getDevices
Description copied from interface:APIClient
return resource for accessing list of devices in Cloud using provided filters- Specified by:
getDevices
in interfaceAPIClient
- Returns:
- list resource for accessing all devices matching selected filters, if no filter used returns all devices
-
getDevices
- Specified by:
getDevices
in interfaceAPIClient
-
getLabelGroups
- Specified by:
getLabelGroups
in interfaceAPIClient
-
getLabelGroups
- Specified by:
getLabelGroups
in interfaceAPIClient
-
findDevicePropertyInLabelGroup
public Optional<APIDeviceProperty> findDevicePropertyInLabelGroup(String groupName, String labelName) throws APIException - Specified by:
findDevicePropertyInLabelGroup
in interfaceAPIClient
- Throws:
APIException
-
fromJson
protected <T> T fromJson(InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> type) throws APIException - Throws:
APIException
-
fromJson
protected <T> T fromJson(String content, com.fasterxml.jackson.core.type.TypeReference<T> type) throws APIException - Throws:
APIException
-
buildUrl
- Throws:
APIException
-
fixMapParameters
-
getAPIException
-
disconnectQuietly
protected void disconnectQuietly(com.google.api.client.http.HttpResponse httpResponse)
-