@@ -9,3 +9,10 @@ type AccessKeyCredential struct { | |||||
AccessKeyId string | AccessKeyId string | ||||
AccessKeySecret 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) { | switch instance := credential.(type) { | ||||
case *credentials.AccessKeyCredential: | case *credentials.AccessKeyCredential: | ||||
signer = signers.NewAccessKeySigner(instance) | signer = signers.NewAccessKeySigner(instance) | ||||
case *credentials.BaseCredential: | |||||
signer = signers.NewAccessKeySigner(instance.ToAccessKeyCredential()) | |||||
default: | default: | ||||
err = errors.New("UnsupportedCredentialErrorCode = SDK.UnsupportedCredential") | err = errors.New("UnsupportedCredentialErrorCode = SDK.UnsupportedCredential") | ||||
} | } | ||||
@@ -21,6 +21,7 @@ import ( | |||||
var Version = "0.0.1" | var Version = "0.0.1" | ||||
var defaultConnectTimeout = 5 * time.Second | var defaultConnectTimeout = 5 * time.Second | ||||
var defaultReadTimeout = 10 * 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) | var DefaultUserAgent = fmt.Sprintf("GaoreGoSdk (%s;%s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version) | ||||
type Client struct { | 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) { | func (client *Client) InitWithOptions(host string, config *Config, credential auth.Credential) (err error) { | ||||
client.httpClient = &http.Client{} | client.httpClient = &http.Client{} | ||||
client.config = config | |||||
if config.Transport != nil { | if config.Transport != nil { | ||||
client.httpClient.Transport = config.Transport | 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) | client.signer, err = auth.NewSignerWithCredential(credential, client.ProcessCommonRequestWithSigner) | ||||
return nil | |||||
return | |||||
} | } | ||||
func (client *Client) InitWithConfig() (config *Config) { | func (client *Client) InitWithConfig() (config *Config) { | ||||
@@ -186,7 +188,6 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r | |||||
if err != nil { | if err != nil { | ||||
return err | return err | ||||
} | } | ||||
noProxy := client.getNoProxy(httpRequest.URL.Scheme) | noProxy := client.getNoProxy(httpRequest.URL.Scheme) | ||||
var flag bool | var flag bool | ||||
for _, value := range noProxy { | for _, value := range noProxy { | ||||
@@ -222,18 +223,22 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r | |||||
var httpResponse *http.Response | var httpResponse *http.Response | ||||
httpResponse, err = hookDo(client.httpClient.Do)(httpRequest) | 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 | return | ||||
} | } | ||||
func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *http.Request, err error) { | func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *http.Request, err error) { | ||||
// init param | // init param | ||||
request.SetDomain("") | |||||
domain := request.GetDomain() | |||||
if strings.Index(domain, ".") < 0 { | |||||
domain += defaultDomain | |||||
request.SetDomain(domain) | |||||
} | |||||
if request.GetScheme() == "" { | if request.GetScheme() == "" { | ||||
request.SetScheme(client.config.Scheme) | request.SetScheme(client.config.Scheme) | ||||
} | } | ||||
@@ -242,11 +247,19 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer | |||||
if err != nil { | if err != nil { | ||||
return | 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 { | if err != nil { | ||||
return | return | ||||
} | } | ||||
// build request | |||||
requestMethod := request.GetMethod() | requestMethod := request.GetMethod() | ||||
requestUrl := request.BuildUrl() | requestUrl := request.BuildUrl() | ||||
body := request.GetBodyReader() | body := request.GetBodyReader() | ||||
@@ -1,6 +1,7 @@ | |||||
package requests | package requests | ||||
import ( | import ( | ||||
"fmt" | |||||
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils" | "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils" | ||||
"io" | "io" | ||||
"strings" | "strings" | ||||
@@ -16,22 +17,28 @@ func (request *HttpRequest) init() { | |||||
} | } | ||||
func (request *HttpRequest) BuildUrl() string { | 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 { | 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 { | func (request *HttpRequest) GetActionName() string { | ||||
return request.actionName | return request.actionName | ||||
} | } | ||||
func (request *HttpRequest) InitWithApiInfo(product, version, action string) { | |||||
func (request *HttpRequest) InitWithApiInfo(domain, version, urlPath string) { | |||||
request.init() | request.init() | ||||
request.product = product | |||||
request.SetDomain(domain) | |||||
request.version = version | request.version = version | ||||
request.actionName = action | |||||
request.actionName = urlPath | |||||
} | } | ||||
func (request *HttpRequest) GetBodyReader() io.Reader { | func (request *HttpRequest) GetBodyReader() io.Reader { | ||||
@@ -48,6 +48,7 @@ type AcsRequest interface { | |||||
GetScheme() string | GetScheme() string | ||||
GetDomain() string | GetDomain() string | ||||
GetActionName() string | GetActionName() string | ||||
GetAcceptFormat() string | |||||
GetHeaders() map[string]string | GetHeaders() map[string]string | ||||
BuildUrl() string | BuildUrl() string | ||||
@@ -66,7 +67,6 @@ type AcsRequest interface { | |||||
type baseRequest struct { | type baseRequest struct { | ||||
Scheme string | Scheme string | ||||
Method string | Method string | ||||
Host string | |||||
Port string | Port string | ||||
Domain string | Domain string | ||||
From string | From string | ||||
@@ -90,6 +90,10 @@ type baseRequest struct { | |||||
stringToSign string | stringToSign string | ||||
} | } | ||||
func (request *baseRequest) GetAcceptFormat() string { | |||||
return request.AcceptFormat | |||||
} | |||||
func (request *baseRequest) GetHeaders() map[string]string { | func (request *baseRequest) GetHeaders() map[string]string { | ||||
return request.Headers | return request.Headers | ||||
} | } | ||||
@@ -168,7 +172,7 @@ func (request *baseRequest) addFormParam(key, val string) { | |||||
func defaultBaseRequest() (request *baseRequest) { | func defaultBaseRequest() (request *baseRequest) { | ||||
request = &baseRequest{ | request = &baseRequest{ | ||||
Scheme: "", | |||||
Scheme: HTTP, | |||||
AcceptFormat: "JSON", | AcceptFormat: "JSON", | ||||
Method: GET, | Method: GET, | ||||
QueryParams: make(map[string]string), | QueryParams: make(map[string]string), | ||||
@@ -1,6 +1,9 @@ | |||||
package responses | package responses | ||||
import ( | import ( | ||||
"encoding/json" | |||||
"errors" | |||||
"fmt" | |||||
"io/ioutil" | "io/ioutil" | ||||
"net/http" | "net/http" | ||||
) | ) | ||||
@@ -21,6 +24,10 @@ type BaseResponse struct { | |||||
httpContentString string | httpContentString string | ||||
httpContentBytes []byte | httpContentBytes []byte | ||||
originHttpResponse *http.Response | originHttpResponse *http.Response | ||||
Code int `json:"code"` | |||||
Status bool `json:"status"` | |||||
Msg string `json:"msg"` | |||||
} | } | ||||
func (baseResponse *BaseResponse) GetHttpStatus() int { | 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" | import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" | ||||
const ( | const ( | ||||
PRODUCT = "jedi" | |||||
HOST = "jedi.gaore.com" | |||||
VERSION = "2020-08-04" | VERSION = "2020-08-04" | ||||
) | ) | ||||
@@ -7,21 +7,26 @@ import ( | |||||
type SendSmsRequest struct { | type SendSmsRequest struct { | ||||
*requests.HttpRequest | *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 { | type SendSmsResponse struct { | ||||
*responses.BaseResponse | *responses.BaseResponse | ||||
BizId string `json:"BizId"` | |||||
Data SendSmsResponseData `json:"data"` | |||||
} | } | ||||
func CreateSendSmsRequest() (req *SendSmsRequest) { | func CreateSendSmsRequest() (req *SendSmsRequest) { | ||||
req = &SendSmsRequest{ | req = &SendSmsRequest{ | ||||
HttpRequest: &requests.HttpRequest{}, | HttpRequest: &requests.HttpRequest{}, | ||||
} | } | ||||
req.InitWithApiInfo(PRODUCT, VERSION, "/api/sms/send") | |||||
req.InitWithApiInfo(HOST, VERSION, "/api/sms/send") | |||||
return | return | ||||
} | } | ||||