RestApiHelper

public class RestApiHelper
extends Object implements IRestApiHelper

java.lang.Object
   ↳ com.android.tradefed.util.RestApiHelper


A helper class for performing REST API calls.

Summary

Constants

int DEFAULT_NUMBER_OF_RETRIES

String JSON_MIME

Fields

protected static final JsonFactory JSON_FACTORY

Public constructors

RestApiHelper(HttpRequestFactory requestFactory, String baseUri)

Creates an API helper instance with the given information.

Public methods

GenericUrl buildQueryUri(String[] uriParts, Map<String, Object> options)

Construct a URI for a API call with given URI parts and options.

HttpResponse execute(String method, String[] uriParts, Map<String, Object> options, JSONObject data)

Executes an API request.

HttpRequestFactory getRequestFactory()

Returns the HttpRequestFactory.

static RestApiHelper newInstanceWithGoogleCredential(String baseUri, File jsonKeyFile, Collection<String> scopes)

Creates an API helper instance which uses a Credential for authentication.

Constants

DEFAULT_NUMBER_OF_RETRIES

protected static final int DEFAULT_NUMBER_OF_RETRIES

Constant Value: 2 (0x00000002)

JSON_MIME

protected static final String JSON_MIME

Constant Value: "application/json"

Fields

JSON_FACTORY

protected static final JsonFactory JSON_FACTORY

Public constructors

RestApiHelper

public RestApiHelper (HttpRequestFactory requestFactory, 
                String baseUri)

Creates an API helper instance with the given information.

Parameters
requestFactory HttpRequestFactory: the factory to use when creating HttpRequests.

baseUri String: the base URI of API

Public methods

buildQueryUri

public GenericUrl buildQueryUri (String[] uriParts, 
                Map<String, Object> options)

Construct a URI for a API call with given URI parts and options. uriParts should be URL-encoded already, while options should be unencoded Strings.

Parameters
uriParts String

options Map

Returns
GenericUrl

execute

public HttpResponse execute (String method, 
                String[] uriParts, 
                Map<String, Object> options, 
                JSONObject data)

Executes an API request.

Parameters
method String: a HTTP method of the request

uriParts String: URL encoded URI parts to be used to construct the request URI.

options Map: unencoded parameter names and values used to construct the query string

data JSONObject: data to be sent with the request

Returns
HttpResponse a HttpResponse object

Throws
IOException

getRequestFactory

public HttpRequestFactory getRequestFactory ()

Returns the HttpRequestFactory.

Exposed for testing.

Returns
HttpRequestFactory

newInstanceWithGoogleCredential

public static RestApiHelper newInstanceWithGoogleCredential (String baseUri, 
                File jsonKeyFile, 
                Collection<String> scopes)

Creates an API helper instance which uses a Credential for authentication.

Parameters
baseUri String: the base URI of the API

jsonKeyFile File: the service account json key file

scopes Collection: the collection of OAuth scopes to use with the service account

Returns
RestApiHelper

Throws
java.io.IOException
java.security.GeneralSecurityException
GeneralSecurityException
IOException