From 1f82a534c32844127db2c9c6b81eb66cd3f8bcf5 Mon Sep 17 00:00:00 2001 From: liangzy Date: Wed, 23 Sep 2020 20:23:35 +0800 Subject: [PATCH] =?UTF-8?q?3.=20=E5=A2=9E=E5=8A=A0=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E8=AF=B7=E6=B1=82=20=E5=AF=B9=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84header=20=E5=92=8C=20=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/auth/rali_signature_composer.go | 4 ++++ sdk/client.go | 16 +++++++++++--- sdk/config.go | 1 + sdk/requests/common_request.go | 2 +- sdk/requests/request.go | 33 +++++++++++++++++++++++------ sdk/requests/rpc_request.go | 12 +++++++++-- services/jedi/client.go | 20 +++++++++++++---- services/sso/client.go | 10 +++++++-- 8 files changed, 80 insertions(+), 18 deletions(-) diff --git a/sdk/auth/rali_signature_composer.go b/sdk/auth/rali_signature_composer.go index e4fbd2c..37cd3d1 100644 --- a/sdk/auth/rali_signature_composer.go +++ b/sdk/auth/rali_signature_composer.go @@ -38,6 +38,10 @@ func completeRaliSignParams(request requests.AcsRequest, signer Signer) (err err request.GetHeaders()["X-Ca-Nonce"] = utils.GetUUID() request.GetHeaders()["X-Ca-Key"], err = signer.GetAccessKeyId() + if request.GetEnv() != "" { + request.GetHeaders()["X-Ca-Stage"] = request.GetEnv() + } + if request.GetMethod() == requests.POST { request.GetHeaders()["Content-type"] = requests.Form } diff --git a/sdk/client.go b/sdk/client.go index de6413d..0e2add5 100644 --- a/sdk/client.go +++ b/sdk/client.go @@ -93,9 +93,14 @@ func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, accessKeyF return client.InitWithOptions(config, credential) } -func (client *Client) InitWithAliAppcode(accessKeyId, accessKeySecret string) (err error) { +func (client *Client) InitWithAliAppcode(accessKeyId, accessKeySecret string, env ...string) (err error) { config := client.InitWithConfig() credential := credentials.NewAliAppcodeCredential(accessKeyId, accessKeySecret) + + if len(env) > 0 { + config.Env = env[0] + } + return client.InitWithOptions(config, credential) } @@ -261,8 +266,8 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r 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 + if strings.Index(domain.Default, ".") < 0 { + domain.Default += defaultDomain request.SetDomain(domain) } @@ -270,6 +275,10 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer request.SetScheme(client.config.Scheme) } + if request.GetEnv() == "" && client.config.Env != "" { + request.SetEnv(client.config.Env) + } + err = requests.InitParam(request) if err != nil { return @@ -302,6 +311,7 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer if host, isContainsHost := request.GetHeaders()["host"]; isContainsHost { httpRequest.Host = host } + userAgent := DefaultUserAgent httpRequest.Header.Set("User-Agent", userAgent) diff --git a/sdk/config.go b/sdk/config.go index db136bd..ae79f51 100644 --- a/sdk/config.go +++ b/sdk/config.go @@ -14,6 +14,7 @@ type Config struct { UserAgent string `default:""` Scheme string `default:"HTTP"` Timeout time.Duration `default:"5"` + Env string `default:""` } func NewConfig() *Config { diff --git a/sdk/requests/common_request.go b/sdk/requests/common_request.go index 7d5f653..74fea83 100644 --- a/sdk/requests/common_request.go +++ b/sdk/requests/common_request.go @@ -31,6 +31,6 @@ func (request *CommonRequest) GetStyle() string { return request.Ontology.GetStyle() } -func (request *CommonRequest) InitWithApiInfo(domain, version, urlPath string) { +func (request *CommonRequest) InitWithApiInfo(domain Host, version, urlPath string) { request.Ontology.InitWithApiInfo(domain, version, urlPath) } diff --git a/sdk/requests/request.go b/sdk/requests/request.go index 8ae7217..c0e6200 100644 --- a/sdk/requests/request.go +++ b/sdk/requests/request.go @@ -41,9 +41,18 @@ const ( Body = "Body" Path = "Path" + TEST = "TEST" + PRE = "PRE" + RELEASE = "RELEASE" + HeaderSeparator = "\n" ) +type Host struct { + Default string + Func func(string) string +} + var debug utils.Debug func init() { @@ -61,20 +70,23 @@ type AcsRequest interface { GetFormParams() map[string]string GetMethod() string GetScheme() string - GetDomain() string + GetDomain() Host + SetDomain(host Host) GetActionName() string GetAcceptFormat() string GetAccept() string GetHeaders() map[string]string GetStyle() string - InitWithApiInfo(domain, version, urlPath string) + InitWithApiInfo(domain Host, version, urlPath string) + GetEnv() string + SetEnv(string) BuildUrl() string BuildQueries() string SetScheme(scheme string) SetContent(content []byte) - SetDomain(host string) + SetStringToSign(stringToSign string) GetStringToSign() string GetBodyReader() io.Reader @@ -88,11 +100,12 @@ type baseRequest struct { Scheme string Method string Port string - Domain string + Domain Host From string ReadTimeout time.Duration ConnectTimeout time.Duration isInsecure *bool + Env string AcceptFormat string actionName string @@ -110,6 +123,14 @@ type baseRequest struct { stringToSign string } +func (request *baseRequest) GetEnv() string { + return request.Env +} + +func (request *baseRequest) SetEnv(e string) { + request.Env = e +} + func (request *baseRequest) GetStringToSign() string { return request.stringToSign } @@ -144,7 +165,7 @@ func (request *baseRequest) SetScheme(scheme string) { request.Scheme = scheme } -func (request *baseRequest) SetDomain(host string) { +func (request *baseRequest) SetDomain(host Host) { request.Domain = host } @@ -152,7 +173,7 @@ func (request *baseRequest) GetScheme() string { return request.Scheme } -func (request *baseRequest) GetDomain() string { +func (request *baseRequest) GetDomain() Host { return request.Domain } diff --git a/sdk/requests/rpc_request.go b/sdk/requests/rpc_request.go index 712b948..55e763c 100644 --- a/sdk/requests/rpc_request.go +++ b/sdk/requests/rpc_request.go @@ -17,7 +17,15 @@ func (request *RpcRequest) init() { } func (request *RpcRequest) BuildUrl() string { - url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), request.Domain) + + var hostname string + if request.Domain.Func == nil { + hostname = request.Domain.Default + } else if hostname = request.Domain.Func(request.GetEnv()); hostname == "" { + hostname = request.Domain.Default + } + + url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), hostname) if len(request.Port) > 0 { url = fmt.Sprintf("%s:%s", url, request.Port) } @@ -38,7 +46,7 @@ func (request *RpcRequest) GetActionName() string { return request.actionName } -func (request *RpcRequest) InitWithApiInfo(domain, version, urlPath string) { +func (request *RpcRequest) InitWithApiInfo(domain Host, version, urlPath string) { request.init() request.SetDomain(domain) request.version = version diff --git a/services/jedi/client.go b/services/jedi/client.go index 84a1a0e..ca91eb7 100644 --- a/services/jedi/client.go +++ b/services/jedi/client.go @@ -2,14 +2,26 @@ package jedi import ( "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" "strings" ) const ( - HOST = "jedi.oapi.gaore.com" - VERSION = "2020-08-04" + VERSION = "2020-09-24" ) +var HOST requests.Host = requests.Host{ + Default: "jedi.api.gaore.com", + Func: func(s string) string { + var a = map[string]string{ + requests.RELEASE: "jedi.api.gaore.com", + requests.PRE: "jedi.api.gaore.com", + requests.TEST: "jedi.oapi.gaore.com", + } + return a[s] + }, +} + type Client struct { sdk.Client } @@ -35,8 +47,8 @@ func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client, return } -func NewClientWithAliAppcode(accesskey, secrect string) (client *Client, err error) { +func NewClientWithAliAppcode(accesskey, secrect string, env ...string) (client *Client, err error) { client = &Client{} - err = client.InitWithAliAppcode(accesskey, secrect) + err = client.InitWithAliAppcode(accesskey, secrect, env...) return } diff --git a/services/sso/client.go b/services/sso/client.go index ba01773..3e726fe 100644 --- a/services/sso/client.go +++ b/services/sso/client.go @@ -1,12 +1,18 @@ package sso -import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" +import ( + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" +) const ( - HOST = "sso" VERSION = "2020-08-07" ) +var HOST = requests.Host{ + Default: "sso", +} + type Client struct { sdk.Client }