7
0

init project

This commit is contained in:
liangzy 2020-08-06 12:15:51 +08:00
parent 2807474003
commit 0feabe2c1f
11 changed files with 121 additions and 36 deletions

View File

@ -3,16 +3,19 @@ package credentials
type BaseCredential struct { type BaseCredential struct {
AccessKeyId string AccessKeyId string
AccessKeySecret string AccessKeySecret string
AccessKeyFrom string
} }
type AccessKeyCredential struct { type AccessKeyCredential struct {
AccessKeyId string AccessKeyId string
AccessKeySecret string AccessKeySecret string
AccessKeyFrom string
} }
func (baseCred *BaseCredential) ToAccessKeyCredential() *AccessKeyCredential { func (baseCred *BaseCredential) ToAccessKeyCredential() *AccessKeyCredential {
return &AccessKeyCredential{ return &AccessKeyCredential{
AccessKeyId: baseCred.AccessKeyId, AccessKeyId: baseCred.AccessKeyId,
AccessKeySecret: baseCred.AccessKeySecret, AccessKeySecret: baseCred.AccessKeySecret,
AccessKeyFrom: baseCred.AccessKeyFrom,
} }
} }

View File

@ -10,8 +10,8 @@ import (
"time" "time"
) )
func signHttpRequest(request requests.AcsRequest, signer Signer) (err error) { func signRpcRequest(request requests.AcsRequest, signer Signer) (err error) {
err = completeHttpSignParams(request, signer) err = completeRpcSignParams(request, signer)
if err != nil { if err != nil {
return return
} }
@ -19,25 +19,37 @@ func signHttpRequest(request requests.AcsRequest, signer Signer) (err error) {
delete(request.GetQueryParams(), "sign") delete(request.GetQueryParams(), "sign")
} }
stringToSign := buildHttpStringToSign(request) stringToSign := buildRpcStringToSign(request)
request.SetStringToSign(stringToSign) request.SetStringToSign(stringToSign)
signature := signer.Sign(stringToSign, "&") signature := signer.Sign(stringToSign, "&")
request.GetQueryParams()["sign"] = signature request.GetQueryParams()["sign"] = signature
return return
} }
func completeHttpSignParams(request requests.AcsRequest, signer Signer) (err error) { func completeRpcSignParams(request requests.AcsRequest, signer Signer) (err error) {
var accessKeyFrom string
if accessKeyFrom, err = signer.GetAccessKeyFrom(); err != nil {
return
}
queryParams := request.GetQueryParams() queryParams := request.GetQueryParams()
queryParams["timestamp"] = fmt.Sprintf("%d", time.Now().Unix()) queryParams["access_time"] = fmt.Sprintf("%d", time.Now().Unix())
queryParams["access_key"], err = signer.GetAccessKeyId() queryParams["access_key"], err = signer.GetAccessKeyId()
queryParams["access_from"] = accessKeyFrom
if err != nil {
return
}
request.GetHeaders()["Content-type"] = requests.Form request.GetHeaders()["Content-type"] = requests.Form
request.GetHeaders()["Gr-Sdk-From"] = accessKeyFrom
formString := utils.GetUrlFormedMap(request.GetFormParams()) formString := utils.GetUrlFormedMap(request.GetFormParams())
request.SetContent(bytes.NewBufferString(formString).Bytes()) request.SetContent(bytes.NewBufferString(formString).Bytes())
return return
} }
func buildHttpStringToSign(request requests.AcsRequest) (stringToSign string) { func buildRpcStringToSign(request requests.AcsRequest) (stringToSign string) {
signParams := make(map[string]string) signParams := make(map[string]string)
for key, value := range request.GetQueryParams() { for key, value := range request.GetQueryParams() {
signParams[key] = value signParams[key] = value

View File

@ -11,6 +11,7 @@ import (
type Signer interface { type Signer interface {
GetName() string GetName() string
GetAccessKeyId() (string, error) GetAccessKeyId() (string, error)
GetAccessKeyFrom() (string, error)
Sign(stringToSign, secretSuffix string) string Sign(stringToSign, secretSuffix string) string
} }
@ -28,6 +29,6 @@ func NewSignerWithCredential(credential Credential, commonApi func(request *requ
func Sign(request requests.AcsRequest, signer Signer) (err error) { func Sign(request requests.AcsRequest, signer Signer) (err error) {
//TODO 根据rpc和roa两种风格签名自行选择 //TODO 根据rpc和roa两种风格签名自行选择
err = signHttpRequest(request, signer) err = signRpcRequest(request, signer)
return return
} }

View File

@ -15,6 +15,10 @@ func (signer *AccessKeySigner) GetAccessKeyId() (string, error) {
return signer.credential.AccessKeyId, nil return signer.credential.AccessKeyId, nil
} }
func (signer *AccessKeySigner) GetAccessKeyFrom() (string, error) {
return signer.credential.AccessKeyFrom, nil
}
func (*AccessKeySigner) GetName() string { func (*AccessKeySigner) GetName() string {
return "HMAC-SHA1" return "HMAC-SHA1"
} }

View File

@ -8,6 +8,7 @@ import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -35,7 +36,7 @@ type Client struct {
httpProxy string httpProxy string
httpsProxy string httpsProxy string
noProxy string noProxy string
sourceFrom string debug utils.Debug
} }
func (client *Client) GetNoProxy() string { func (client *Client) GetNoProxy() string {
@ -75,20 +76,20 @@ func (client *Client) InitClientConfig() (config *Config) {
} }
func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, source string) (err error) { func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, accessKeyFrom string) (err error) {
config := client.InitWithConfig() config := client.InitWithConfig()
credential := &credentials.BaseCredential{ credential := &credentials.BaseCredential{
AccessKeyId: accessKeyId, AccessKeyId: accessKeyId,
AccessKeySecret: accessKeySecret, AccessKeySecret: accessKeySecret,
AccessKeyFrom: accessKeyFrom,
} }
return client.InitWithOptions(source, config, credential) return client.InitWithOptions(config, credential)
} }
func (client *Client) InitWithOptions(source string, config *Config, credential auth.Credential) (err error) { func (client *Client) InitWithOptions(config *Config, credential auth.Credential) (err error) {
client.httpClient = &http.Client{} client.httpClient = &http.Client{}
client.config = config client.config = config
client.sourceFrom = source client.debug = utils.Init("sdk", true)
if config.Transport != nil { if config.Transport != nil {
client.httpClient.Transport = config.Transport client.httpClient.Transport = config.Transport
@ -279,7 +280,8 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
} }
userAgent := DefaultUserAgent userAgent := DefaultUserAgent
httpRequest.Header.Set("User-Agent", userAgent) httpRequest.Header.Set("User-Agent", userAgent)
httpRequest.Header.Set("Gr-Sdk-From", client.sourceFrom)
client.debug("%s", request.GetStringToSign())
return return
} }

View File

@ -9,7 +9,7 @@ type CommonRequest struct {
} }
func (request *CommonRequest) TransToAscRequest() { func (request *CommonRequest) TransToAscRequest() {
httpReqeust := &HttpRequest{} httpReqeust := &RpcRequest{}
httpReqeust.baseRequest = request.baseRequest httpReqeust.baseRequest = request.baseRequest
httpReqeust.product = request.Product httpReqeust.product = request.Product
request.Ontology = httpReqeust request.Ontology = httpReqeust

View File

@ -4,8 +4,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log" "math/cmplx"
"reflect" "reflect"
"strconv"
"time" "time"
) )
@ -65,6 +66,7 @@ type AcsRequest interface {
SetContent(content []byte) SetContent(content []byte)
SetDomain(host string) SetDomain(host string)
SetStringToSign(stringToSign string) SetStringToSign(stringToSign string)
GetStringToSign() string
GetBodyReader() io.Reader GetBodyReader() io.Reader
addHeaderParam(key, value string) addHeaderParam(key, value string)
@ -98,6 +100,10 @@ type baseRequest struct {
stringToSign string stringToSign string
} }
func (request *baseRequest) GetStringToSign() string {
return request.stringToSign
}
func (request *baseRequest) SetContent(content []byte) { func (request *baseRequest) SetContent(content []byte) {
request.Content = content request.Content = content
} }
@ -200,17 +206,48 @@ func defaultBaseRequest() (request *baseRequest) {
func InitParam(request AcsRequest) (err error) { func InitParam(request AcsRequest) (err error) {
reflectValue := reflect.ValueOf(request).Elem() reflectValue := reflect.ValueOf(request).Elem()
err = flatRepeatedList(reflectValue, request, "")
return nil
}
func flatRepeatedList(reflectValue reflect.Value, request AcsRequest, position string) (err error) {
reflectType := reflectValue.Type() reflectType := reflectValue.Type()
for i := 0; i < reflectType.NumField(); i++ { for i := 0; i < reflectType.NumField(); i++ {
field := reflectType.Field(i) field := reflectType.Field(i)
name, isContiansNameTag := field.Tag.Lookup("field") name, isContiansNameTag := field.Tag.Lookup("field")
fieldPosition, _ := field.Tag.Lookup("position")
log.Println(name, field) fieldPosition := position
if fieldPosition == "" {
fieldPosition, _ = field.Tag.Lookup("position")
}
fieldDefault, _ := field.Tag.Lookup("default")
if isContiansNameTag { if isContiansNameTag {
err = addParam(request, fieldPosition, name, reflectValue.Field(i).String()) var value string
switch field.Type.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value = strconv.FormatInt(reflectValue.Field(i).Int(), 10)
case reflect.Uintptr, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value = strconv.FormatUint(reflectValue.Field(i).Uint(), 10)
case reflect.Float32, reflect.Float64:
value = strconv.FormatFloat(reflectValue.Field(i).Float(), 'E', -1, 64)
case reflect.Bool:
value = strconv.FormatBool(reflectValue.Field(i).Bool())
case reflect.Complex64, reflect.Complex128:
value = fmt.Sprint(cmplx.Sqrt(reflectValue.Field(i).Complex()))
default:
value = reflectValue.Field(i).String()
}
if len(value) == 0 {
value = fieldDefault
}
err = addParam(request, fieldPosition, name, value)
} }
} }
return nil
return
} }
func addParam(request AcsRequest, position, key, value string) (err error) { func addParam(request AcsRequest, position, key, value string) (err error) {

View File

@ -7,16 +7,16 @@ import (
"strings" "strings"
) )
type HttpRequest struct { type RpcRequest struct {
*baseRequest *baseRequest
} }
func (request *HttpRequest) init() { func (request *RpcRequest) init() {
request.baseRequest = defaultBaseRequest() request.baseRequest = defaultBaseRequest()
request.Method = POST request.Method = POST
} }
func (request *HttpRequest) BuildUrl() string { func (request *RpcRequest) BuildUrl() string {
url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), request.Domain) url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), request.Domain)
if len(request.Port) > 0 { if len(request.Port) > 0 {
url = fmt.Sprintf("%s:%s", url, request.Port) url = fmt.Sprintf("%s:%s", url, request.Port)
@ -24,24 +24,25 @@ func (request *HttpRequest) BuildUrl() string {
return url + request.BuildQueries() return url + request.BuildQueries()
} }
func (request *HttpRequest) BuildQueries() string { func (request *RpcRequest) BuildQueries() string {
path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/") path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/")
request.queries = "/" + path + "?" + utils.GetUrlFormedMap(request.QueryParams) request.queries = "/" + path + "?" + utils.GetUrlFormedMap(request.QueryParams)
return request.queries return request.queries
} }
func (request *HttpRequest) GetActionName() string { func (request *RpcRequest) GetActionName() string {
return request.actionName return request.actionName
} }
func (request *HttpRequest) InitWithApiInfo(domain, version, urlPath string) { func (request *RpcRequest) InitWithApiInfo(domain, version, urlPath string) *RpcRequest {
request.init() request.init()
request.SetDomain(domain) request.SetDomain(domain)
request.version = version request.version = version
request.actionName = urlPath request.actionName = urlPath
return request
} }
func (request *HttpRequest) GetBodyReader() io.Reader { func (request *RpcRequest) GetBodyReader() io.Reader {
if request.FormParams != nil && len(request.FormParams) > 0 { if request.FormParams != nil && len(request.FormParams) > 0 {
formString := utils.GetUrlFormedMap(request.FormParams) formString := utils.GetUrlFormedMap(request.FormParams)
return strings.NewReader(formString) return strings.NewReader(formString)

10
sdk/requests/types.go Normal file
View File

@ -0,0 +1,10 @@
package requests
import "strconv"
type Interger string
func (i Interger) ToInt() int {
a, _ := strconv.ParseInt(string(i), 10, 64)
return int(a)
}

18
sdk/utils/debug.go Normal file
View File

@ -0,0 +1,18 @@
package utils
import "fmt"
type Debug func(format string, v ...interface{})
var hookPrint = func(input string) {
fmt.Println(input)
}
func Init(flag string, enable bool) Debug {
return func(format string, v ...interface{}) {
if enable {
hookPrint(fmt.Sprintf(format, v...))
}
}
}

View File

@ -6,11 +6,10 @@ import (
) )
type SendSmsRequest struct { type SendSmsRequest struct {
*requests.HttpRequest *requests.RpcRequest
User string `position:"Query" field:"user" default:"-" ` User string `position:"Query" field:"user" default:"" `
Code string `position:"Query" field:"code" default:"-" ` Code string `position:"Query" field:"code" default:"" `
Params string `position:"Query" field:"params" default:"-" ` Params string `position:"Query" field:"params" default:"" `
Test string `position:"Body" field:"test" default:"-" `
} }
type SendSmsResponseData struct { type SendSmsResponseData struct {
@ -25,12 +24,10 @@ type SendSmsResponse struct {
func CreateSendSmsRequest() (req *SendSmsRequest) { func CreateSendSmsRequest() (req *SendSmsRequest) {
req = &SendSmsRequest{ req = &SendSmsRequest{
HttpRequest: &requests.HttpRequest{}, RpcRequest: &requests.RpcRequest{},
} }
req.InitWithApiInfo(HOST, VERSION, "/api/sms/send") req.InitWithApiInfo(HOST, VERSION, "/api/sms/send")
req.Method = requests.POST req.Method = requests.GET
req.Scheme = requests.HTTP
return return
} }