feat: add support
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package ccc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
openapiutil "github.com/alibabacloud-go/openapi-util/service"
|
||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
)
|
||||
|
||||
const defaultEndpoint = "ccc.cn-shanghai.aliyuncs.com"
|
||||
|
||||
func createClient(accessKeyId, accessKeySecret, endpoint string) (*openapi.Client, error) {
|
||||
if endpoint == "" {
|
||||
endpoint = defaultEndpoint
|
||||
}
|
||||
cfg := &openapi.Config{
|
||||
AccessKeyId: tea.String(accessKeyId),
|
||||
AccessKeySecret: tea.String(accessKeySecret),
|
||||
Endpoint: tea.String(endpoint),
|
||||
}
|
||||
return openapi.NewClient(cfg)
|
||||
}
|
||||
|
||||
func createApiInfo(action, version string) *openapi.Params {
|
||||
return &openapi.Params{
|
||||
Action: tea.String(action),
|
||||
Version: tea.String(version),
|
||||
Protocol: tea.String("HTTPS"),
|
||||
Method: tea.String("POST"),
|
||||
AuthType: tea.String("AK"),
|
||||
Style: tea.String("RPC"),
|
||||
Pathname: tea.String("/"),
|
||||
ReqBodyType: tea.String("json"),
|
||||
BodyType: tea.String("json"),
|
||||
}
|
||||
}
|
||||
|
||||
func capitalizeKey(key string) string {
|
||||
if key == "" {
|
||||
return key
|
||||
}
|
||||
return strings.ToUpper(key[:1]) + key[1:]
|
||||
}
|
||||
|
||||
func Call(action, version, requestJSON, accessKeyId, accessKeySecret, endpoint string) (any, error) {
|
||||
client, err := createClient(accessKeyId, accessKeySecret, endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
requestStr := strings.TrimSpace(requestJSON)
|
||||
if requestStr == "" {
|
||||
requestStr = "{}"
|
||||
}
|
||||
|
||||
var requestStruct map[string]any
|
||||
if err = json.NewDecoder(strings.NewReader(requestStr)).Decode(&requestStruct); err != nil {
|
||||
return nil, fmt.Errorf("request json: %w", err)
|
||||
}
|
||||
|
||||
queries := make(map[string]any, len(requestStruct))
|
||||
for key, value := range requestStruct {
|
||||
queries[capitalizeKey(key)] = value
|
||||
}
|
||||
|
||||
res, err := client.CallApi(createApiInfo(action, version), &openapi.OpenApiRequest{
|
||||
Query: openapiutil.Query(queries),
|
||||
}, &util.RuntimeOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return res["body"], nil
|
||||
}
|
||||
Reference in New Issue
Block a user