-
Notifications
You must be signed in to change notification settings - Fork 196
/
common_client.go
64 lines (53 loc) · 1.69 KB
/
common_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
tchttp "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions"
"log"
"os"
)
// 目前只支持 签名v3+POST
func main() {
credential := common.NewCredential(
os.Getenv("TENCENTCLOUD_SECRET_ID"),
os.Getenv("TENCENTCLOUD_SECRET_KEY"))
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "cvm.tencentcloudapi.com"
cpf.HttpProfile.ReqMethod = "POST"
//创建common client
client := common.NewCommonClient(credential, regions.Guangzhou, cpf).WithLogger(log.Default())
// 创建common request,依次传入产品名、产品版本、接口名称
request := tchttp.NewCommonRequest("cvm", "2017-03-12", "DescribeZones")
// 自定义请求参数:
// SetActionParameters 函数支持三种数据类型的入参:
// 1. map[string]interface{}
// body:=map[string]interface{}{
// "InstanceId":"crs-xxx",
// "SpanType":2,
// }
body := map[string]interface{}{}
// // 2. string
// bodyStr := `{}`
// // 3. []byte
// bodyBytes := []byte(bodyStr)
// set custom headers
request.SetHeader(map[string]string{
"X-TC-TraceId": "ffe0c072-8a5d-4e17-8887-a8a60252abca",
})
// 设置action所需的请求数据
err := request.SetActionParameters(body)
if err != nil {
return
}
//创建common response
response := tchttp.NewCommonResponse()
//发送请求
err = client.Send(request, response)
if err != nil {
fmt.Printf("fail to invoke api: %v \n", err)
}
// 获取响应结果
fmt.Println(string(response.GetBody()))
}