Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple PreRequestHooks #665

Open
rostislaved opened this issue May 18, 2023 · 4 comments
Open

Multiple PreRequestHooks #665

rostislaved opened this issue May 18, 2023 · 4 comments

Comments

@rostislaved
Copy link

I face the same problem as in #517 (comment) - I couldn't get not nil raw request.

The only way to get raw request is to use PreRequestHook. But we can have only one hook which will we overwritten if we try to attach the second one like this:

client := resty.New().
		SetPreRequestHook(preRequestHook1).
		SetPreRequestHook(preRequestHook2)

But having several hooks can be handy. I personally use it to:

  1. Add custom auth which requires making hash of the request
  2. Printing the final request as a curl (for debug purposes)
  3. Can be used to remove headers which are set automatically like user-agent or content-type

That is why I wrote function which acts like this:

	client := resty.New().
		SetPreRequestHook(
			And(
				preRequestHook1,
				preRequestHook2,
			),
		)

So I want so propose to make it possible to have multiple PreRequestHook objects. That will allow to use resty this way:

client := resty.New().
   	SetPreRequestHook(preRequestHook1).
   	SetPreRequestHook(preRequestHook2)
@jeevatkm
Copy link
Member

@rostislaved It was intentionally kept as one prehook. However, I'm planning to make much improvement in v3.

@chb0github
Copy link

The vernacular should change from SetPrerequestHook to AddPreRequestHook. If you intend to stick with this approach, then offer a CompositePreRequestHook that take a slice of hooks and returns a singular using a closure

func Compose(hooks ...PreRequestHook) PreRequestHook {
       var errors []error
	return func(c *Client, r *http.Request) error {

		for _, hook := range hooks {
			errors := append(errors,hook(c,r))
		}
		return fmt.ErrorF("%v",errors)
	}
}

@jeevatkm
Copy link
Member

@chb0github There is an upcoming redesign in the middleware chain on request and response on v3.0.0. Current v2 series, I'm planning to keep it as-is.

@chb0github
Copy link

Looking forward to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants