Package com.testdroid.api
Class AbstractAPIClient
- java.lang.Object
-
- com.testdroid.api.AbstractAPIClient
-
- All Implemented Interfaces:
APIClient
- Direct Known Subclasses:
APIKeyClient
,DefaultAPIClient
public abstract class AbstractAPIClient extends java.lang.Object implements APIClient
- Author:
- MichaĆ Szpruta
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.String
ACCEPT_HEADER
protected static java.lang.String
API_URI
protected java.lang.String
apiURL
protected int
clientConnectTimeout
protected int
clientRequestTimeout
protected static java.lang.String
DEVICES_URI
protected com.google.api.client.http.HttpTransport
httpTransport
protected static java.lang.String
LABEL_GROUPS_URI
-
Constructor Summary
Constructors Constructor Description AbstractAPIClient()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected <T extends APIEntity>
java.lang.StringbuildUrl(java.lang.String url, Context<T> context)
void
delete(java.lang.String uri)
Calls DELETE request to APIprotected void
deleteOnce(java.lang.String uri)
protected void
disconnectQuietly(com.google.api.client.http.HttpResponse httpResponse)
java.util.Optional<APIDeviceProperty>
findDevicePropertyInLabelGroup(java.lang.String groupName, java.lang.String labelName)
protected java.util.Map<java.lang.String,java.lang.Object>
fixMapParameters(java.util.Map<java.lang.String,java.lang.Object> map)
protected <T> T
fromJson(java.io.InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> type)
protected <T> T
fromJson(java.lang.String content, com.fasterxml.jackson.core.type.TypeReference<T> type)
java.io.InputStream
get(java.lang.String uri)
Calls GET request to API.<T extends APIEntity>
APIList<T>get(java.lang.String uri, Context<T> context)
<T extends APIEntity>
Tget(java.lang.String uri, java.lang.Class<T> type)
Calls GET request to APIprotected APIException
getAPIException(com.google.api.client.http.HttpResponseException ex)
APIListResource<APIDevice>
getDevices()
return resource for accessing list of devices in Cloud using provided filtersAPIListResource<APIDevice>
getDevices(Context<APIDevice> context)
protected abstract com.google.api.client.http.HttpHeaders
getHttpHeaders()
com.google.api.client.http.HttpResponse
getHttpResponse(java.lang.String uri, Context<?> context)
APIListResource<APILabelGroup>
getLabelGroups()
APIListResource<APILabelGroup>
getLabelGroups(Context<APILabelGroup> context)
protected <T extends APIEntity>
TgetOnce(java.lang.String uri, Context<?> context, com.fasterxml.jackson.core.type.TypeReference<T> type)
Tries to call API once.protected com.google.api.client.http.HttpRequestFactory
getRequestFactory()
protected java.io.InputStream
getStream(java.lang.String uri)
APIUser
me()
Fetch and return me - the authenticated user.<T extends APIEntity>
Tpost(java.lang.String uri, java.lang.Object body, java.lang.Class<T> type)
Calls POST request to API<T extends APIEntity>
TpostFile(java.lang.String uri, java.lang.String contentType, java.io.File file, java.util.Map<java.lang.String,java.lang.String> fileExtraParams, java.lang.Class<T> type)
Calls POST request to APIprotected <T extends APIEntity>
TpostOnce(java.lang.String uri, java.lang.Object body, java.util.Map<java.lang.String,java.lang.String> fileExtraParams, java.lang.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 Detail
-
API_URI
protected static final java.lang.String API_URI
- See Also:
- Constant Field Values
-
ACCEPT_HEADER
protected static final java.lang.String ACCEPT_HEADER
- See Also:
- Constant Field Values
-
DEVICES_URI
protected static final java.lang.String DEVICES_URI
- See Also:
- Constant Field Values
-
LABEL_GROUPS_URI
protected static final java.lang.String LABEL_GROUPS_URI
- See Also:
- Constant Field Values
-
clientConnectTimeout
protected int clientConnectTimeout
-
clientRequestTimeout
protected int clientRequestTimeout
-
httpTransport
protected com.google.api.client.http.HttpTransport httpTransport
-
apiURL
protected java.lang.String apiURL
-
-
Method Detail
-
getRequestFactory
protected com.google.api.client.http.HttpRequestFactory getRequestFactory() throws APIException
- Throws:
APIException
-
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
public <T extends APIEntity> T get(java.lang.String uri, java.lang.Class<T> type) throws APIException
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
public java.io.InputStream get(java.lang.String uri) throws APIException
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
public <T extends APIEntity> APIList<T> get(java.lang.String uri, Context<T> context) throws APIException
- Specified by:
get
in interfaceAPIClient
- Throws:
APIException
-
getHttpHeaders
protected abstract com.google.api.client.http.HttpHeaders getHttpHeaders()
-
getOnce
protected <T extends APIEntity> T getOnce(java.lang.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
protected java.io.InputStream getStream(java.lang.String uri) throws APIException
- Throws:
APIException
-
getHttpResponse
public com.google.api.client.http.HttpResponse getHttpResponse(java.lang.String uri, Context<?> context) throws APIException
- Specified by:
getHttpResponse
in interfaceAPIClient
- Throws:
APIException
-
post
public <T extends APIEntity> T post(java.lang.String uri, java.lang.Object body, java.lang.Class<T> type) throws APIException
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(java.lang.String uri, java.lang.Object body, java.util.Map<java.lang.String,java.lang.String> fileExtraParams, java.lang.String contentType, com.fasterxml.jackson.core.type.TypeReference<T> type) throws APIException
- Throws:
APIException
-
postFile
public <T extends APIEntity> T postFile(java.lang.String uri, java.lang.String contentType, java.io.File file, java.util.Map<java.lang.String,java.lang.String> fileExtraParams, java.lang.Class<T> type) throws APIException
Description 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
public void delete(java.lang.String uri) throws APIException
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
protected void deleteOnce(java.lang.String uri) throws APIException
- Throws:
APIException
-
me
public APIUser me() throws APIException
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
public APIListResource<APIDevice> 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
public APIListResource<APIDevice> getDevices(Context<APIDevice> context)
- Specified by:
getDevices
in interfaceAPIClient
-
getLabelGroups
public APIListResource<APILabelGroup> getLabelGroups()
- Specified by:
getLabelGroups
in interfaceAPIClient
-
getLabelGroups
public APIListResource<APILabelGroup> getLabelGroups(Context<APILabelGroup> context)
- Specified by:
getLabelGroups
in interfaceAPIClient
-
findDevicePropertyInLabelGroup
public java.util.Optional<APIDeviceProperty> findDevicePropertyInLabelGroup(java.lang.String groupName, java.lang.String labelName) throws APIException
- Specified by:
findDevicePropertyInLabelGroup
in interfaceAPIClient
- Throws:
APIException
-
fromJson
protected <T> T fromJson(java.io.InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> type) throws APIException
- Throws:
APIException
-
fromJson
protected <T> T fromJson(java.lang.String content, com.fasterxml.jackson.core.type.TypeReference<T> type) throws APIException
- Throws:
APIException
-
buildUrl
protected <T extends APIEntity> java.lang.String buildUrl(java.lang.String url, Context<T> context) throws APIException
- Throws:
APIException
-
fixMapParameters
protected java.util.Map<java.lang.String,java.lang.Object> fixMapParameters(java.util.Map<java.lang.String,java.lang.Object> map)
-
getAPIException
protected APIException getAPIException(com.google.api.client.http.HttpResponseException ex)
-
disconnectQuietly
protected void disconnectQuietly(com.google.api.client.http.HttpResponse httpResponse)
-
-