注意
依赖的版本信息需满足volc-sdk-java版本≥1.0.133。
package com.example.demo;
import com.volcengine.error.SdkError;
import com.volcengine.helper.Const;
import com.volcengine.model.ApiInfo;
import com.volcengine.model.Credentials;
import com.volcengine.model.ServiceInfo;
import com.volcengine.model.response.RawResponse;
import com.volcengine.service.BaseServiceImpl;
import org.apache.http.NameValuePair;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class DemoService extends BaseServiceImpl {
private static final String ServiceName = "apmplus_openapi";
private static final String Host = "open.volcengineapi.com";
private static final String Scheme = "https";
private static final String Path = "/";
private static final String AK = "xx";
private static final String SK = "xx==";
private static final String AppID = "123456";
public DemoService(ServiceInfo info, Map<String, ApiInfo> apiInfoList) {
super(info, null, apiInfoList);
}
public static void main(String[] args) throws Exception {
String action = "AnalyseSummary"; // 接口名
String version = "2022-10-12"; // 版本号
Map<String, ApiInfo> apiInfoList = buildApiInfo(action, version);
ServiceInfo serviceInfo = buildServiceInfo();
DemoService service = new DemoService(serviceInfo, apiInfoList);
service.setAccessKey(DemoService.AK);
service.setSecretKey(DemoService.SK);
RawResponse response = service.json(action, null, buildRequestJsonBody());
if (response.getCode() != SdkError.SUCCESS.getNumber()) {
System.out.println("errorNo:" + response.getCode());
return;
}
System.out.println(new String(response.getData(), StandardCharsets.UTF_8));
}
/**
* 当前接口的请求 body,json string 格式
* {
* "os":"iOS",
* "start_time":1658664000,
* "end_time":1658750543,
* "compare_start_time":1658577457,
* "compare_end_time":1658664000,
* "filters": {
* "type":"and",
* "sub_conditions":[
* {
* "dimension":"os",
* "op":"in",
* "type":"expression",
* "values":["iOS"]
* }
* ]
* },
* "order_by":"count"
* }
*/
private static String buildRequestJsonBody() {
return "{"os":"iOS","start_time":1658664000,"end_time":1658750543,"compare_start_time":1658577457,"compare_end_time":1658664000,"filters":{"type":"and","sub_conditions":[{"dimension":"os","op":"in","type":"expression","values":["iOS"]}]},"order_by":"count"}";
}
private static ServiceInfo buildServiceInfo() {
return new ServiceInfo(new HashMap<String, Object>() {
{
put(Const.CONNECTION_TIMEOUT, 5000);
put(Const.SOCKET_TIMEOUT, 5000);
put(Const.Host, DemoService.Host);
put(Const.Credentials, new Credentials(Const.REGION_CN_NORTH_1, DemoService.ServiceName));
put(Const.Scheme, DemoService.Scheme);
}
});
}
private static Map<String, ApiInfo> buildApiInfo(final String action, final String version) {
return new HashMap<String, ApiInfo>() {
{
put(action, new ApiInfo(
new HashMap<String, Object>() {
{
put(Const.Method, "POST");
put(Const.Path, DemoService.Path);
put(Const.Header, new ArrayList<Header>() {
{
add(new BasicHeader("x-app-ids", DemoService.AppID));
}
});
put(Const.Query, new ArrayList<NameValuePair>() {
{
add(new BasicNameValuePair("Action", action));
add(new BasicNameValuePair("Version", version));
}
});
}
}));
}
};
}
}