-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddl.go
52 lines (43 loc) · 1.21 KB
/
middl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package middl
import (
"net/http"
"github.com/nmrshll/go-httpclient-middl/middleware"
"github.com/palantir/stacktrace"
)
// Client is a wrapper around http.Client that you can add middleware to
type Client struct {
*http.Client
}
// NewClient creates a new HttpClient
func NewClient(clients ...*http.Client) (*Client, error) {
// validate parameters
{
if len(clients) > 1 {
return nil, stacktrace.NewError("can't instantiate Client with more than one http.Client")
}
}
// if a client is given then use this one
if len(clients) == 1 {
client := clients[0]
if client.Transport == nil {
client.Transport = http.DefaultTransport
}
return &Client{client}, nil
}
// if no client is given create a client using http.DefaultClient
client := &Client{Client: &http.Client{Transport: http.DefaultTransport}}
return client, nil
}
type MiddlewareFunc func(parent http.RoundTripper) http.RoundTripper
func (c *Client) UseMiddleware(middleware ...middleware.MiddlewareFunc) {
// validate / modify parameters
{
// client should have transport
if c.Transport == nil {
c.Transport = http.DefaultTransport
}
}
for _, middlewareFunc := range middleware {
c.Transport = middlewareFunc(c.Transport)
}
}