7
0

3. 增加不同环境请求 对不同的header 和 请求域名

This commit is contained in:
liangzy 2020-09-23 20:23:35 +08:00
parent 69c6020214
commit 1f82a534c3
8 changed files with 80 additions and 18 deletions

View File

@ -38,6 +38,10 @@ func completeRaliSignParams(request requests.AcsRequest, signer Signer) (err err
request.GetHeaders()["X-Ca-Nonce"] = utils.GetUUID() request.GetHeaders()["X-Ca-Nonce"] = utils.GetUUID()
request.GetHeaders()["X-Ca-Key"], err = signer.GetAccessKeyId() request.GetHeaders()["X-Ca-Key"], err = signer.GetAccessKeyId()
if request.GetEnv() != "" {
request.GetHeaders()["X-Ca-Stage"] = request.GetEnv()
}
if request.GetMethod() == requests.POST { if request.GetMethod() == requests.POST {
request.GetHeaders()["Content-type"] = requests.Form request.GetHeaders()["Content-type"] = requests.Form
} }

View File

@ -93,9 +93,14 @@ func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, accessKeyF
return client.InitWithOptions(config, credential) 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() config := client.InitWithConfig()
credential := credentials.NewAliAppcodeCredential(accessKeyId, accessKeySecret) credential := credentials.NewAliAppcodeCredential(accessKeyId, accessKeySecret)
if len(env) > 0 {
config.Env = env[0]
}
return client.InitWithOptions(config, credential) 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) { func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *http.Request, err error) {
// init param // init param
domain := request.GetDomain() domain := request.GetDomain()
if strings.Index(domain, ".") < 0 { if strings.Index(domain.Default, ".") < 0 {
domain += defaultDomain domain.Default += defaultDomain
request.SetDomain(domain) request.SetDomain(domain)
} }
@ -270,6 +275,10 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
request.SetScheme(client.config.Scheme) request.SetScheme(client.config.Scheme)
} }
if request.GetEnv() == "" && client.config.Env != "" {
request.SetEnv(client.config.Env)
}
err = requests.InitParam(request) err = requests.InitParam(request)
if err != nil { if err != nil {
return return
@ -302,6 +311,7 @@ func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer
if host, isContainsHost := request.GetHeaders()["host"]; isContainsHost { if host, isContainsHost := request.GetHeaders()["host"]; isContainsHost {
httpRequest.Host = host httpRequest.Host = host
} }
userAgent := DefaultUserAgent userAgent := DefaultUserAgent
httpRequest.Header.Set("User-Agent", userAgent) httpRequest.Header.Set("User-Agent", userAgent)

View File

@ -14,6 +14,7 @@ type Config struct {
UserAgent string `default:""` UserAgent string `default:""`
Scheme string `default:"HTTP"` Scheme string `default:"HTTP"`
Timeout time.Duration `default:"5"` Timeout time.Duration `default:"5"`
Env string `default:""`
} }
func NewConfig() *Config { func NewConfig() *Config {

View File

@ -31,6 +31,6 @@ func (request *CommonRequest) GetStyle() string {
return request.Ontology.GetStyle() 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) request.Ontology.InitWithApiInfo(domain, version, urlPath)
} }

View File

@ -41,9 +41,18 @@ const (
Body = "Body" Body = "Body"
Path = "Path" Path = "Path"
TEST = "TEST"
PRE = "PRE"
RELEASE = "RELEASE"
HeaderSeparator = "\n" HeaderSeparator = "\n"
) )
type Host struct {
Default string
Func func(string) string
}
var debug utils.Debug var debug utils.Debug
func init() { func init() {
@ -61,20 +70,23 @@ type AcsRequest interface {
GetFormParams() map[string]string GetFormParams() map[string]string
GetMethod() string GetMethod() string
GetScheme() string GetScheme() string
GetDomain() string GetDomain() Host
SetDomain(host Host)
GetActionName() string GetActionName() string
GetAcceptFormat() string GetAcceptFormat() string
GetAccept() string GetAccept() string
GetHeaders() map[string]string GetHeaders() map[string]string
GetStyle() string GetStyle() string
InitWithApiInfo(domain, version, urlPath string) InitWithApiInfo(domain Host, version, urlPath string)
GetEnv() string
SetEnv(string)
BuildUrl() string BuildUrl() string
BuildQueries() string BuildQueries() string
SetScheme(scheme string) SetScheme(scheme string)
SetContent(content []byte) SetContent(content []byte)
SetDomain(host string)
SetStringToSign(stringToSign string) SetStringToSign(stringToSign string)
GetStringToSign() string GetStringToSign() string
GetBodyReader() io.Reader GetBodyReader() io.Reader
@ -88,11 +100,12 @@ type baseRequest struct {
Scheme string Scheme string
Method string Method string
Port string Port string
Domain string Domain Host
From string From string
ReadTimeout time.Duration ReadTimeout time.Duration
ConnectTimeout time.Duration ConnectTimeout time.Duration
isInsecure *bool isInsecure *bool
Env string
AcceptFormat string AcceptFormat string
actionName string actionName string
@ -110,6 +123,14 @@ type baseRequest struct {
stringToSign string stringToSign string
} }
func (request *baseRequest) GetEnv() string {
return request.Env
}
func (request *baseRequest) SetEnv(e string) {
request.Env = e
}
func (request *baseRequest) GetStringToSign() string { func (request *baseRequest) GetStringToSign() string {
return request.stringToSign return request.stringToSign
} }
@ -144,7 +165,7 @@ func (request *baseRequest) SetScheme(scheme string) {
request.Scheme = scheme request.Scheme = scheme
} }
func (request *baseRequest) SetDomain(host string) { func (request *baseRequest) SetDomain(host Host) {
request.Domain = host request.Domain = host
} }
@ -152,7 +173,7 @@ func (request *baseRequest) GetScheme() string {
return request.Scheme return request.Scheme
} }
func (request *baseRequest) GetDomain() string { func (request *baseRequest) GetDomain() Host {
return request.Domain return request.Domain
} }

View File

@ -17,7 +17,15 @@ func (request *RpcRequest) init() {
} }
func (request *RpcRequest) BuildUrl() string { 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 { if len(request.Port) > 0 {
url = fmt.Sprintf("%s:%s", url, request.Port) url = fmt.Sprintf("%s:%s", url, request.Port)
} }
@ -38,7 +46,7 @@ func (request *RpcRequest) GetActionName() string {
return request.actionName return request.actionName
} }
func (request *RpcRequest) InitWithApiInfo(domain, version, urlPath string) { func (request *RpcRequest) InitWithApiInfo(domain Host, version, urlPath string) {
request.init() request.init()
request.SetDomain(domain) request.SetDomain(domain)
request.version = version request.version = version

View File

@ -2,14 +2,26 @@ package jedi
import ( import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"strings" "strings"
) )
const ( const (
HOST = "jedi.oapi.gaore.com" VERSION = "2020-09-24"
VERSION = "2020-08-04"
) )
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 { type Client struct {
sdk.Client sdk.Client
} }
@ -35,8 +47,8 @@ func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client,
return return
} }
func NewClientWithAliAppcode(accesskey, secrect string) (client *Client, err error) { func NewClientWithAliAppcode(accesskey, secrect string, env ...string) (client *Client, err error) {
client = &Client{} client = &Client{}
err = client.InitWithAliAppcode(accesskey, secrect) err = client.InitWithAliAppcode(accesskey, secrect, env...)
return return
} }

View File

@ -1,12 +1,18 @@
package sso 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 ( const (
HOST = "sso"
VERSION = "2020-08-07" VERSION = "2020-08-07"
) )
var HOST = requests.Host{
Default: "sso",
}
type Client struct { type Client struct {
sdk.Client sdk.Client
} }