7
0

Compare commits

..

No commits in common. "13ab011852c40475f6c772d1c978c9df19f3f41a" and "b688b909e879c8ab89403ef0adfb5367897f0b94" have entirely different histories.

12 changed files with 21 additions and 379 deletions

View File

@ -9,10 +9,3 @@ type AccessKeyCredential struct {
AccessKeyId string AccessKeyId string
AccessKeySecret string AccessKeySecret string
} }
func (baseCred *BaseCredential) ToAccessKeyCredential() *AccessKeyCredential {
return &AccessKeyCredential{
AccessKeyId: baseCred.AccessKeyId,
AccessKeySecret: baseCred.AccessKeySecret,
}
}

View File

@ -17,8 +17,6 @@ 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")
} }

View File

@ -2,7 +2,6 @@ package sdk
import ( import (
"context" "context"
"crypto/tls"
"fmt" "fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
@ -10,9 +9,6 @@ import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
"net" "net"
"net/http" "net/http"
"net/url"
"os"
"regexp"
"runtime" "runtime"
"strings" "strings"
"time" "time"
@ -21,7 +17,6 @@ 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 {
@ -32,45 +27,9 @@ type Client struct {
readTimeout time.Duration readTimeout time.Duration
connectTimeout time.Duration connectTimeout time.Duration
config *Config config *Config
httpProxy string
httpsProxy string
noProxy string
}
func (client *Client) GetNoProxy() string {
return client.noProxy
}
func (client *Client) SetNoProxy(noProxy string) {
client.noProxy = noProxy
}
func (client *Client) GetHttpsProxy() string {
return client.httpsProxy
}
func (client *Client) SetHttpsProxy(httpsProxy string) {
client.httpsProxy = httpsProxy
}
func (client *Client) SetHttpProxy(httpProxy string) {
client.httpProxy = httpProxy
}
func (client *Client) GetHttpProxy() string {
return client.httpProxy
}
func (client *Client) GetHTTPSInsecure() bool {
return client.isInsecure
} }
func (client *Client) InitClientConfig() (config *Config) { func (client *Client) InitClientConfig() (config *Config) {
if client.config != nil {
return client.config
} else {
return NewConfig()
}
} }
@ -84,9 +43,7 @@ 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
@ -99,7 +56,8 @@ 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
return nil
} }
func (client *Client) InitWithConfig() (config *Config) { func (client *Client) InitWithConfig() (config *Config) {
@ -163,157 +121,10 @@ func (client *Client) getTimeOut(request requests.AcsRequest) (time.Duration, ti
return readTimeOut, connectTimeOut return readTimeOut, connectTimeOut
} }
func (client *Client) getHTTPSInsecure(request requests.AcsRequest) (insecure bool) {
if request.GetHTTPSInsecure() != nil {
insecure = *request.GetHTTPSInsecure()
} else {
insecure = client.GetHTTPSInsecure()
}
return
}
func (client *Client) DoAction(request requests.AcsRequest, response responses.AcsResponse) (err error) { func (client *Client) DoAction(request requests.AcsRequest, response responses.AcsResponse) (err error) {
return client.DoActionWithSigner(request, response, nil) return client.DoActionWithSigner(request, response, nil)
} }
func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) { func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) {
httpRequest, err := client.buildRequestWithSigner(request, signer)
if err != nil {
return err
}
client.setTimeOut(request)
proxy, err := client.getHttpProxy(httpRequest.URL.Scheme)
if err != nil {
return err
}
noProxy := client.getNoProxy(httpRequest.URL.Scheme)
var flag bool
for _, value := range noProxy {
if strings.HasPrefix(value, "*") {
value = fmt.Sprint(".%s", value)
}
noProxyReg, err := regexp.Compile(value)
if err != nil {
return err
}
if noProxyReg.MatchString(httpRequest.Host) {
flag = true
break
}
}
if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
if trans.TLSClientConfig != nil {
trans.TLSClientConfig.InsecureSkipVerify = client.getHTTPSInsecure(request)
} else {
trans.TLSClientConfig = &tls.Config{
InsecureSkipVerify: client.getHTTPSInsecure(request),
}
}
if proxy != nil && !flag {
trans.Proxy = http.ProxyURL(proxy)
}
client.httpClient.Transport = trans
}
var httpResponse *http.Response
httpResponse, err = hookDo(client.httpClient.Do)(httpRequest)
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
domain := request.GetDomain()
if strings.Index(domain, ".") < 0 {
domain += defaultDomain
request.SetDomain(domain)
}
if request.GetScheme() == "" {
request.SetScheme(client.config.Scheme)
}
err = requests.InitParam(request)
if err != nil {
return
}
// 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()
httpRequest, err = http.NewRequest(requestMethod, requestUrl, body)
if err != nil {
return
}
for key, val := range request.GetHeaders() {
httpRequest.Header[key] = []string{val}
}
if host, isContainsHost := request.GetHeaders()["host"]; isContainsHost {
httpRequest.Host = host
}
userAgent := DefaultUserAgent
httpRequest.Header.Set("User-Agent", userAgent)
return
}
func (client *Client) getHttpProxy(scheme string) (proxy *url.URL, err error) {
switch scheme {
case "https":
if client.GetHttpsProxy() != "" {
proxy, err = url.Parse(client.httpsProxy)
} else if rawurl := os.Getenv("HTTPS_PROXY"); rawurl != "" {
proxy, err = url.Parse(rawurl)
} else if rawurl := os.Getenv("https_proxy"); rawurl != "" {
proxy, err = url.Parse(rawurl)
}
default:
if client.GetHttpProxy() != "" {
proxy, err = url.Parse(client.httpProxy)
} else if rawurl := os.Getenv("HTTP_PROXY"); rawurl != "" {
proxy, err = url.Parse(rawurl)
} else if rawurl := os.Getenv("http_proxy"); rawurl != "" {
proxy, err = url.Parse(rawurl)
}
}
return
}
func (client *Client) getNoProxy(scheme string) []string {
var urls []string
if client.GetNoProxy() != "" {
urls = strings.Split(client.noProxy, ",")
} else if rawurl := os.Getenv("NO_PROXY"); rawurl != "" {
urls = strings.Split(rawurl, ",")
} else if rawurl := os.Getenv("no_proxy"); rawurl != "" {
urls = strings.Split(rawurl, ",")
}
return urls
}
func hookDo(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
return fn
} }

View File

@ -1,23 +1,16 @@
package sdk package sdk
import ( import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"net/http" "net/http"
"time" "time"
) )
type Config struct { type Config struct {
Debug bool `default:"false"` Timeout time.Duration
HttpTransport *http.Transport `default:""` HttpTransport *http.Transport `default:""`
Transport http.RoundTripper `default:""` Transport http.RoundTripper `default:""`
GoRoutinePoolSize int `default:"0"`
UserAgent string `default:""`
Scheme string `default:"HTTP"`
Timeout time.Duration `default:"5"`
} }
func NewConfig() *Config { func NewConfig() *Config {
config := &Config{} return &Config{}
utils.InitStructWithDefaultTag(config)
return config
} }

View File

@ -1,7 +1,5 @@
package requests package requests
import "io"
type CommonRequest struct { type CommonRequest struct {
*baseRequest *baseRequest
Product string Product string
@ -22,7 +20,3 @@ func (request *CommonRequest) BuildUrl() string {
func (request *CommonRequest) BuildQueries() string { func (request *CommonRequest) BuildQueries() string {
return request.Ontology.BuildQueries() return request.Ontology.BuildQueries()
} }
func (request *CommonRequest) GetBodyReader() io.Reader {
return request.Ontology.GetBodyReader()
}

View File

@ -1,12 +1,5 @@
package requests package requests
import (
"fmt"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
"io"
"strings"
)
type HttpRequest struct { type HttpRequest struct {
*baseRequest *baseRequest
} }
@ -17,35 +10,20 @@ func (request *HttpRequest) init() {
} }
func (request *HttpRequest) BuildUrl() string { func (request *HttpRequest) BuildUrl() string {
url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), request.Domain) return ""
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 {
path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/") return ""
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(domain, version, urlPath string) { func (request *HttpRequest) InitWithApiInfo(product, version, action string) {
request.init() request.init()
request.SetDomain(domain) request.product = product
request.version = version request.version = version
request.actionName = urlPath request.actionName = action
}
func (request *HttpRequest) GetBodyReader() io.Reader {
if request.FormParams != nil && len(request.FormParams) > 0 {
formString := utils.GetUrlFormedMap(request.FormParams)
return strings.NewReader(formString)
} else {
return strings.NewReader("")
}
} }

View File

@ -1,7 +1,6 @@
package requests package requests
import ( import (
"io"
"time" "time"
) )
@ -48,8 +47,6 @@ type AcsRequest interface {
GetScheme() string GetScheme() string
GetDomain() string GetDomain() string
GetActionName() string GetActionName() string
GetAcceptFormat() string
GetHeaders() map[string]string
BuildUrl() string BuildUrl() string
BuildQueries() string BuildQueries() string
@ -57,16 +54,12 @@ type AcsRequest interface {
SetScheme(scheme string) SetScheme(scheme string)
SetDomain(host string) SetDomain(host string)
SetStringToSign(stringToSign string) SetStringToSign(stringToSign string)
GetBodyReader() io.Reader
addHeaderParam(key, value string)
addQueryParam(key, value string)
addFormParam(key, value string)
} }
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,14 +83,6 @@ type baseRequest struct {
stringToSign string stringToSign string
} }
func (request *baseRequest) GetAcceptFormat() string {
return request.AcceptFormat
}
func (request *baseRequest) GetHeaders() map[string]string {
return request.Headers
}
func (request *baseRequest) GetActionName() string { func (request *baseRequest) GetActionName() string {
return request.actionName return request.actionName
} }
@ -158,21 +143,9 @@ func (request *baseRequest) SetStringToSign(stringToSign string) {
request.stringToSign = stringToSign request.stringToSign = stringToSign
} }
func (request *baseRequest) addHeaderParam(key, val string) {
request.Headers[key] = val
}
func (request *baseRequest) addQueryParam(key, val string) {
request.QueryParams[key] = val
}
func (request *baseRequest) addFormParam(key, val string) {
request.FormParams[key] = val
}
func defaultBaseRequest() (request *baseRequest) { func defaultBaseRequest() (request *baseRequest) {
request = &baseRequest{ request = &baseRequest{
Scheme: HTTP, Scheme: "",
AcceptFormat: "JSON", AcceptFormat: "JSON",
Method: GET, Method: GET,
QueryParams: make(map[string]string), QueryParams: make(map[string]string),

View File

@ -1,9 +1,6 @@
package responses package responses
import ( import (
"encoding/json"
"errors"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
) )
@ -24,10 +21,6 @@ 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 {
@ -80,26 +73,3 @@ func NewCommonResponse() (response *CommonResponse) {
BaseResponse: &BaseResponse{}, BaseResponse: &BaseResponse{},
} }
} }
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
}

View File

@ -1,13 +1,6 @@
package utils package utils
import ( import "net/url"
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
func GetUrlFormedMap(source map[string]string) (urlEncoded string) { func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
urlEncoder := url.Values{} urlEncoder := url.Values{}
@ -17,34 +10,3 @@ func GetUrlFormedMap(source map[string]string) (urlEncoded string) {
urlEncoded = urlEncoder.Encode() urlEncoded = urlEncoder.Encode()
return return
} }
func InitStructWithDefaultTag(bean interface{}) {
beantype := reflect.TypeOf(bean)
for i := 0; i < beantype.Elem().NumField(); i++ {
field := beantype.Elem().Field(i)
defaultValue := strings.TrimSpace(field.Tag.Get("default"))
if defaultValue == "" || defaultValue == "-" {
continue
}
setter := reflect.ValueOf(bean).Elem().Field(i)
fieldTypeName := field.Type.String()
switch fieldTypeName {
case "int", "int64", "int32", "int8", "int16":
intval, _ := strconv.ParseInt(defaultValue, 10, 64)
setter.SetInt(intval)
case "uint", "uint8", "uint16", "uint32", "uint64", "uintptr":
uintval, _ := strconv.ParseUint(defaultValue, 10, 64)
setter.SetUint(uintval)
case "string":
setter.SetString(defaultValue)
case "time.Duration":
intval, _ := strconv.ParseInt(defaultValue, 10, 64)
setter.SetInt(intval * int64(time.Second))
case "bool":
boolval, _ := strconv.ParseBool(defaultValue)
setter.SetBool(boolval)
default:
fmt.Println(field.Type.String(), field.Name)
}
}
}

View File

@ -1,25 +0,0 @@
package utils
import (
"fmt"
"net/http"
"testing"
"time"
)
type TestCase struct {
Debug bool `default:"false"`
HttpTransport *http.Transport `default:""`
Transport http.RoundTripper `default:""`
GoRoutinePoolSize int `default:"5"`
UserAgent string `default:""`
Scheme string `default:"HTTP"`
Haha uintptr `default:"232"`
Timeout time.Duration `default:"5"`
}
func TestInitStructWithDefaultTag(t *testing.T) {
testcase := &TestCase{}
InitStructWithDefaultTag(testcase)
fmt.Printf("%+v", testcase)
}

View File

@ -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 (
HOST = "jedi.gaore.com" PRODUCT = "jedi"
VERSION = "2020-08-04" VERSION = "2020-08-04"
) )
@ -19,6 +19,6 @@ func (c *Client) SendSms(req *SendSmsRequest) (response *SendSmsResponse, err er
func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client, err error) { func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client, err error) {
client = &Client{} client = &Client{}
err = client.InitWithAccessKey(accesskey, secrect, source) err = client.InitWithOptions("", nil, "")
return return
} }

View File

@ -9,24 +9,19 @@ type SendSmsRequest struct {
*requests.HttpRequest *requests.HttpRequest
User string `position:"Query" name:"User" default:""` User string `position:"Query" name:"User" default:""`
Code string `position:"Query" name:"Code" default:"-"` Code string `position:"Query" name:"Code" default:"-"`
Params string `position:"Query" name:"Prams" default:"-"` Prams 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
Data SendSmsResponseData `json:"data"` BizId string `json:"BizId"`
} }
func CreateSendSmsRequest() (req *SendSmsRequest) { func CreateSendSmsRequest() (req *SendSmsRequest) {
req = &SendSmsRequest{ req = &SendSmsRequest{
HttpRequest: &requests.HttpRequest{}, HttpRequest: &requests.HttpRequest{},
} }
req.InitWithApiInfo(HOST, VERSION, "/api/sms/send") req.InitWithApiInfo(PRODUCT, VERSION, "/api/sms/send")
return return
} }