| 1 |
package cs |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/md5" |
| 6 |
"encoding/base64" |
| 7 |
"encoding/json" |
| 8 |
"io" |
| 9 |
"io/ioutil" |
| 10 |
"log" |
| 11 |
"net/http" |
| 12 |
"net/url" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/denverdino/aliyungo/common" |
| 16 |
"github.com/denverdino/aliyungo/util" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
|
| 21 |
CSDefaultEndpoint = "https://cs.aliyuncs.com" |
| 22 |
CSAPIVersion = "2015-12-15" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
type Client struct { |
| 27 |
AccessKeyId string |
| 28 |
AccessKeySecret string |
| 29 |
SecurityToken string |
| 30 |
endpoint string |
| 31 |
Version string |
| 32 |
debug bool |
| 33 |
userAgent string |
| 34 |
httpClient *http.Client |
| 35 |
} |
| 36 |
|
| 37 |
type PaginationResult struct { |
| 38 |
TotalCount int `json:"total_count"` |
| 39 |
PageNumber int `json:"page_number"` |
| 40 |
PageSize int `json:"page_size"` |
| 41 |
} |
| 42 |
|
| 43 |
type Response struct { |
| 44 |
RequestId string `json:"request_id"` |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func NewClient(accessKeyId, accessKeySecret string) *Client { |
| 49 |
return &Client{ |
| 50 |
AccessKeyId: accessKeyId, |
| 51 |
AccessKeySecret: accessKeySecret, |
| 52 |
endpoint: CSDefaultEndpoint, |
| 53 |
Version: CSAPIVersion, |
| 54 |
httpClient: &http.Client{}, |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func NewClientForAussumeRole(accessKeyId, accessKeySecret, securityToken string) *Client { |
| 59 |
return &Client{ |
| 60 |
AccessKeyId: accessKeyId, |
| 61 |
AccessKeySecret: accessKeySecret, |
| 62 |
SecurityToken: securityToken, |
| 63 |
endpoint: CSDefaultEndpoint, |
| 64 |
Version: CSAPIVersion, |
| 65 |
httpClient: &http.Client{}, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (client *Client) SetDebug(debug bool) { |
| 71 |
client.debug = debug |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
func (client *Client) SetUserAgent(userAgent string) { |
| 76 |
client.userAgent = userAgent |
| 77 |
} |
| 78 |
|
| 79 |
type Request struct { |
| 80 |
Method string |
| 81 |
URL string |
| 82 |
Version string |
| 83 |
Region common.Region |
| 84 |
Signature string |
| 85 |
SignatureMethod string |
| 86 |
SignatureNonce string |
| 87 |
Timestamp util.ISO6801Time |
| 88 |
Body []byte |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (client *Client) Invoke(region common.Region, method string, path string, query url.Values, args interface{}, response interface{}) error { |
| 93 |
|
| 94 |
var reqBody []byte |
| 95 |
var err error |
| 96 |
var contentType string |
| 97 |
var contentMD5 string |
| 98 |
|
| 99 |
if args != nil { |
| 100 |
reqBody, err = json.Marshal(args) |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
contentType = "application/json" |
| 105 |
hasher := md5.New() |
| 106 |
hasher.Write(reqBody) |
| 107 |
contentMD5 = base64.StdEncoding.EncodeToString(hasher.Sum(nil)) |
| 108 |
} |
| 109 |
|
| 110 |
requestURL := client.endpoint + path |
| 111 |
if query != nil && len(query) > 0 { |
| 112 |
requestURL = requestURL + "?" + util.Encode(query) |
| 113 |
} |
| 114 |
var bodyReader io.Reader |
| 115 |
if reqBody != nil { |
| 116 |
bodyReader = bytes.NewReader(reqBody) |
| 117 |
} |
| 118 |
httpReq, err := http.NewRequest(method, requestURL, bodyReader) |
| 119 |
if err != nil { |
| 120 |
return common.GetClientError(err) |
| 121 |
} |
| 122 |
|
| 123 |
if region != "" { |
| 124 |
httpReq.Header["x-acs-region-id"] = []string{string(region)} |
| 125 |
} |
| 126 |
|
| 127 |
if contentType != "" { |
| 128 |
httpReq.Header.Set("Content-Type", contentType) |
| 129 |
} |
| 130 |
if contentMD5 != "" { |
| 131 |
httpReq.Header.Set("Content-MD5", contentMD5) |
| 132 |
} |
| 133 |
|
| 134 |
httpReq.Header.Set("Date", util.GetGMTime()) |
| 135 |
httpReq.Header.Set("Accept", "application/json") |
| 136 |
httpReq.Header["x-acs-version"] = []string{client.Version} |
| 137 |
httpReq.Header["x-acs-signature-version"] = []string{"1.0"} |
| 138 |
httpReq.Header["x-acs-signature-nonce"] = []string{util.CreateRandomString()} |
| 139 |
httpReq.Header["x-acs-signature-method"] = []string{"HMAC-SHA1"} |
| 140 |
|
| 141 |
if client.debug { |
| 142 |
log.Printf("Header = %++v", httpReq.Header) |
| 143 |
} |
| 144 |
|
| 145 |
if client.userAgent != "" { |
| 146 |
httpReq.Header.Set("User-Agent", client.userAgent) |
| 147 |
} |
| 148 |
|
| 149 |
if client.SecurityToken != "" { |
| 150 |
httpReq.Header["x-acs-security-token"] = []string{client.SecurityToken} |
| 151 |
} |
| 152 |
|
| 153 |
client.signRequest(httpReq) |
| 154 |
|
| 155 |
t0 := time.Now() |
| 156 |
httpResp, err := client.httpClient.Do(httpReq) |
| 157 |
t1 := time.Now() |
| 158 |
if err != nil { |
| 159 |
return common.GetClientError(err) |
| 160 |
} |
| 161 |
statusCode := httpResp.StatusCode |
| 162 |
|
| 163 |
if client.debug { |
| 164 |
log.Printf("Invoke %s %s %d (%v)", method, requestURL, statusCode, t1.Sub(t0)) |
| 165 |
} |
| 166 |
|
| 167 |
defer httpResp.Body.Close() |
| 168 |
body, err := ioutil.ReadAll(httpResp.Body) |
| 169 |
|
| 170 |
if err != nil { |
| 171 |
return common.GetClientError(err) |
| 172 |
} |
| 173 |
|
| 174 |
if client.debug { |
| 175 |
var prettyJSON bytes.Buffer |
| 176 |
err = json.Indent(&prettyJSON, body, "", " ") |
| 177 |
log.Println(string(prettyJSON.Bytes())) |
| 178 |
} |
| 179 |
|
| 180 |
if statusCode >= 400 && statusCode <= 599 { |
| 181 |
errorResponse := common.ErrorResponse{} |
| 182 |
err = json.Unmarshal(body, &errorResponse) |
| 183 |
ecsError := &common.Error{ |
| 184 |
ErrorResponse: errorResponse, |
| 185 |
StatusCode: statusCode, |
| 186 |
} |
| 187 |
return ecsError |
| 188 |
} |
| 189 |
|
| 190 |
if response != nil { |
| 191 |
err = json.Unmarshal(body, response) |
| 192 |
|
| 193 |
if err != nil { |
| 194 |
return common.GetClientError(err) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return nil |
| 199 |
} |