Codebase list golang-github-go-kit-kit / 05e8864
transport/http: added after ClientOption to Client Geoff Berger authored 7 years ago Geoff Berger committed 7 years ago
1 changed file(s) with 20 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1717 enc EncodeRequestFunc
1818 dec DecodeResponseFunc
1919 before []RequestFunc
20 after []ClientResponseFunc
2021 bufferedStream bool
2122 }
2223
3536 enc: enc,
3637 dec: dec,
3738 before: []RequestFunc{},
39 after: []ClientResponseFunc{},
3840 bufferedStream: false,
3941 }
4042 for _, option := range options {
4648 // ClientOption sets an optional parameter for clients.
4749 type ClientOption func(*Client)
4850
51 // ClientResponseFunc takes information from an HTTP request to read from the
52 // response and do something additional with the response or add/update
53 // context. ClientResponseFuncs are only executed on the client immediately
54 // after the request has been made but prior to decoding. This allows for
55 // intervention in case there is an error from the request.
56 type ClientResponseFunc func(context.Context, *http.Response)
57
4958 // SetClient sets the underlying HTTP client used for requests.
5059 // By default, http.DefaultClient is used.
5160 func SetClient(client *http.Client) ClientOption {
5665 // request before it's invoked.
5766 func SetClientBefore(before ...RequestFunc) ClientOption {
5867 return func(c *Client) { c.before = before }
68 }
69
70 // SetClientAfter sets the ClientResponseFunc applied to the incoming HTTP
71 // request prior to it being decoded. This is useful for obtaining anything off
72 // of the response and adding onto the context prior to decoding.
73 func SetClientAfter(after ...ClientResponseFunc) ClientOption {
74 return func(c *Client) { c.after = after }
5975 }
6076
6177 // SetBufferedStream sets whether the Response.Body is left open, allowing it
84100 }
85101
86102 resp, err := ctxhttp.Do(ctx, c.client, req)
103 for _, f := range c.after {
104 f(ctx, resp)
105 }
106
87107 if err != nil {
88108 return nil, Error{Domain: DomainDo, Err: err}
89109 }