mirror of
https://github.com/moby/moby.git
synced 2026-08-09 09:33:50 +00:00
ResponseHooks are executed as part of the http.RoundTripper, which is
documented as;
// RoundTrip should not attempt to interpret the response. In
// particular, RoundTrip must return err == nil if it obtained
// a response, regardless of the response's HTTP status code.
// A non-nil err should be reserved for failure to obtain a
// response. Similarly, RoundTrip should not attempt to
// handle higher-level protocol details such as redirects,
// authentication, or cookies.
So errors should be reserved for failing to make a request; if a
webhook would return an error for other reasons, that would break
this contract (and prevent running other response-hooks), so we should
consider response-hooks to never return an error, and users of this
functionality to handle errors in other ways.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
24 lines
352 B
Go
24 lines
352 B
Go
package client
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
type responseHookTransport struct {
|
|
base http.RoundTripper
|
|
hooks []ResponseHook
|
|
}
|
|
|
|
func (t *responseHookTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
resp, err := t.base.RoundTrip(req)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
for _, h := range t.hooks {
|
|
h(resp)
|
|
}
|
|
|
|
return resp, nil
|
|
}
|