@@ -9,3 +9,10 @@ type AccessKeyCredential struct { | |||
AccessKeyId string | |||
AccessKeySecret string | |||
} | |||
func (baseCred *BaseCredential) ToAccessKeyCredential() *AccessKeyCredential { | |||
return &AccessKeyCredential{ | |||
AccessKeyId: baseCred.AccessKeyId, | |||
AccessKeySecret: baseCred.AccessKeySecret, | |||
} | |||
} |
@@ -17,6 +17,8 @@ func NewSignerWithCredential(credential Credential, commonApi func(request *requ | |||
switch instance := credential.(type) { | |||
case *credentials.AccessKeyCredential: | |||
signer = signers.NewAccessKeySigner(instance) | |||
case *credentials.BaseCredential: | |||
signer = signers.NewAccessKeySigner(instance.ToAccessKeyCredential()) | |||
default: | |||
err = errors.New("UnsupportedCredentialErrorCode = SDK.UnsupportedCredential") | |||
} | |||
@@ -21,6 +21,7 @@ import ( | |||
var Version = "0.0.1" | |||
var defaultConnectTimeout = 5 * time.Second | |||
var defaultReadTimeout = 10 * time.Second | |||
var defaultDomain = ".gaore.com" | |||
var DefaultUserAgent = fmt.Sprintf("GaoreGoSdk (%s;%s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version) | |||
type Client struct { | |||
@@ -83,7 +84,9 @@ func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, source str | |||
} | |||
func (client *Client) InitWithOptions(host string, config *Config, credential auth.Credential) (err error) { | |||
client.httpClient = &http.Client{} | |||
client.config = config | |||
if config.Transport != nil { | |||
client.httpClient.Transport = config.Transport | |||
@@ -96,8 +99,7 @@ func (client *Client) InitWithOptions(host string, config *Config, credential au | |||
} | |||
client.signer, err = auth.NewSignerWithCredential(credential, client.ProcessCommonRequestWithSigner) | |||
return nil | |||
return | |||
} | |||
func (client *Client) InitWithConfig() (config *Config) { | |||
@@ -186,7 +188,6 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r | |||
if err != nil { | |||
return err | |||
} | |||
noProxy := client.getNoProxy(httpRequest.URL.Scheme) | |||
var flag bool | |||
for _, value := range noProxy { | |||
@@ -222,18 +223,22 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r | |||
var httpResponse *http.Response | |||
httpResponse, err = hookDo(client.httpClient.Do)(httpRequest) | |||
if err == nil { | |||
for key, val := range httpResponse.Header { | |||
fmt.Println(key, val) | |||
} | |||
if err != nil { | |||
return | |||
} | |||
err = responses.Unmarshal(response, httpResponse, request.GetAcceptFormat()) | |||
return | |||
} | |||
func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *http.Request, err error) { | |||
// init param | |||
request.SetDomain("") | |||
domain := request.GetDomain() | |||
if strings.Index(domain, ".") < 0 { | |||
domain += defaultDomain | |||
request.SetDomain(domain) | |||
} | |||
if request.GetScheme() == "" { | |||
request.SetScheme(client.config.Scheme) | |||
} | |||
@@ -242,11 +247,19 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer | |||
if err != nil { | |||
return | |||
} | |||
// build request | |||
err = auth.Sign(request, signer) | |||
// build signature | |||
var finalSigner auth.Signer | |||
if signer != nil { | |||
finalSigner = signer | |||
} else { | |||
finalSigner = client.signer | |||
} | |||
err = auth.Sign(request, finalSigner) | |||
if err != nil { | |||
return | |||
} | |||
// build request | |||
requestMethod := request.GetMethod() | |||
requestUrl := request.BuildUrl() | |||
body := request.GetBodyReader() | |||
@@ -1,6 +1,7 @@ | |||
package requests | |||
import ( | |||
"fmt" | |||
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils" | |||
"io" | |||
"strings" | |||
@@ -16,22 +17,28 @@ func (request *HttpRequest) init() { | |||
} | |||
func (request *HttpRequest) BuildUrl() string { | |||
return "" | |||
url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), request.Domain) | |||
if len(request.Port) > 0 { | |||
url = fmt.Sprintf("%s:%s", url, request.Port) | |||
} | |||
return url + request.BuildQueries() | |||
} | |||
func (request *HttpRequest) BuildQueries() string { | |||
return "" | |||
path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/") | |||
request.queries = "/" + path + "?" + utils.GetUrlFormedMap(request.QueryParams) | |||
return request.queries | |||
} | |||
func (request *HttpRequest) GetActionName() string { | |||
return request.actionName | |||
} | |||
func (request *HttpRequest) InitWithApiInfo(product, version, action string) { | |||
func (request *HttpRequest) InitWithApiInfo(domain, version, urlPath string) { | |||
request.init() | |||
request.product = product | |||
request.SetDomain(domain) | |||
request.version = version | |||
request.actionName = action | |||
request.actionName = urlPath | |||
} | |||
func (request *HttpRequest) GetBodyReader() io.Reader { | |||
@@ -48,6 +48,7 @@ type AcsRequest interface { | |||
GetScheme() string | |||
GetDomain() string | |||
GetActionName() string | |||
GetAcceptFormat() string | |||
GetHeaders() map[string]string | |||
BuildUrl() string | |||
@@ -66,7 +67,6 @@ type AcsRequest interface { | |||
type baseRequest struct { | |||
Scheme string | |||
Method string | |||
Host string | |||
Port string | |||
Domain string | |||
From string | |||
@@ -90,6 +90,10 @@ type baseRequest struct { | |||
stringToSign string | |||
} | |||
func (request *baseRequest) GetAcceptFormat() string { | |||
return request.AcceptFormat | |||
} | |||
func (request *baseRequest) GetHeaders() map[string]string { | |||
return request.Headers | |||
} | |||
@@ -168,7 +172,7 @@ func (request *baseRequest) addFormParam(key, val string) { | |||
func defaultBaseRequest() (request *baseRequest) { | |||
request = &baseRequest{ | |||
Scheme: "", | |||
Scheme: HTTP, | |||
AcceptFormat: "JSON", | |||
Method: GET, | |||
QueryParams: make(map[string]string), | |||
@@ -1,6 +1,9 @@ | |||
package responses | |||
import ( | |||
"encoding/json" | |||
"errors" | |||
"fmt" | |||
"io/ioutil" | |||
"net/http" | |||
) | |||
@@ -21,6 +24,10 @@ type BaseResponse struct { | |||
httpContentString string | |||
httpContentBytes []byte | |||
originHttpResponse *http.Response | |||
Code int `json:"code"` | |||
Status bool `json:"status"` | |||
Msg string `json:"msg"` | |||
} | |||
func (baseResponse *BaseResponse) GetHttpStatus() int { | |||
@@ -74,6 +81,25 @@ func NewCommonResponse() (response *CommonResponse) { | |||
} | |||
} | |||
func Unmarshal(response AcsResponse, httpResponse *http.Response, format string) { | |||
func Unmarshal(response AcsResponse, httpResponse *http.Response, format string) (err error) { | |||
err = response.parseFromHttpResponse(httpResponse) | |||
if err != nil { | |||
return | |||
} | |||
if !response.IsSuccess() { | |||
err = errors.New(fmt.Sprintf("%d %s", response.GetHttpStatus(), response.GetHttpContentString())) | |||
return | |||
} | |||
if _, isCommonResponse := response.(CommonResponse); isCommonResponse { | |||
return | |||
} | |||
if format != "xml" { | |||
err = json.Unmarshal(response.GetHttpContentBytes(), response) | |||
if err != nil { | |||
return errors.New("json Unmarshal:" + err.Error()) | |||
} | |||
} | |||
return | |||
} |
@@ -3,7 +3,7 @@ package jedi | |||
import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" | |||
const ( | |||
PRODUCT = "jedi" | |||
HOST = "jedi.gaore.com" | |||
VERSION = "2020-08-04" | |||
) | |||
@@ -7,21 +7,26 @@ import ( | |||
type SendSmsRequest struct { | |||
*requests.HttpRequest | |||
User string `position:"Query" name:"User" default:""` | |||
Code string `position:"Query" name:"Code" default:"-"` | |||
Prams string `position:"Query" name:"Prams" default:"-"` | |||
User string `position:"Query" name:"User" default:""` | |||
Code string `position:"Query" name:"Code" default:"-"` | |||
Params string `position:"Query" name:"Prams" default:"-"` | |||
} | |||
type SendSmsResponseData struct { | |||
Account string `json:"account"` | |||
Createtime int64 `json:"createtime"` | |||
} | |||
type SendSmsResponse struct { | |||
*responses.BaseResponse | |||
BizId string `json:"BizId"` | |||
Data SendSmsResponseData `json:"data"` | |||
} | |||
func CreateSendSmsRequest() (req *SendSmsRequest) { | |||
req = &SendSmsRequest{ | |||
HttpRequest: &requests.HttpRequest{}, | |||
} | |||
req.InitWithApiInfo(PRODUCT, VERSION, "/api/sms/send") | |||
req.InitWithApiInfo(HOST, VERSION, "/api/sms/send") | |||
return | |||
} | |||