Search in sources :

Example 6 with CommonClient

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

the class CommonClientTest method testOctetStream.

@Test
public void testOctetStream() {
    Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));
    CommonClient client = new CommonClient("cls", "2020-10-16", cred, "ap-guangzhou");
    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", "");
    String filePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "java" + File.separator + "com" + File.separator + "tencentcloudapi" + File.separator + "integration" + File.separator + "commonclient" + File.separator + "binary.data";
    try {
        byte[] body = Files.readAllBytes(Paths.get(filePath));
        String resp = client.callOctetStream("UploadLog", headers, body);
    } catch (TencentCloudSDKException e) {
        Assert.assertTrue(e.toString().contains("ResourceNotFound.TopicNotExist"));
    } catch (Exception e) {
        fail(e.toString());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) HashMap(java.util.HashMap) CommonClient(com.tencentcloudapi.common.CommonClient) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) Test(org.junit.Test)

Example 7 with CommonClient

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

the class CommonClientRequest method main.

public static void main(String[] args) {
    try {
        Credential cred = new Credential("secretId", "secretKey");
        CommonClient client = new CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou");
        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 8 with CommonClient

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

the class Demo method main.

/**
 *  java 调用 云api 上传日志 demo
 * @param args
 */
public static void main(String[] args) {
    // TODO 入参需要传入腾讯云账户密钥 secretId,secretKey
    Credential cred = new Credential(SECRET_ID, SECRET_KEY);
    // region 设置http选项中的参数
    HttpProfile httpProfile = new HttpProfile();
    // post请求  (默认为post请求)
    httpProfile.setReqMethod(HttpProfile.REQ_POST);
    // 指定接入地域域名(默认就近接入)
    httpProfile.setEndpoint(ENDPOINT);
    httpProfile.setRootDomain(ROOT_DOMAIN);
    // 在外网互通的网络环境下支持http协议(默认是https协议),请选择(https:// or http://)
    httpProfile.setProtocol(HttpProfile.REQ_HTTPS);
    // 设置读取超时时间,单位为秒(默认0秒)
    httpProfile.setReadTimeout(0);
    // 设置写入超时时间,单位为秒(默认0秒)
    httpProfile.setWriteTimeout(0);
    // 请求连接超时时间,单位为秒(默认60秒)
    httpProfile.setConnTimeout(HttpProfile.TM_MINUTE);
    // endregion
    // region 设置client选项中的参数
    ClientProfile clientProfile = new ClientProfile();
    clientProfile.setHttpProfile(httpProfile);
    // 指定签名算法默认("TC3-HMAC-SHA256"),它更安全但是会轻微降低性能。
    clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
    // 打印日志,默认是false
    clientProfile.setDebug(false);
    // endregion
    // region headers 和 body 信息
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put(X_CLS_TOPIC_ID, TOPIC);
    headers.put(X_CLS_HASH_KEY, "");
    // body lz4 压缩
    headers.put(X_CLS_COMPRESS_TYPE, LZ_4);
    byte[] body = compressedByte(getBodyInfo());
    try {
        CommonClient client = new CommonClient(PRODUCT_NAME_CLS, VERSION, cred, REGION, clientProfile);
        String resp = client.callOctetStream(UPLOAD_LOG_URL, headers, body);
        System.out.println(resp);
    } catch (TencentCloudSDKException te) {
        System.out.println(te.getMessage());
    }
}
Also used : Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) HashMap(java.util.HashMap) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) CommonClient(com.tencentcloudapi.common.CommonClient)

Example 9 with CommonClient

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

the class CommonClientCall method main.

public static void main(String[] args) {
    try {
        Credential cred = new Credential("secretId", "secretKey");
        CommonClient client = new CommonClient("cvm", "2017-03-12", cred, "ap-guangzhou");
        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)

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