| 1 | package sls |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "io/ioutil" |
| 9 | "log" |
| 10 | "net/http" |
| 11 | "net/url" |
| 12 | "strconv" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/denverdino/aliyungo/util" |
| 16 | ) |
| 17 | |
| 18 | type request struct { |
| 19 | endpoint string |
| 20 | method string |
| 21 | path string |
| 22 | contentType string |
| 23 | params map[string]string |
| 24 | headers map[string]string |
| 25 | payload []byte |
| 26 | } |
| 27 | |
| 28 | func (req *request) url() string { |
| 29 | params := &url.Values{} |
| 30 | |
| 31 | if req.params != nil { |
| 32 | for k, v := range req.params { |
| 33 | params.Set(k, v) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | u := url.URL{ |
| 38 | Scheme: "http", |
| 39 | Host: req.endpoint, |
| 40 | Path: req.path, |
| 41 | RawQuery: params.Encode(), |
| 42 | } |
| 43 | return u.String() |
| 44 | } |
| 45 | |
| 46 | func (client *Client) doRequest(req *request) (*http.Response, error) { |
| 47 | |
| 48 | payload := req.payload |
| 49 | |
| 50 | if req.headers == nil { |
| 51 | req.headers = make(map[string]string) |
| 52 | } |
| 53 | |
| 54 | if req.endpoint == "" { |
| 55 | req.endpoint = client.endpoint |
| 56 | } |
| 57 | |
| 58 | contentLength := "0" |
| 59 | |
| 60 | if payload != nil { |
| 61 | contentLength = strconv.Itoa(len(payload)) |
| 62 | } |
| 63 | |
| 64 | req.headers["Content-Type"] = req.contentType |
| 65 | req.headers["Content-Length"] = contentLength |
| 66 | req.headers["x-log-bodyrawsize"] = contentLength |
| 67 | req.headers["Date"] = util.GetGMTime() |
| 68 | req.headers["Host"] = req.endpoint |
| 69 | req.headers["x-log-apiversion"] = client.version |
| 70 | req.headers["x-log-signaturemethod"] = "hmac-sha1" |
| 71 | if client.securityToken != "" { |
| 72 | req.headers["x-acs-security-token"] = client.securityToken |
| 73 | } |
| 74 | |
| 75 | client.signRequest(req, payload) |
| 76 | |
| 77 | var reader io.Reader |
| 78 | |
| 79 | if payload != nil { |
| 80 | reader = bytes.NewReader(payload) |
| 81 | } |
| 82 | |
| 83 | hreq, err := http.NewRequest(req.method, req.url(), reader) |
| 84 | |
| 85 | for k, v := range req.headers { |
| 86 | if v != "" { |
| 87 | hreq.Header.Set(k, v) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | t0 := time.Now() |
| 95 | resp, err := client.httpClient.Do(hreq) |
| 96 | t1 := time.Now() |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | |
| 101 | if client.debug { |
| 102 | log.Printf("Invoke %s %s %d (%v)", req.method, req.url(), resp.StatusCode, t1.Sub(t0)) |
| 103 | } |
| 104 | |
| 105 | if resp.StatusCode != 200 && resp.StatusCode != 204 && resp.StatusCode != 206 { |
| 106 | return nil, buildError(resp) |
| 107 | } |
| 108 | return resp, nil |
| 109 | } |
| 110 | |
| 111 | func (client *Client) requestWithJsonResponse(req *request, v interface{}) error { |
| 112 | resp, err := client.doRequest(req) |
| 113 | |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | defer resp.Body.Close() |
| 118 | data, err := ioutil.ReadAll(resp.Body) |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | |
| 123 | return json.Unmarshal(data, v) |
| 124 | } |
| 125 | |
| 126 | func (client *Client) requestWithClose(req *request) error { |
| 127 | resp, err := client.doRequest(req) |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | |
| 132 | resp.Body.Close() |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | type Error struct { |
| 137 | StatusCode int |
| 138 | Code string `json:"errorCode,omitempty"` |
| 139 | Message string `json:"errorMessage,omitempty"` |
| 140 | } |
| 141 | |
| 142 | func (err *Error) Error() string { |
| 143 | return fmt.Sprintf("Aliyun API Error: Status Code: %d Code: %s Message: %s", err.StatusCode, err.Code, err.Message) |
| 144 | } |
| 145 | |
| 146 | func buildError(resp *http.Response) error { |
| 147 | defer resp.Body.Close() |
| 148 | err := &Error{} |
| 149 | json.NewDecoder(resp.Body).Decode(err) |
| 150 | err.StatusCode = resp.StatusCode |
| 151 | if err.Message == "" { |
| 152 | err.Message = resp.Status |
| 153 | } |
| 154 | return err |
| 155 | } |