converting to post-processor prototype

This commit is contained in:
mzack 2021-11-13 03:17:05 +01:00
parent e517797cfa
commit eb61c519c3
4 changed files with 36 additions and 27 deletions

View File

@ -139,7 +139,7 @@ type Request struct {
// description: | // description: |
// SelfContained specifies if the request is self contained. // SelfContained specifies if the request is self contained.
SelfContained bool `yaml:"-" json:"-"` SelfContained bool `yaml:"-" json:"-"`
AwsSign bool `yaml:"-" json:"-"` PostProcessors []string `yaml:"-" json:"-"`
// description: | // description: |
// CookieReuse is an optional setting that enables cookie reuse for // CookieReuse is an optional setting that enables cookie reuse for

View File

@ -197,6 +197,10 @@ func (request *Request) executeTurboHTTP(reqURL string, dynamicValues, previous
return requestErr return requestErr
} }
func (request *Request) hasPreprocessors() bool {
return len(request.PostProcessors) > 0
}
// ExecuteWithResults executes the final request on a URL // ExecuteWithResults executes the final request on a URL
func (request *Request) ExecuteWithResults(reqURL string, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error { func (request *Request) ExecuteWithResults(reqURL string, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
// verify if pipeline was requested // verify if pipeline was requested
@ -296,6 +300,7 @@ func (request *Request) executeRequest(reqURL string, generatedRequest *generate
// For race conditions we can't dump the request body at this point as it's already waiting the open-gate event, already handled with a similar code within the race function // For race conditions we can't dump the request body at this point as it's already waiting the open-gate event, already handled with a similar code within the race function
if !generatedRequest.original.Race { if !generatedRequest.original.Race {
var dumpError error var dumpError error
// TODO: dump is currently not working with post-processors - somehow it alters the signature
dumpedRequest, dumpError = dump(generatedRequest, reqURL) dumpedRequest, dumpError = dump(generatedRequest, reqURL)
if dumpError != nil { if dumpError != nil {
return dumpError return dumpError
@ -349,23 +354,26 @@ func (request *Request) executeRequest(reqURL string, generatedRequest *generate
} }
} }
if resp == nil { if resp == nil {
// aws sign the request if necessary for _, postProcessor := range request.PostProcessors {
if request.AwsSign { switch postProcessor {
case "aws-sign":
var awsSigner *AwsSigner var awsSigner *AwsSigner
payloads := request.options.Options.Vars.AsMap() payloads := request.options.Options.Vars.AsMap()
if request.options.Options.EnvironmentVariables {
var err error
awsSigner, err = NewAwsSignerFromEnv()
if err != nil {
return err
}
} else { // get from variables {
awsAccessKeyId := types.ToString(payloads["aws-id"]) awsAccessKeyId := types.ToString(payloads["aws-id"])
awsSecretAccessKey := types.ToString(payloads["aws-secret"]) awsSecretAccessKey := types.ToString(payloads["aws-secret"])
if awsAccessKeyId != "" && awsSecretAccessKey != "" {
awsSigner, err = NewAwsSigner(awsAccessKeyId, awsSecretAccessKey) awsSigner, err = NewAwsSigner(awsAccessKeyId, awsSecretAccessKey)
} else {
awsSigner, err = NewAwsSignerFromEnv()
}
if err != nil { if err != nil {
return err return err
} }
service := types.ToString(payloads["service"])
region := types.ToString(payloads["region"])
if service == "" || region == "" {
return errors.New("service and region are mandatory")
} }
args := SignArguments{ args := SignArguments{
@ -378,6 +386,7 @@ func (request *Request) executeRequest(reqURL string, generatedRequest *generate
return err return err
} }
} }
}
resp, err = request.httpClient.Do(generatedRequest.request) resp, err = request.httpClient.Do(generatedRequest.request)
} }
} }

View File

@ -107,9 +107,9 @@ func Parse(filePath string, preprocessor Preprocessor, options protocols.Execute
// parseSelfContainedRequests parses the self contained template requests. // parseSelfContainedRequests parses the self contained template requests.
func (template *Template) parseSelfContainedRequests() { func (template *Template) parseSelfContainedRequests() {
if template.AwsSign { if len(template.PostProcessors) > 0 {
for _, request := range template.RequestsHTTP { for _, request := range template.RequestsHTTP {
request.AwsSign = true request.PostProcessors = template.PostProcessors
} }
} }
if !template.SelfContained { if !template.SelfContained {

View File

@ -74,7 +74,7 @@ type Template struct {
// description: | // description: |
// Self Contained marks Requests for the template as self-contained // Self Contained marks Requests for the template as self-contained
SelfContained bool `yaml:"self-contained,omitempty" jsonschema:"title=mark requests as self-contained,description=Mark Requests for the template as self-contained"` SelfContained bool `yaml:"self-contained,omitempty" jsonschema:"title=mark requests as self-contained,description=Mark Requests for the template as self-contained"`
AwsSign bool `yaml:"aws-sign,omitempty"` PostProcessors []string `yaml:"post-processors,omitempty"`
// TotalRequests is the total number of requests for the template. // TotalRequests is the total number of requests for the template.
TotalRequests int `yaml:"-" json:"-"` TotalRequests int `yaml:"-" json:"-"`