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

@ -138,8 +138,8 @@ 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,33 +354,37 @@ 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 {
var awsSigner *AwsSigner case "aws-sign":
payloads := request.options.Options.Vars.AsMap() var awsSigner *AwsSigner
if request.options.Options.EnvironmentVariables { payloads := request.options.Options.Vars.AsMap()
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"])
awsSigner, err = NewAwsSigner(awsAccessKeyId, awsSecretAccessKey) if awsAccessKeyId != "" && awsSecretAccessKey != "" {
awsSigner, err = NewAwsSigner(awsAccessKeyId, awsSecretAccessKey)
} else {
awsSigner, err = NewAwsSignerFromEnv()
}
if err != nil { if err != nil {
return err return err
} }
}
args := SignArguments{ service := types.ToString(payloads["service"])
Service: types.ToString(payloads["service"]), region := types.ToString(payloads["region"])
Region: types.ToString(payloads["region"]), if service == "" || region == "" {
Time: time.Now(), return errors.New("service and region are mandatory")
} }
err = awsSigner.SignHTTP(generatedRequest.request.Request, args)
if err != nil { args := SignArguments{
return err Service: types.ToString(payloads["service"]),
Region: types.ToString(payloads["region"]),
Time: time.Now(),
}
err = awsSigner.SignHTTP(generatedRequest.request.Request, args)
if err != nil {
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

@ -73,8 +73,8 @@ 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:"-"`