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

Proposal: http.RoundTripperFunc #40292

Closed
sethvargo opened this issue Jul 19, 2020 · 2 comments
Closed

Proposal: http.RoundTripperFunc #40292

sethvargo opened this issue Jul 19, 2020 · 2 comments

Comments

@sethvargo
Copy link
Contributor

sethvargo commented Jul 19, 2020

The http package currently exposes a HandlerFunc interface that satisfies the http.Handler interface.

I'm proposing a similar addition, satisfying the http.RoundTripper interface. This would enable composable RoundTrippers and avoid the need to create a struct in some instances.

Proposed design

type RoundTripperFunc func(*http.Request) (*http.Response, error)

func (f RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
  return f(r)
}

Example usage

func addAuthRoundTripper(apiKey string) http.RoundTripper {
  return http.RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
    r.SetBasicAuth("", apiKey) // Not super secure, just using as an example
    return http.DefaultTransport.RoundTrip(r)
  })
}

Without

This shows the required effort without this addition.

type authRoundTripper struct {
  apiKey string
}

func (a *authRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
  r.SetBasicAuth("", apiKey)
  return http.DefaultTransport.RoundTrip(r)
}
@gopherbot gopherbot added this to the Proposal milestone Jul 19, 2020
@martisch
Copy link
Contributor

There already is a proposal for a net/http RoundTripperFunc. Duplicate of #38479?

@ianlancetaylor
Copy link
Contributor

It does look the same. Closing as a dup. Please comment if you disagree. Or maybe comment on #38479 to suggest changes there.

@golang golang locked and limited conversation to collaborators Aug 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants