forked from denverdino/aliyungo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
143 lines (116 loc) · 2.69 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
131
132
133
134
135
136
137
138
139
140
141
142
143
package sls
import (
"bytes"
"encoding/json"
"fmt"
"github.com/denverdino/aliyungo/util"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
type request struct {
endpoint string
method string
path string
contentType string
params map[string]string
headers map[string]string
payload []byte
}
func (req *request) url() string {
params := &url.Values{}
if req.params != nil {
for k, v := range req.params {
params.Set(k, v)
}
}
u := url.URL{
Scheme: "http",
Host: req.endpoint,
Path: req.path,
RawQuery: params.Encode(),
}
return u.String()
}
func (client *Client) doRequest(req *request) (*http.Response, error) {
payload := req.payload
if req.headers == nil {
req.headers = make(map[string]string)
}
if req.endpoint == "" {
req.endpoint = client.endpoint
}
contentLength := "0"
if payload != nil {
contentLength = strconv.Itoa(len(payload))
}
req.headers["Content-Type"] = req.contentType
req.headers["Content-Length"] = contentLength
req.headers["x-log-bodyrawsize"] = contentLength
req.headers["Date"] = util.GetGMTime()
req.headers["Host"] = req.endpoint
req.headers["x-log-apiversion"] = client.version
req.headers["x-log-signaturemethod"] = "hmac-sha1"
client.signRequest(req, payload)
var reader io.Reader
if payload != nil {
reader = bytes.NewReader(payload)
}
hreq, err := http.NewRequest(req.method, req.url(), reader)
for k, v := range req.headers {
if v != "" {
hreq.Header.Set(k, v)
}
}
if err != nil {
return nil, err
}
resp, err := client.httpClient.Do(hreq)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 && resp.StatusCode != 204 && resp.StatusCode != 206 {
return nil, buildError(resp)
}
return resp, nil
}
func (client *Client) requestWithJsonResponse(req *request, v interface{}) error {
resp, err := client.doRequest(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
func (client *Client) requestWithClose(req *request) error {
resp, err := client.doRequest(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
type Error struct {
StatusCode int
Code string `json:"errorCode,omitempty"`
Message string `json:"errorMessage,omitempty"`
}
func (err *Error) Error() string {
return fmt.Sprintf("Aliyun API Error: Status Code: %d Code: %s Message: %s", err.StatusCode, err.Code, err.Message)
}
func buildError(resp *http.Response) error {
defer resp.Body.Close()
err := &Error{}
json.NewDecoder(resp.Body).Decode(err)
err.StatusCode = resp.StatusCode
if err.Message == "" {
err.Message = resp.Status
}
return err
}