Class AbstractAPIClient

java.lang.Object
com.testdroid.api.AbstractAPIClient
All Implemented Interfaces:
APIClient
Direct Known Subclasses:
APIKeyClient, DefaultAPIClient

public abstract class AbstractAPIClient extends Object implements APIClient
Author:
MichaƂ Szpruta <[email protected]>
  • Field Details

    • API_URI

      protected static final String API_URI
      See Also:
    • ACCEPT_HEADER

      protected static final String ACCEPT_HEADER
      See Also:
    • DEVICES_URI

      protected static final String DEVICES_URI
      See Also:
    • LABEL_GROUPS_URI

      protected static final String LABEL_GROUPS_URI
      See Also:
    • clientConnectTimeout

      protected int clientConnectTimeout
    • clientRequestTimeout

      protected int clientRequestTimeout
    • httpTransport

      protected com.google.api.client.http.HttpTransport httpTransport
    • apiURL

      protected String apiURL
  • Constructor Details

    • AbstractAPIClient

      public AbstractAPIClient()
  • Method Details

    • getRequestFactory

      protected com.google.api.client.http.HttpRequestFactory getRequestFactory() throws APIException
      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 interface APIClient
      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 interface APIClient
      Parameters:
      timeout - timeout is ms. Value 0 means infinite timeout.
    • get

      public <T extends APIEntity> T get(String uri, Class<T> type) throws APIException
      Description copied from interface: APIClient
      Calls GET request to API
      Specified by:
      get in interface APIClient
      Type Parameters:
      T - expected result class to be parsed from response
      Parameters:
      uri - context URI of the resource (without /api/v2 prefix
      type - 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 InputStream get(String uri) throws APIException
      Description copied from interface: APIClient
      Calls GET request to API. Used when result expected as stream.
      Specified by:
      get in interface APIClient
      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(String uri, Context<T> context) throws APIException
      Specified by:
      get in interface APIClient
      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

      protected InputStream getStream(String uri) throws APIException
      Throws:
      APIException
    • getHttpResponse

      public com.google.api.client.http.HttpResponse getHttpResponse(String uri, Context<?> context) throws APIException
      Specified by:
      getHttpResponse in interface APIClient
      Throws:
      APIException
    • post

      public <T extends APIEntity> T post(String uri, Object body, Class<T> type) throws APIException
      Description copied from interface: APIClient
      Calls POST request to API
      Specified by:
      post in interface APIClient
      Type Parameters:
      T - expected result class to be parsed from response
      Parameters:
      uri - context URI of the resource (without /api/v2 prefix
      body - body - usually url encoded, to POST to API
      type - 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 APIException
      Description copied from interface: APIClient
      Calls POST request to API
      Specified by:
      postFile in interface APIClient
      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 file
      file - file to be uploaded with that request
      fileExtraParams - extra parameters for form-data describing the file
      type - 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(String uri) throws APIException
      Description copied from interface: APIClient
      Calls DELETE request to API
      Specified by:
      delete in interface APIClient
      Parameters:
      uri - context URI of the resource (without /api/v2 prefix
      Throws:
      APIException - on any problem related to API communication
    • deleteOnce

      protected void deleteOnce(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 interface APIClient
      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 interface APIClient
      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 interface APIClient
    • getLabelGroups

      public APIListResource<APILabelGroup> getLabelGroups()
      Specified by:
      getLabelGroups in interface APIClient
    • getLabelGroups

      public APIListResource<APILabelGroup> getLabelGroups(Context<APILabelGroup> context)
      Specified by:
      getLabelGroups in interface APIClient
    • findDevicePropertyInLabelGroup

      public Optional<APIDeviceProperty> findDevicePropertyInLabelGroup(String groupName, String labelName) throws APIException
      Specified by:
      findDevicePropertyInLabelGroup in interface APIClient
      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

      protected <T extends APIEntity> String buildUrl(String url, Context<T> context) throws APIException
      Throws:
      APIException
    • fixMapParameters

      protected Map<String,Object> fixMapParameters(Map<String,Object> map)
    • getAPIException

      protected APIException getAPIException(com.google.api.client.http.HttpResponseException ex)
    • disconnectQuietly

      protected void disconnectQuietly(com.google.api.client.http.HttpResponse httpResponse)