This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
request.go
executable file
·130 lines (112 loc) · 3.06 KB
/
request.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ali
import (
"crypto/md5"
"encoding/json"
"fmt"
"net/http"
"github.com/go-resty/resty/v2"
)
const requestIDHeader = "x-log-requestid"
const (
httpScheme = "http://"
httpsScheme = "https://"
)
type badResError struct {
body string
header map[string][]string
code int
}
func (e badResError) String() string {
b, err := json.MarshalIndent(e, "", " ")
if err != nil {
return ""
}
return string(b)
}
func (e badResError) Error() string {
return e.String()
}
func newBadResError(body []byte, header map[string][]string, httpCode int) *badResError {
return &badResError{
body: string(body),
header: header,
code: httpCode,
}
}
type cliError struct {
HTTPCode int `json:"httpCode"`
Code string `json:"errorCode"`
Message string `json:"errorMessage"`
RequestID string `json:"requestID"`
}
func (e cliError) String() string {
b, err := json.MarshalIndent(e, "", " ")
if err != nil {
return ""
}
return string(b)
}
func (e cliError) Error() string {
return e.String()
}
// newClientError new client error
func newClientError(err error) *cliError {
if err == nil {
return nil
}
if clientError, ok := err.(*cliError); ok {
return clientError
}
clientError := new(cliError)
clientError.HTTPCode = -1
clientError.Code = "ClientError"
clientError.Message = err.Error()
return clientError
}
// request sends a request to alibaba cloud Log Service.
// @note if error is nil, you must call http.Response.Body.Close() to finalize reader
func (p *logProject) request(method, uri string, headers map[string]string, body []byte) (*resty.Response, error) {
// The caller should provide 'x-log-bodyrawsize' header
if _, ok := headers["x-log-bodyrawsize"]; !ok {
return nil, newClientError(fmt.Errorf("can't find 'x-log-bodyrawsize' header"))
}
// headers["Host"] = strings.Trim(p.Host, "http://")
headers["Date"] = nowRFC1123()
headers["x-log-apiversion"] = version
headers["x-log-signaturemethod"] = signatureMethod
headers["User-Agent"] = "ego-sdk"
// Access with token
if p.securityToken != "" {
headers["x-acs-security-token"] = p.securityToken
}
if body != nil {
bodyMD5 := fmt.Sprintf("%X", md5.Sum(body))
headers["Content-MD5"] = bodyMD5
if _, ok := headers["Content-Type"]; !ok {
return nil, newClientError(fmt.Errorf("can't find 'Content-Type' header"))
}
}
// Calc Authorization
// Authorization = "SLS <AccessKeyId>:<Signature>"
digest, err := signature(p.accessKeySecret, method, uri, headers)
if err != nil {
return nil, newClientError(err)
}
auth := fmt.Sprintf("SLS %v:%v", p.accessKeyID, digest)
headers["Authorization"] = auth
res, err := p.cli.R().SetHeaders(headers).SetBody(body).Execute(method, uri)
if err != nil {
return nil, err
}
// Parse the ali error from body.
if res.StatusCode() != http.StatusOK {
err := &cliError{}
err.HTTPCode = res.StatusCode()
if jErr := json.Unmarshal(res.Body(), err); jErr != nil {
return nil, newBadResError(res.Body(), res.Header(), res.StatusCode())
}
err.RequestID = res.Header().Get(requestIDHeader)
return nil, err
}
return res, nil
}