/
general.go
79 lines (73 loc) · 2.25 KB
/
general.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package imagex
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
common "github.com/volcengine/volc-sdk-golang/base"
)
func (c *Imagex) ImageXGet(action string, query url.Values, result interface{}) error {
respBody, _, err := c.Client.Query(action, query)
if err != nil {
return fmt.Errorf("%s: fail to do request, %v", action, err)
}
if err := unmarshalInto(respBody, result); err != nil {
return fmt.Errorf("%s: fail to unmarshal response, %v", action, err)
}
return nil
}
func (c *Imagex) ImageXPost(action string, query url.Values, req, result interface{}) error {
body, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("%s: fail to marshal request, %v", action, err)
}
data, _, err := c.Client.Json(action, query, string(body))
if err != nil {
return fmt.Errorf("%s: fail to do request, %v", action, err)
}
if err := unmarshalInto(data, result); err != nil {
return fmt.Errorf("%s: fail to unmarshal response, %v", action, err)
}
return nil
}
func (c *Imagex) GeneralGet(ctx context.Context, action, version string, query url.Values, result interface{}) error {
apiInfo := &common.ApiInfo{
Method: http.MethodGet,
Path: "/",
Query: url.Values{
"Action": []string{action},
"Version": []string{version},
},
}
respBody, _, err := c.Client.CtxQueryThumb(ctx, action, apiInfo, query)
if err != nil {
return fmt.Errorf("%s: fail to do request, %v", action, err)
}
if err := unmarshalInto(respBody, result); err != nil {
return fmt.Errorf("%s: fail to unmarshal response, %v", action, err)
}
return nil
}
func (c *Imagex) GeneralPost(ctx context.Context, action, version string, query url.Values, req, result interface{}) error {
apiInfo := &common.ApiInfo{
Method: http.MethodPost,
Path: "/",
Query: url.Values{
"Action": []string{action},
"Version": []string{version},
},
}
body, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("%s: fail to marshal request, %v", action, err)
}
data, _, err := c.Client.CtxJsonThumb(ctx, action, apiInfo, query, body)
if err != nil {
return fmt.Errorf("%s: fail to do request, %v", action, err)
}
if err := unmarshalInto(data, result); err != nil {
return fmt.Errorf("%s: fail to unmarshal response, %v", action, err)
}
return nil
}