Add ability to define custom interceptors (fixes #35)
This commit is contained in:
parent
8244b5a5f5
commit
4145fa842c
14
client.go
14
client.go
@ -15,9 +15,10 @@ import (
|
||||
|
||||
// Client defines our structure
|
||||
type Client struct {
|
||||
root string
|
||||
headers http.Header
|
||||
c *http.Client
|
||||
root string
|
||||
headers http.Header
|
||||
interceptor func(method string, rq *http.Request)
|
||||
c *http.Client
|
||||
|
||||
authMutex sync.Mutex
|
||||
auth Authenticator
|
||||
@ -58,7 +59,7 @@ func (n *NoAuth) Authorize(req *http.Request, method string, path string) {
|
||||
|
||||
// NewClient creates a new instance of client
|
||||
func NewClient(uri, user, pw string) *Client {
|
||||
return &Client{FixSlash(uri), make(http.Header), &http.Client{}, sync.Mutex{}, &NoAuth{user, pw}}
|
||||
return &Client{FixSlash(uri), make(http.Header), nil, &http.Client{}, sync.Mutex{}, &NoAuth{user, pw}}
|
||||
}
|
||||
|
||||
// SetHeader lets us set arbitrary headers for a given client
|
||||
@ -66,6 +67,11 @@ func (c *Client) SetHeader(key, value string) {
|
||||
c.headers.Add(key, value)
|
||||
}
|
||||
|
||||
// SetInterceptor lets us set an arbitrary interceptor for a given client
|
||||
func (c *Client) SetInterceptor(interceptor func(method string, rq *http.Request)) {
|
||||
c.interceptor = interceptor
|
||||
}
|
||||
|
||||
// SetTimeout exposes the ability to set a time limit for requests
|
||||
func (c *Client) SetTimeout(timeout time.Duration) {
|
||||
c.c.Timeout = timeout
|
||||
|
@ -43,6 +43,10 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
|
||||
intercept(r)
|
||||
}
|
||||
|
||||
if c.interceptor != nil {
|
||||
c.interceptor(method, r)
|
||||
}
|
||||
|
||||
rs, err := c.c.Do(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Loading…
Reference in New Issue
Block a user