RestApiHelper

public class RestApiHelper
extends Object implements IRestApiHelper

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


用于执行 REST API 调用的辅助类。

摘要

常量

int DEFAULT_NUMBER_OF_RETRIES

String JSON_MIME

字段

protected static final JsonFactory JSON_FACTORY

公共构造函数

RestApiHelper(HttpRequestFactory requestFactory, String baseUri)

使用给定信息创建 API 辅助实例。

公共方法

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

使用给定的 URI 部分和选项构建 API 调用的 URI。

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

执行 API 请求。

HttpRequestFactory getRequestFactory()

返回 HttpRequestFactory。

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

创建使用 Credential 进行身份验证的 API 辅助实例。

常量

DEFAULT_NUMBER_OF_RETRIES

protected static final int DEFAULT_NUMBER_OF_RETRIES

常量值: 2 (0x00000002)

JSON_MIME

protected static final String JSON_MIME

常量值: "application/json"

字段

JSON_FACTORY

protected static final JsonFactory JSON_FACTORY

公共构造函数

RestApiHelper

public RestApiHelper (HttpRequestFactory requestFactory, 
                String baseUri)

使用给定信息创建 API 辅助实例。

参数
requestFactory HttpRequestFactory:创建 HttpRequest 时要使用的工厂。

baseUri String:API 的基本 URI

公共方法

buildQueryUri

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

使用给定的 URI 部分和选项构建 API 调用的 URI。uriParts 应已进行网址编码,而 options 应为未编码的字符串。

参数
uriParts String

options Map

返回
GenericUrl

execute

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

执行 API 请求。

参数
method String:请求的 HTTP 方法

uriParts String:用于构建请求 URI 的网址编码 URI 部分。

options Map:用于构建查询字符串的未编码参数名称和值

data JSONObject:要随请求发送的数据

返回
HttpResponse HttpResponse 对象

抛出
IOException

getRequestFactory

public HttpRequestFactory getRequestFactory ()

返回 HttpRequestFactory。

公开以供测试。

返回
HttpRequestFactory

newInstanceWithGoogleCredential

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

创建使用 Credential 进行身份验证的 API 辅助实例。

参数
baseUri String:API 的基本 URI

jsonKeyFile File:服务账号 JSON 密钥文件

scopes Collection:要与服务账号搭配使用的 OAuth 范围的集合

返回
RestApiHelper

抛出
java.io.IOException
java.security.GeneralSecurityException
GeneralSecurityException
IOException