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
Labels
Milestone

Comments

@sethvargo
Copy link
Contributor

@sethvargo 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
@gopherbot gopherbot added the Proposal label Jul 19, 2020
@martisch
Copy link
Contributor

@martisch martisch commented Jul 19, 2020

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

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 7, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.