Search in sources :

Example 1 with CvmClient

use of com.tencentcloudapi.cvm.v20170312.CvmClient in project tencentcloud-sdk-java by TencentCloud.

the class DescribeInstances method main.

public static void main(String[] args) {
    try {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        Credential cred = new Credential("secretId", "secretKey");
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        // get请求(默认为post请求)
        httpProfile.setReqMethod("GET");
        // 请求连接超时时间,单位为秒(默认60秒)
        httpProfile.setConnTimeout(30);
        // 指定接入地域域名(默认就近接入)
        httpProfile.setEndpoint("cvm.ap-shanghai.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        // 指定签名算法(默认为HmacSHA256)
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        clientProfile.setDebug(true);
        // 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的
        CvmClient client = new CvmClient(cred, "ap-shanghai", clientProfile);
        // 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
        DescribeInstancesRequest req = new DescribeInstancesRequest();
        // 填充请求参数,这里request对象的成员变量即对应接口的入参
        // 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
        // 创建Filter对象, 以zone的维度来查询cvm实例
        Filter respFilter = new Filter();
        respFilter.setName("zone");
        respFilter.setValues(new String[] { "ap-shanghai-1", "ap-shanghai-2" });
        // Filters 是成员为Filter对象的列表
        req.setFilters(new Filter[] { respFilter });
        // 这里还支持以标准json格式的string来赋值请求参数的方式。下面的代码跟上面的参数赋值是等效的
        String params = "{\"Filters\":[{\"Name\":\"zone\",\"Values\":[\"ap-shanghai-1\",\"ap-shanghai-2\"]}]}";
        req = DescribeInstancesRequest.fromJsonString(params, DescribeInstancesRequest.class);
        // 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的
        // 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应
        DescribeInstancesResponse resp = client.DescribeInstances(req);
        // 输出json格式的字符串回包
        System.out.println(DescribeInstancesResponse.toJsonString(resp));
        // 也可以取出单个值。
        // 你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
        System.out.println(resp.getTotalCount());
    } catch (TencentCloudSDKException e) {
        System.out.println(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) Filter(com.tencentcloudapi.cvm.v20170312.models.Filter) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) CvmClient(com.tencentcloudapi.cvm.v20170312.CvmClient) DescribeInstancesRequest(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest) DescribeInstancesResponse(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse)

Example 2 with CvmClient

use of com.tencentcloudapi.cvm.v20170312.CvmClient in project tencentcloud-sdk-java by TencentCloud.

the class DescribeZones method main.

public static void main(String[] args) {
    try {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        Credential cred = new Credential("secretId", "secretKey");
        // 实例化要请求产品(以cvm为例)的client对象
        CvmClient client = new CvmClient(cred, "ap-guangzhou");
        // 实例化一个请求对象
        DescribeZonesRequest req = new DescribeZonesRequest();
        // 通过client对象调用想要访问的接口,需要传入请求对象
        DescribeZonesResponse resp = client.DescribeZones(req);
        // 输出json格式的字符串回包
        System.out.println(DescribeZonesRequest.toJsonString(resp));
    } catch (TencentCloudSDKException e) {
        System.out.println(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) DescribeZonesRequest(com.tencentcloudapi.cvm.v20170312.models.DescribeZonesRequest) DescribeZonesResponse(com.tencentcloudapi.cvm.v20170312.models.DescribeZonesResponse) CvmClient(com.tencentcloudapi.cvm.v20170312.CvmClient)

Example 3 with CvmClient

use of com.tencentcloudapi.cvm.v20170312.CvmClient in project tencentcloud-sdk-java by TencentCloud.

the class Retry method main.

public static void main(String[] args) {
    try {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        Credential cred = new Credential("secretId", "secretKey");
        // 实例化要请求产品(以cvm为例)的client对象,依次传入Credential、地域
        CvmClient client = new CvmClient(cred, "ap-guangzhou");
        // 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
        DescribeInstancesRequest req = new DescribeInstancesRequest();
        // 通过client对象调用retry方法发起请求。依次传入请求对象req和重试次数
        // 注意需要将返回值强制转换成与请求对象对应的Response类型
        DescribeInstancesResponse resp = (DescribeInstancesResponse) client.retry(req, 5);
        // 输出json格式的字符串回包
        System.out.println(DescribeInstancesResponse.toJsonString(resp));
    } catch (TencentCloudSDKException e) {
        System.out.println(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) CvmClient(com.tencentcloudapi.cvm.v20170312.CvmClient) DescribeInstancesRequest(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest) DescribeInstancesResponse(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse)

Example 4 with CvmClient

use of com.tencentcloudapi.cvm.v20170312.CvmClient in project tencentcloud-sdk-java by TencentCloud.

the class SignatureTest method testGetHmacSHA1.

@Test
public void testGetHmacSHA1() {
    Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));
    HttpProfile httpProfile = new HttpProfile();
    ClientProfile clientProfile = new ClientProfile();
    clientProfile.setHttpProfile(httpProfile);
    clientProfile.setSignMethod("HmacSHA1");
    httpProfile.setReqMethod("GET");
    CvmClient client = new CvmClient(cred, "ap-guangzhou", clientProfile);
    DescribeInstancesRequest req = new DescribeInstancesRequest();
    try {
        DescribeInstancesResponse resp = client.DescribeInstances(req);
        Assert.assertTrue(resp.getTotalCount() >= 0);
    } catch (TencentCloudSDKException e) {
        fail(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) CvmClient(com.tencentcloudapi.cvm.v20170312.CvmClient) DescribeInstancesRequest(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest) DescribeInstancesResponse(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse) Test(org.junit.Test)

Example 5 with CvmClient

use of com.tencentcloudapi.cvm.v20170312.CvmClient in project tencentcloud-sdk-java-intl-en by TencentCloud.

the class DescribeInstances method main.

public static void main(String[] args) {
    try {
        Credential cred = new Credential("your-secret-id", "your-secret-key");
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("GET");
        httpProfile.setConnTimeout(30);
        httpProfile.setEndpoint("cvm.ap-shanghai.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        CvmClient client = new CvmClient(cred, "ap-shanghai", clientProfile);
        DescribeInstancesRequest req = new DescribeInstancesRequest();
        Filter respFilter = new Filter();
        respFilter.setName("zone");
        respFilter.setValues(new String[] { "ap-shanghai-1", "ap-shanghai-2" });
        req.setFilters(new Filter[] { respFilter });
        DescribeInstancesResponse resp = client.DescribeInstances(req);
        System.out.println(DescribeInstancesResponse.toJsonString(resp));
        System.out.println(resp.getTotalCount());
    } catch (TencentCloudSDKException e) {
        System.out.println(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) Filter(com.tencentcloudapi.cvm.v20170312.models.Filter) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) CvmClient(com.tencentcloudapi.cvm.v20170312.CvmClient) DescribeInstancesRequest(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest) DescribeInstancesResponse(com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse)

Aggregations

Credential (com.tencentcloudapi.common.Credential)12 TencentCloudSDKException (com.tencentcloudapi.common.exception.TencentCloudSDKException)12 CvmClient (com.tencentcloudapi.cvm.v20170312.CvmClient)12 DescribeInstancesRequest (com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest)10 DescribeInstancesResponse (com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse)10 ClientProfile (com.tencentcloudapi.common.profile.ClientProfile)8 HttpProfile (com.tencentcloudapi.common.profile.HttpProfile)8 Test (org.junit.Test)6 DescribeZonesRequest (com.tencentcloudapi.cvm.v20170312.models.DescribeZonesRequest)2 DescribeZonesResponse (com.tencentcloudapi.cvm.v20170312.models.DescribeZonesResponse)2 Filter (com.tencentcloudapi.cvm.v20170312.models.Filter)2 DefaultCredentialsProvider (com.tencentcloudapi.common.provider.DefaultCredentialsProvider)1