Search in sources :

Example 1 with CommonClient

use of com.tencentcloudapi.common.CommonClient in project tencentcloud-sdk-java by TencentCloud.

the class CommonClientCall method main.

public static void main(String[] args) {
    try {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        Credential cred = new Credential("secretId", "secretKey");
        // 实例化要请求产品的CommonClient对象,依次传入产品名、产品版本、Credential、地域
        CommonClient client = new CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou");
        /* 调用client对象的call方法,传入接口名和json格式的字符串向接口发起调用
             * 以查询可用区(zone)为广州一区,实例计费模式(instance-charge-type)为包年包月或者按量计费的实例为例
             */
        String resp = client.call("DescribeInstances", "{\"Filters\":" + "[{\"Name\":\"zone\"," + "\"Values\":[\"ap-guangzhou-1\"]}," + "{\"Name\":\"instance-charge-type\"," + "\"Values\":[\"PREPAID\",\"POSTPAID_BY_HOUR\"]}]" + "}");
        // 输出返回的字符串结果
        System.out.println(resp);
    } catch (TencentCloudSDKException e) {
        System.out.println(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) CommonClient(com.tencentcloudapi.common.CommonClient)

Example 2 with CommonClient

use of com.tencentcloudapi.common.CommonClient in project tencentcloud-sdk-java by TencentCloud.

the class CommonClientCallOctetStream method main.

public static void main(String[] args) {
    try {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        Credential cred = new Credential("secretId", "secretKey");
        // 实例化要请求产品的CommonClient对象,依次传入产品名、产品版本、Credential、地域
        CommonClient client = new CommonClient("cls", "2020-10-16", cred, "ap-guangzhou");
        // 调用client对象的call方法,传入接口名、请求入参(http头部)和二进制包体向接口发起调用
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("X-CLS-TopicId", "e621fdb8-16f4-41cf-bc73-5aead0b75a03");
        headers.put("X-CLS-HashKey", "0fffffffffffffffffffffffffffffff");
        headers.put("X-CLS-CompressType", "");
        // 实际内容可能需要从外部文件加载
        byte[] body = new byte[1024];
        String resp = client.callOctetStream("UploadLog", headers, body);
        // 输出返回的字符串结果
        System.out.println(resp);
    } catch (TencentCloudSDKException e) {
        System.out.println(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) HashMap(java.util.HashMap) CommonClient(com.tencentcloudapi.common.CommonClient)

Example 3 with CommonClient

use of com.tencentcloudapi.common.CommonClient in project tencentcloud-sdk-java by TencentCloud.

the class CommonClientRequest method main.

public static void main(String[] args) {
    try {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        Credential cred = new Credential("secretId", "secretKey");
        // 实例化要请求产品的CommonClient对象,依次传入产品名、产品版本、Credential、地域
        CommonClient client = new CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou");
        /* CommonClient中的commonRequest方法支持传入继承了AbstractModel的Request类和接口名对接口进行调用
             * 用户可以按照标准自行实现一个继承了AbstractModel的Request类
             */
        DescribeInstancesRequest req = new DescribeInstancesRequest();
        String resp = client.commonRequest(req, "DescribeInstances");
        // 输出返回的字符串结果
        System.out.println(resp);
    } catch (TencentCloudSDKException e) {
        System.out.println(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) DescribeInstancesRequest(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest) CommonClient(com.tencentcloudapi.common.CommonClient)

Example 4 with CommonClient

use of com.tencentcloudapi.common.CommonClient in project tencentcloud-sdk-java by TencentCloud.

the class STSCredential method updateCredential.

private void updateCredential() throws TencentCloudSDKException {
    Credential cred = new Credential(secretId, secretKey);
    HttpProfile httpProfile = new HttpProfile();
    httpProfile.setEndpoint("sts.tencentcloudapi.com");
    ClientProfile clientProfile = new ClientProfile();
    clientProfile.setHttpProfile(httpProfile);
    CommonClient client = new CommonClient("sts", "2018-08-13", cred, "ap-guangzhou", clientProfile);
    String resp = client.call("AssumeRole", "{\"RoleArn\":\"" + roleArn + "\"," + "\"RoleSessionName\":\"" + roleSessionName + "\"}");
    Map<String, Object> map = new Gson().fromJson(resp, new TypeToken<HashMap<String, Object>>() {
    }.getType());
    Map<String, Object> respmap = (Map<String, Object>) map.get("Response");
    Map<String, String> credmap = (Map<String, String>) respmap.get("Credentials");
    tmpSecretId = credmap.get("TmpSecretId");
    tmpSecretKey = credmap.get("TmpSecretKey");
    token = credmap.get("Token");
    expiredTime = ((Double) respmap.get("ExpiredTime")).intValue();
}
Also used : Credential(com.tencentcloudapi.common.Credential) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) TypeToken(com.google.gson.reflect.TypeToken) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) Gson(com.google.gson.Gson) Map(java.util.Map) HashMap(java.util.HashMap) CommonClient(com.tencentcloudapi.common.CommonClient)

Example 5 with CommonClient

use of com.tencentcloudapi.common.CommonClient in project tencentcloud-sdk-java by TencentCloud.

the class CommonClientTest method testJson.

@Test
public void testJson() {
    Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));
    CommonClient client = new CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou");
    try {
        String resp = client.call("DescribeInstances", "{\"Filters\":" + "[{\"Name\":\"zone\"," + "\"Values\":[\"ap-guangzhou-1\"]}]" + "}");
    } catch (Exception e) {
        fail(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) CommonClient(com.tencentcloudapi.common.CommonClient) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) Test(org.junit.Test)

Aggregations

CommonClient (com.tencentcloudapi.common.CommonClient)9 Credential (com.tencentcloudapi.common.Credential)9 TencentCloudSDKException (com.tencentcloudapi.common.exception.TencentCloudSDKException)8 HashMap (java.util.HashMap)4 ClientProfile (com.tencentcloudapi.common.profile.ClientProfile)2 HttpProfile (com.tencentcloudapi.common.profile.HttpProfile)2 DescribeInstancesRequest (com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest)2 Test (org.junit.Test)2 Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1 Map (java.util.Map)1