2017-03-03 04:19:16 -05:00
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
2017-11-27 21:22:16 -05:00
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2017-03-03 04:19:16 -05:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
2017-11-27 21:22:16 -05:00
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
2018-04-18 11:41:56 -04:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2017-03-03 04:19:16 -05:00
|
|
|
|
|
|
|
|
|
"github.com/denverdino/aliyungo/util"
|
|
|
|
|
)
|
|
|
|
|
|
2017-05-25 06:27:54 -04:00
|
|
|
|
// RemovalPolicy.N add index to array item
|
|
|
|
|
// RemovalPolicy=["a", "b"] => RemovalPolicy.1="a" RemovalPolicy.2="b"
|
|
|
|
|
type FlattenArray []string
|
|
|
|
|
|
|
|
|
|
// string contains underline which will be replaced with dot
|
|
|
|
|
// SystemDisk_Category => SystemDisk.Category
|
|
|
|
|
type UnderlineString string
|
|
|
|
|
|
2017-03-03 04:19:16 -05:00
|
|
|
|
// A Client represents a client of ECS services
|
|
|
|
|
type Client struct {
|
|
|
|
|
AccessKeyId string //Access Key Id
|
|
|
|
|
AccessKeySecret string //Access Key Secret
|
2017-11-27 21:22:16 -05:00
|
|
|
|
securityToken string
|
2017-03-03 04:19:16 -05:00
|
|
|
|
debug bool
|
|
|
|
|
httpClient *http.Client
|
|
|
|
|
endpoint string
|
|
|
|
|
version string
|
2017-03-04 05:06:32 -05:00
|
|
|
|
serviceCode string
|
|
|
|
|
regionID Region
|
|
|
|
|
businessInfo string
|
2017-11-27 21:22:16 -05:00
|
|
|
|
userAgent string
|
2017-03-03 04:19:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
|
// Initialize properties of a client instance
|
2017-03-03 04:19:16 -05:00
|
|
|
|
func (client *Client) Init(endpoint, version, accessKeyId, accessKeySecret string) {
|
|
|
|
|
client.AccessKeyId = accessKeyId
|
2018-04-18 11:41:56 -04:00
|
|
|
|
ak := accessKeySecret
|
|
|
|
|
if !strings.HasSuffix(ak, "&") {
|
|
|
|
|
ak += "&"
|
|
|
|
|
}
|
|
|
|
|
client.AccessKeySecret = ak
|
2017-03-03 04:19:16 -05:00
|
|
|
|
client.debug = false
|
2017-11-27 21:22:16 -05:00
|
|
|
|
handshakeTimeout, err := strconv.Atoi(os.Getenv("TLSHandshakeTimeout"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
handshakeTimeout = 0
|
|
|
|
|
}
|
|
|
|
|
if handshakeTimeout == 0 {
|
|
|
|
|
client.httpClient = &http.Client{}
|
|
|
|
|
} else {
|
|
|
|
|
t := &http.Transport{
|
2018-04-18 11:41:56 -04:00
|
|
|
|
TLSHandshakeTimeout: time.Duration(handshakeTimeout) * time.Second}
|
|
|
|
|
client.httpClient = &http.Client{Transport: t}
|
2017-11-27 21:22:16 -05:00
|
|
|
|
}
|
2017-03-03 04:19:16 -05:00
|
|
|
|
client.endpoint = endpoint
|
|
|
|
|
client.version = version
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
|
// Initialize properties of a client instance including regionID
|
2017-03-04 05:06:32 -05:00
|
|
|
|
func (client *Client) NewInit(endpoint, version, accessKeyId, accessKeySecret, serviceCode string, regionID Region) {
|
|
|
|
|
client.Init(endpoint, version, accessKeyId, accessKeySecret)
|
|
|
|
|
client.serviceCode = serviceCode
|
|
|
|
|
client.regionID = regionID
|
2018-04-18 11:41:56 -04:00
|
|
|
|
client.setEndpointByLocation(regionID, serviceCode, accessKeyId, accessKeySecret, client.securityToken)
|
2017-03-04 05:06:32 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
|
// Intialize client object when all properties are ready
|
|
|
|
|
func (client *Client) InitClient() *Client {
|
|
|
|
|
client.debug = false
|
|
|
|
|
handshakeTimeout, err := strconv.Atoi(os.Getenv("TLSHandshakeTimeout"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
handshakeTimeout = 0
|
|
|
|
|
}
|
|
|
|
|
if handshakeTimeout == 0 {
|
|
|
|
|
client.httpClient = &http.Client{}
|
|
|
|
|
} else {
|
|
|
|
|
t := &http.Transport{
|
2018-04-18 11:41:56 -04:00
|
|
|
|
TLSHandshakeTimeout: time.Duration(handshakeTimeout) * time.Second}
|
|
|
|
|
client.httpClient = &http.Client{Transport: t}
|
2017-11-27 21:22:16 -05:00
|
|
|
|
}
|
2018-04-18 11:41:56 -04:00
|
|
|
|
client.setEndpointByLocation(client.regionID, client.serviceCode, client.AccessKeyId, client.AccessKeySecret, client.securityToken)
|
2017-11-27 21:22:16 -05:00
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 11:41:56 -04:00
|
|
|
|
func (client *Client) NewInitForAssumeRole(endpoint, version, accessKeyId, accessKeySecret, serviceCode string, regionID Region, securityToken string) {
|
|
|
|
|
client.NewInit(endpoint, version, accessKeyId, accessKeySecret, serviceCode, regionID)
|
|
|
|
|
client.securityToken = securityToken
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
|
//NewClient using location service
|
2018-04-18 11:41:56 -04:00
|
|
|
|
func (client *Client) setEndpointByLocation(region Region, serviceCode, accessKeyId, accessKeySecret, securityToken string) {
|
|
|
|
|
locationClient := NewLocationClient(accessKeyId, accessKeySecret, securityToken)
|
2017-03-04 05:06:32 -05:00
|
|
|
|
ep := locationClient.DescribeOpenAPIEndpoint(region, serviceCode)
|
|
|
|
|
if ep == "" {
|
|
|
|
|
ep = loadEndpointFromFile(region, serviceCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ep != "" {
|
|
|
|
|
client.endpoint = ep
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
|
// Ensure all necessary properties are valid
|
|
|
|
|
func (client *Client) ensureProperties() error {
|
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
|
|
if client.endpoint == "" {
|
|
|
|
|
msg = fmt.Sprintf("endpoint cannot be empty!")
|
|
|
|
|
} else if client.version == "" {
|
|
|
|
|
msg = fmt.Sprintf("version cannot be empty!")
|
|
|
|
|
} else if client.AccessKeyId == "" {
|
|
|
|
|
msg = fmt.Sprintf("AccessKeyId cannot be empty!")
|
|
|
|
|
} else if client.AccessKeySecret == "" {
|
|
|
|
|
msg = fmt.Sprintf("AccessKeySecret cannot be empty!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if msg != "" {
|
|
|
|
|
return errors.New(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------
|
|
|
|
|
// WithXXX methods
|
|
|
|
|
// ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// WithEndpoint sets custom endpoint
|
|
|
|
|
func (client *Client) WithEndpoint(endpoint string) *Client {
|
|
|
|
|
client.SetEndpoint(endpoint)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithVersion sets custom version
|
|
|
|
|
func (client *Client) WithVersion(version string) *Client {
|
|
|
|
|
client.SetVersion(version)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithRegionID sets Region ID
|
|
|
|
|
func (client *Client) WithRegionID(regionID Region) *Client {
|
|
|
|
|
client.SetRegionID(regionID)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//WithServiceCode sets serviceCode
|
|
|
|
|
func (client *Client) WithServiceCode(serviceCode string) *Client {
|
|
|
|
|
client.SetServiceCode(serviceCode)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithAccessKeyId sets new AccessKeyId
|
|
|
|
|
func (client *Client) WithAccessKeyId(id string) *Client {
|
|
|
|
|
client.SetAccessKeyId(id)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithAccessKeySecret sets new AccessKeySecret
|
|
|
|
|
func (client *Client) WithAccessKeySecret(secret string) *Client {
|
|
|
|
|
client.SetAccessKeySecret(secret)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithSecurityToken sets securityToken
|
|
|
|
|
func (client *Client) WithSecurityToken(securityToken string) *Client {
|
|
|
|
|
client.SetSecurityToken(securityToken)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithDebug sets debug mode to log the request/response message
|
|
|
|
|
func (client *Client) WithDebug(debug bool) *Client {
|
|
|
|
|
client.SetDebug(debug)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithBusinessInfo sets business info to log the request/response message
|
|
|
|
|
func (client *Client) WithBusinessInfo(businessInfo string) *Client {
|
|
|
|
|
client.SetBusinessInfo(businessInfo)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithUserAgent sets user agent to the request/response message
|
|
|
|
|
func (client *Client) WithUserAgent(userAgent string) *Client {
|
|
|
|
|
client.SetUserAgent(userAgent)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------
|
|
|
|
|
// SetXXX methods
|
|
|
|
|
// ----------------------------------------------------
|
|
|
|
|
|
2017-03-03 04:19:16 -05:00
|
|
|
|
// SetEndpoint sets custom endpoint
|
|
|
|
|
func (client *Client) SetEndpoint(endpoint string) {
|
|
|
|
|
client.endpoint = endpoint
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetEndpoint sets custom version
|
|
|
|
|
func (client *Client) SetVersion(version string) {
|
|
|
|
|
client.version = version
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
|
// SetEndpoint sets Region ID
|
2017-03-04 05:06:32 -05:00
|
|
|
|
func (client *Client) SetRegionID(regionID Region) {
|
|
|
|
|
client.regionID = regionID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//SetServiceCode sets serviceCode
|
|
|
|
|
func (client *Client) SetServiceCode(serviceCode string) {
|
|
|
|
|
client.serviceCode = serviceCode
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 04:19:16 -05:00
|
|
|
|
// SetAccessKeyId sets new AccessKeyId
|
|
|
|
|
func (client *Client) SetAccessKeyId(id string) {
|
|
|
|
|
client.AccessKeyId = id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetAccessKeySecret sets new AccessKeySecret
|
|
|
|
|
func (client *Client) SetAccessKeySecret(secret string) {
|
|
|
|
|
client.AccessKeySecret = secret + "&"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetDebug sets debug mode to log the request/response message
|
|
|
|
|
func (client *Client) SetDebug(debug bool) {
|
|
|
|
|
client.debug = debug
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
|
// SetBusinessInfo sets business info to log the request/response message
|
|
|
|
|
func (client *Client) SetBusinessInfo(businessInfo string) {
|
|
|
|
|
if strings.HasPrefix(businessInfo, "/") {
|
|
|
|
|
client.businessInfo = businessInfo
|
|
|
|
|
} else if businessInfo != "" {
|
|
|
|
|
client.businessInfo = "/" + businessInfo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 21:56:04 -04:00
|
|
|
|
// SetUserAgent sets user agent to the request/response message
|
|
|
|
|
func (client *Client) SetUserAgent(userAgent string) {
|
|
|
|
|
client.userAgent = userAgent
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 11:41:56 -04:00
|
|
|
|
//set SecurityToken
|
|
|
|
|
func (client *Client) SetSecurityToken(securityToken string) {
|
|
|
|
|
client.securityToken = securityToken
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 04:19:16 -05:00
|
|
|
|
// Invoke sends the raw HTTP request for ECS services
|
|
|
|
|
func (client *Client) Invoke(action string, args interface{}, response interface{}) error {
|
2017-11-27 21:22:16 -05:00
|
|
|
|
if err := client.ensureProperties(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-03-03 04:19:16 -05:00
|
|
|
|
|
|
|
|
|
request := Request{}
|
2017-11-27 21:22:16 -05:00
|
|
|
|
request.init(client.version, action, client.AccessKeyId, client.securityToken, client.regionID)
|
2017-03-03 04:19:16 -05:00
|
|
|
|
|
|
|
|
|
query := util.ConvertToQueryValues(request)
|
|
|
|
|
util.SetQueryValues(args, &query)
|
|
|
|
|
|
|
|
|
|
// Sign request
|
|
|
|
|
signature := util.CreateSignatureForRequest(ECSRequestMethod, &query, client.AccessKeySecret)
|
|
|
|
|
|
|
|
|
|
// Generate the request URL
|
|
|
|
|
requestURL := client.endpoint + "?" + query.Encode() + "&Signature=" + url.QueryEscape(signature)
|
|
|
|
|
|
|
|
|
|
httpReq, err := http.NewRequest(ECSRequestMethod, requestURL, nil)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO move to util and add build val flag
|
2017-03-04 05:06:32 -05:00
|
|
|
|
httpReq.Header.Set("X-SDK-Client", `AliyunGO/`+Version+client.businessInfo)
|
2018-04-18 11:41:56 -04:00
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
|
httpReq.Header.Set("User-Agent", httpReq.UserAgent()+" "+client.userAgent)
|
2017-08-03 21:56:04 -04:00
|
|
|
|
|
2017-03-03 04:19:16 -05:00
|
|
|
|
t0 := time.Now()
|
|
|
|
|
httpResp, err := client.httpClient.Do(httpReq)
|
|
|
|
|
t1 := time.Now()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
statusCode := httpResp.StatusCode
|
|
|
|
|
|
|
|
|
|
if client.debug {
|
|
|
|
|
log.Printf("Invoke %s %s %d (%v)", ECSRequestMethod, requestURL, statusCode, t1.Sub(t0))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer httpResp.Body.Close()
|
|
|
|
|
body, err := ioutil.ReadAll(httpResp.Body)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if client.debug {
|
|
|
|
|
var prettyJSON bytes.Buffer
|
|
|
|
|
err = json.Indent(&prettyJSON, body, "", " ")
|
2017-05-25 06:27:54 -04:00
|
|
|
|
log.Println(string(prettyJSON.Bytes()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if statusCode >= 400 && statusCode <= 599 {
|
|
|
|
|
errorResponse := ErrorResponse{}
|
|
|
|
|
err = json.Unmarshal(body, &errorResponse)
|
|
|
|
|
ecsError := &Error{
|
|
|
|
|
ErrorResponse: errorResponse,
|
|
|
|
|
StatusCode: statusCode,
|
|
|
|
|
}
|
|
|
|
|
return ecsError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, response)
|
|
|
|
|
//log.Printf("%++v", response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Invoke sends the raw HTTP request for ECS services
|
|
|
|
|
func (client *Client) InvokeByFlattenMethod(action string, args interface{}, response interface{}) error {
|
2017-11-27 21:22:16 -05:00
|
|
|
|
if err := client.ensureProperties(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-05-25 06:27:54 -04:00
|
|
|
|
|
|
|
|
|
request := Request{}
|
2017-11-27 21:22:16 -05:00
|
|
|
|
request.init(client.version, action, client.AccessKeyId, client.securityToken, client.regionID)
|
2017-05-25 06:27:54 -04:00
|
|
|
|
|
|
|
|
|
query := util.ConvertToQueryValues(request)
|
|
|
|
|
|
|
|
|
|
util.SetQueryValueByFlattenMethod(args, &query)
|
|
|
|
|
|
|
|
|
|
// Sign request
|
|
|
|
|
signature := util.CreateSignatureForRequest(ECSRequestMethod, &query, client.AccessKeySecret)
|
|
|
|
|
|
|
|
|
|
// Generate the request URL
|
|
|
|
|
requestURL := client.endpoint + "?" + query.Encode() + "&Signature=" + url.QueryEscape(signature)
|
|
|
|
|
|
|
|
|
|
httpReq, err := http.NewRequest(ECSRequestMethod, requestURL, nil)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO move to util and add build val flag
|
|
|
|
|
httpReq.Header.Set("X-SDK-Client", `AliyunGO/`+Version+client.businessInfo)
|
2018-04-18 11:41:56 -04:00
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
|
httpReq.Header.Set("User-Agent", httpReq.UserAgent()+" "+client.userAgent)
|
2017-08-03 21:56:04 -04:00
|
|
|
|
|
2017-05-25 06:27:54 -04:00
|
|
|
|
t0 := time.Now()
|
|
|
|
|
httpResp, err := client.httpClient.Do(httpReq)
|
|
|
|
|
t1 := time.Now()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
statusCode := httpResp.StatusCode
|
|
|
|
|
|
|
|
|
|
if client.debug {
|
|
|
|
|
log.Printf("Invoke %s %s %d (%v)", ECSRequestMethod, requestURL, statusCode, t1.Sub(t0))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer httpResp.Body.Close()
|
|
|
|
|
body, err := ioutil.ReadAll(httpResp.Body)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if client.debug {
|
|
|
|
|
var prettyJSON bytes.Buffer
|
|
|
|
|
err = json.Indent(&prettyJSON, body, "", " ")
|
2017-03-03 04:19:16 -05:00
|
|
|
|
log.Println(string(prettyJSON.Bytes()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if statusCode >= 400 && statusCode <= 599 {
|
|
|
|
|
errorResponse := ErrorResponse{}
|
|
|
|
|
err = json.Unmarshal(body, &errorResponse)
|
|
|
|
|
ecsError := &Error{
|
|
|
|
|
ErrorResponse: errorResponse,
|
|
|
|
|
StatusCode: statusCode,
|
|
|
|
|
}
|
|
|
|
|
return ecsError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, response)
|
|
|
|
|
//log.Printf("%++v", response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Invoke sends the raw HTTP request for ECS services
|
|
|
|
|
//改进了一下上面那个方法,可以使用各种Http方法
|
|
|
|
|
//2017.1.30 增加了一个path参数,用来拓展访问的地址
|
|
|
|
|
func (client *Client) InvokeByAnyMethod(method, action, path string, args interface{}, response interface{}) error {
|
2017-11-27 21:22:16 -05:00
|
|
|
|
if err := client.ensureProperties(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-03-03 04:19:16 -05:00
|
|
|
|
|
|
|
|
|
request := Request{}
|
2017-11-27 21:22:16 -05:00
|
|
|
|
request.init(client.version, action, client.AccessKeyId, client.securityToken, client.regionID)
|
2017-03-03 04:19:16 -05:00
|
|
|
|
data := util.ConvertToQueryValues(request)
|
|
|
|
|
util.SetQueryValues(args, &data)
|
|
|
|
|
|
|
|
|
|
// Sign request
|
|
|
|
|
signature := util.CreateSignatureForRequest(method, &data, client.AccessKeySecret)
|
|
|
|
|
|
|
|
|
|
data.Add("Signature", signature)
|
|
|
|
|
// Generate the request URL
|
|
|
|
|
var (
|
|
|
|
|
httpReq *http.Request
|
2017-03-04 05:06:32 -05:00
|
|
|
|
err error
|
2017-03-03 04:19:16 -05:00
|
|
|
|
)
|
|
|
|
|
if method == http.MethodGet {
|
|
|
|
|
requestURL := client.endpoint + path + "?" + data.Encode()
|
|
|
|
|
//fmt.Println(requestURL)
|
|
|
|
|
httpReq, err = http.NewRequest(method, requestURL, nil)
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Println(client.endpoint + path)
|
2017-03-04 05:06:32 -05:00
|
|
|
|
httpReq, err = http.NewRequest(method, client.endpoint+path, strings.NewReader(data.Encode()))
|
2017-03-03 04:19:16 -05:00
|
|
|
|
httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO move to util and add build val flag
|
2017-03-04 05:06:32 -05:00
|
|
|
|
httpReq.Header.Set("X-SDK-Client", `AliyunGO/`+Version+client.businessInfo)
|
2017-11-27 21:22:16 -05:00
|
|
|
|
httpReq.Header.Set("User-Agent", httpReq.Header.Get("User-Agent")+" "+client.userAgent)
|
2017-08-03 21:56:04 -04:00
|
|
|
|
|
2017-03-03 04:19:16 -05:00
|
|
|
|
t0 := time.Now()
|
|
|
|
|
httpResp, err := client.httpClient.Do(httpReq)
|
|
|
|
|
t1 := time.Now()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
statusCode := httpResp.StatusCode
|
|
|
|
|
|
|
|
|
|
if client.debug {
|
|
|
|
|
log.Printf("Invoke %s %s %d (%v) %v", ECSRequestMethod, client.endpoint, statusCode, t1.Sub(t0), data.Encode())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer httpResp.Body.Close()
|
|
|
|
|
body, err := ioutil.ReadAll(httpResp.Body)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if client.debug {
|
|
|
|
|
var prettyJSON bytes.Buffer
|
|
|
|
|
err = json.Indent(&prettyJSON, body, "", " ")
|
|
|
|
|
log.Println(string(prettyJSON.Bytes()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if statusCode >= 400 && statusCode <= 599 {
|
|
|
|
|
errorResponse := ErrorResponse{}
|
|
|
|
|
err = json.Unmarshal(body, &errorResponse)
|
|
|
|
|
ecsError := &Error{
|
|
|
|
|
ErrorResponse: errorResponse,
|
|
|
|
|
StatusCode: statusCode,
|
|
|
|
|
}
|
|
|
|
|
return ecsError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, response)
|
|
|
|
|
//log.Printf("%++v", response)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return GetClientError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateClientToken generates the Client Token with random string
|
|
|
|
|
func (client *Client) GenerateClientToken() string {
|
|
|
|
|
return util.CreateRandomString()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetClientErrorFromString(str string) error {
|
|
|
|
|
return &Error{
|
|
|
|
|
ErrorResponse: ErrorResponse{
|
|
|
|
|
Code: "AliyunGoClientFailure",
|
|
|
|
|
Message: str,
|
|
|
|
|
},
|
|
|
|
|
StatusCode: -1,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetClientError(err error) error {
|
|
|
|
|
return GetClientErrorFromString(err.Error())
|
|
|
|
|
}
|