Skip to content

Commit

Permalink
support global parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Jun 30, 2022
1 parent c0156b6 commit 8101375
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ import (
credential "github.com/aliyun/credentials-go/credentials"
)

type GlobalParameters struct {
Headers map[string]*string `json:"headers,omitempty" xml:"headers,omitempty"`
Queries map[string]*string `json:"queries,omitempty" xml:"queries,omitempty"`
}

func (s GlobalParameters) String() string {
return tea.Prettify(s)
}

func (s GlobalParameters) GoString() string {
return s.String()
}

func (s *GlobalParameters) SetHeaders(v map[string]*string) *GlobalParameters {
s.Headers = v
return s
}

func (s *GlobalParameters) SetQueries(v map[string]*string) *GlobalParameters {
s.Queries = v
return s
}

/**
* Model for initing client
*/
Expand Down Expand Up @@ -68,6 +91,8 @@ type Config struct {
SignatureVersion *string `json:"signatureVersion,omitempty" xml:"signatureVersion,omitempty"`
// Signature Algorithm
SignatureAlgorithm *string `json:"signatureAlgorithm,omitempty" xml:"signatureAlgorithm,omitempty"`
// Global Parameters
GlobalParameters *GlobalParameters `json:"globalParameters,omitempty" xml:"globalParameters,omitempty"`
}

func (s Config) String() string {
Expand Down Expand Up @@ -198,6 +223,11 @@ func (s *Config) SetSignatureAlgorithm(v string) *Config {
return s
}

func (s *Config) SetGlobalParameters(v *GlobalParameters) *Config {
s.GlobalParameters = v
return s
}

type OpenApiRequest struct {
Headers map[string]*string `json:"headers,omitempty" xml:"headers,omitempty"`
Query map[string]*string `json:"query,omitempty" xml:"query,omitempty"`
Expand Down Expand Up @@ -336,6 +366,7 @@ type Client struct {
SignatureAlgorithm *string
Headers map[string]*string
Spi spi.ClientInterface
GlobalParameters *GlobalParameters
}

/**
Expand Down Expand Up @@ -397,6 +428,7 @@ func (client *Client) Init(config *Config) (_err error) {
client.MaxIdleConns = config.MaxIdleConns
client.SignatureVersion = config.SignatureVersion
client.SignatureAlgorithm = config.SignatureAlgorithm
client.GlobalParameters = config.GlobalParameters
return nil
}

Expand Down Expand Up @@ -1085,7 +1117,22 @@ func (client *Client) DoRequest(params *Params, request *OpenApiRequest, runtime
request_.Protocol = util.DefaultString(client.Protocol, params.Protocol)
request_.Method = params.Method
request_.Pathname = params.Pathname
request_.Query = request.Query
globalQueries := make(map[string]*string)
globalHeaders := make(map[string]*string)
if !tea.BoolValue(util.IsUnset(tea.ToMap(client.GlobalParameters))) {
globalParams := client.GlobalParameters
if !tea.BoolValue(util.IsUnset(globalParams.Queries)) {
globalQueries = globalParams.Queries
}

if !tea.BoolValue(util.IsUnset(globalParams.Headers)) {
globalHeaders = globalParams.Headers
}

}

request_.Query = tea.Merge(globalQueries,
request.Query)
// endpoint is setted in product client
request_.Headers = tea.Merge(map[string]*string{
"host": client.Endpoint,
Expand All @@ -1095,7 +1142,8 @@ func (client *Client) DoRequest(params *Params, request *OpenApiRequest, runtime
"x-acs-date": openapiutil.GetTimestamp(),
"x-acs-signature-nonce": util.GetNonce(),
"accept": tea.String("application/json"),
}, request.Headers)
}, globalHeaders,
request.Headers)
if tea.BoolValue(util.EqualString(params.Style, tea.String("RPC"))) {
headers, _err := client.GetRpcHeaders()
if _err != nil {
Expand Down Expand Up @@ -1353,10 +1401,26 @@ func (client *Client) Execute(params *Params, request *OpenApiRequest, runtime *
return _result, _err
}

globalQueries := make(map[string]*string)
globalHeaders := make(map[string]*string)
if !tea.BoolValue(util.IsUnset(tea.ToMap(client.GlobalParameters))) {
globalParams := client.GlobalParameters
if !tea.BoolValue(util.IsUnset(globalParams.Queries)) {
globalQueries = globalParams.Queries
}

if !tea.BoolValue(util.IsUnset(globalParams.Headers)) {
globalHeaders = globalParams.Headers
}

}

requestContext := &spi.InterceptorContextRequest{
Headers: tea.Merge(request.Headers,
Headers: tea.Merge(globalHeaders,
request.Headers,
headers),
Query: request.Query,
Query: tea.Merge(globalQueries,
request.Query),
Body: request.Body,
Stream: request.Stream,
HostMap: request.HostMap,
Expand Down

0 comments on commit 8101375

Please sign in to comment.