From 2807474003419133344a69ac11bf57b695e57477 Mon Sep 17 00:00:00 2001 From: liangzy Date: Thu, 6 Aug 2020 10:35:24 +0800 Subject: [PATCH] init project --- sdk/auth/http_build_param.go | 19 +++++++--- sdk/auth/signer.go | 1 + sdk/auth/signers/access_key_siginer.go | 15 ++++---- sdk/client.go | 7 ++-- sdk/requests/request.go | 48 +++++++++++++++++++++++--- services/jedi/send_sms.go | 10 ++++-- 6 files changed, 79 insertions(+), 21 deletions(-) diff --git a/sdk/auth/http_build_param.go b/sdk/auth/http_build_param.go index a9ef168..edc845d 100644 --- a/sdk/auth/http_build_param.go +++ b/sdk/auth/http_build_param.go @@ -1,10 +1,13 @@ package auth import ( + "bytes" + "fmt" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils" "net/url" "strings" + "time" ) func signHttpRequest(request requests.AcsRequest, signer Signer) (err error) { @@ -12,17 +15,25 @@ func signHttpRequest(request requests.AcsRequest, signer Signer) (err error) { if err != nil { return } - if _, isContainsSign := request.GetQueryParams()["Sign"]; isContainsSign { - delete(request.GetQueryParams(), "Sign") + if _, isContainsSign := request.GetQueryParams()["sign"]; isContainsSign { + delete(request.GetQueryParams(), "sign") } + stringToSign := buildHttpStringToSign(request) request.SetStringToSign(stringToSign) signature := signer.Sign(stringToSign, "&") - request.GetQueryParams()["Sign"] = signature + request.GetQueryParams()["sign"] = signature return } -func completeHttpSignParams(requests requests.AcsRequest, signer Signer) (err error) { +func completeHttpSignParams(request requests.AcsRequest, signer Signer) (err error) { + queryParams := request.GetQueryParams() + queryParams["timestamp"] = fmt.Sprintf("%d", time.Now().Unix()) + queryParams["access_key"], err = signer.GetAccessKeyId() + + request.GetHeaders()["Content-type"] = requests.Form + formString := utils.GetUrlFormedMap(request.GetFormParams()) + request.SetContent(bytes.NewBufferString(formString).Bytes()) return } diff --git a/sdk/auth/signer.go b/sdk/auth/signer.go index 518982a..a45d1af 100644 --- a/sdk/auth/signer.go +++ b/sdk/auth/signer.go @@ -10,6 +10,7 @@ import ( type Signer interface { GetName() string + GetAccessKeyId() (string, error) Sign(stringToSign, secretSuffix string) string } diff --git a/sdk/auth/signers/access_key_siginer.go b/sdk/auth/signers/access_key_siginer.go index 25805a1..ca17b17 100644 --- a/sdk/auth/signers/access_key_siginer.go +++ b/sdk/auth/signers/access_key_siginer.go @@ -11,23 +11,24 @@ type AccessKeySigner struct { credential *credentials.AccessKeyCredential } -func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner { - return &AccessKeySigner{ - credential: credential, - } +func (signer *AccessKeySigner) GetAccessKeyId() (string, error) { + return signer.credential.AccessKeyId, nil } - func (*AccessKeySigner) GetName() string { return "HMAC-SHA1" } - func (signer *AccessKeySigner) Sign(stringToSign, secretSuffix string) string { secret := signer.credential.AccessKeySecret + secretSuffix return ShaHmac1(stringToSign, secret) } +func NewAccessKeySigner(credential *credentials.AccessKeyCredential) *AccessKeySigner { + return &AccessKeySigner{ + credential: credential, + } +} func ShaHmac1(source, secret string) string { key := []byte(secret) @@ -37,5 +38,3 @@ func ShaHmac1(source, secret string) string { signedString := base64.StdEncoding.EncodeToString(signedBytes) return signedString } - - diff --git a/sdk/client.go b/sdk/client.go index 9ac59ee..530b381 100644 --- a/sdk/client.go +++ b/sdk/client.go @@ -35,6 +35,7 @@ type Client struct { httpProxy string httpsProxy string noProxy string + sourceFrom string } func (client *Client) GetNoProxy() string { @@ -80,13 +81,14 @@ func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, source str AccessKeyId: accessKeyId, AccessKeySecret: accessKeySecret, } - return client.InitWithOptions("", config, credential) + return client.InitWithOptions(source, config, credential) } -func (client *Client) InitWithOptions(host string, config *Config, credential auth.Credential) (err error) { +func (client *Client) InitWithOptions(source string, config *Config, credential auth.Credential) (err error) { client.httpClient = &http.Client{} client.config = config + client.sourceFrom = source if config.Transport != nil { client.httpClient.Transport = config.Transport @@ -277,6 +279,7 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer } userAgent := DefaultUserAgent httpRequest.Header.Set("User-Agent", userAgent) + httpRequest.Header.Set("Gr-Sdk-From", client.sourceFrom) return } diff --git a/sdk/requests/request.go b/sdk/requests/request.go index 70c46e2..e30751b 100644 --- a/sdk/requests/request.go +++ b/sdk/requests/request.go @@ -1,7 +1,11 @@ package requests import ( + "errors" + "fmt" "io" + "log" + "reflect" "time" ) @@ -12,6 +16,9 @@ const ( HTTP = "HTTP" HTTPS = "HTTPS" + JSON = "JSON" + XML = "XML" + DefaultHttpPort = "80" GET = "GET" @@ -55,6 +62,7 @@ type AcsRequest interface { BuildQueries() string SetScheme(scheme string) + SetContent(content []byte) SetDomain(host string) SetStringToSign(stringToSign string) GetBodyReader() io.Reader @@ -90,6 +98,10 @@ type baseRequest struct { stringToSign string } +func (request *baseRequest) SetContent(content []byte) { + request.Content = content +} + func (request *baseRequest) GetAcceptFormat() string { return request.AcceptFormat } @@ -173,13 +185,13 @@ func (request *baseRequest) addFormParam(key, val string) { func defaultBaseRequest() (request *baseRequest) { request = &baseRequest{ Scheme: HTTP, - AcceptFormat: "JSON", + AcceptFormat: JSON, Method: GET, QueryParams: make(map[string]string), Headers: map[string]string{ - "x-sdk-client": "golang/1.0.0", - "x-sdk-invoke-type": "normal", - "Accept-Encoding": "identity", + "gr-sdk-client": "golang/1.14", + "gr-sdk-invoke-type": "normal", + "Accept-Encoding": Json, }, FormParams: make(map[string]string), } @@ -187,5 +199,33 @@ func defaultBaseRequest() (request *baseRequest) { } func InitParam(request AcsRequest) (err error) { + reflectValue := reflect.ValueOf(request).Elem() + reflectType := reflectValue.Type() + for i := 0; i < reflectType.NumField(); i++ { + field := reflectType.Field(i) + name, isContiansNameTag := field.Tag.Lookup("field") + fieldPosition, _ := field.Tag.Lookup("position") + log.Println(name, field) + if isContiansNameTag { + err = addParam(request, fieldPosition, name, reflectValue.Field(i).String()) + } + } return nil } + +func addParam(request AcsRequest, position, key, value string) (err error) { + if len(value) > 0 { + switch position { + case Header: + request.addHeaderParam(key, value) + case Query: + request.addQueryParam(key, value) + case Body: + request.addFormParam(key, value) + default: + errmsg := fmt.Sprintf("unsupport positions add param `%s`", position) + err = errors.New(errmsg) + } + } + return +} diff --git a/services/jedi/send_sms.go b/services/jedi/send_sms.go index 7e3f7a9..1cc023c 100644 --- a/services/jedi/send_sms.go +++ b/services/jedi/send_sms.go @@ -7,9 +7,10 @@ import ( type SendSmsRequest struct { *requests.HttpRequest - User string `position:"Query" name:"User" default:""` - Code string `position:"Query" name:"Code" default:"-"` - Params string `position:"Query" name:"Prams" default:"-"` + User string `position:"Query" field:"user" default:"-" ` + Code string `position:"Query" field:"code" default:"-" ` + Params string `position:"Query" field:"params" default:"-" ` + Test string `position:"Body" field:"test" default:"-" ` } type SendSmsResponseData struct { @@ -26,7 +27,10 @@ func CreateSendSmsRequest() (req *SendSmsRequest) { req = &SendSmsRequest{ HttpRequest: &requests.HttpRequest{}, } + req.InitWithApiInfo(HOST, VERSION, "/api/sms/send") + req.Method = requests.POST + req.Scheme = requests.HTTP return }