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-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
}

View File

@ -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)

View File

@ -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 {

View File

@ -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)
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}