-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.go
43 lines (38 loc) · 965 Bytes
/
transport.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
package chttp
import (
"net/http"
)
func (c *Client) transport() http.RoundTripper {
base := c.base
if base == nil {
base = http.DefaultTransport
}
return &transport{
Client: c,
Default: base,
}
}
type transport struct {
Client *Client
Default http.RoundTripper
}
func (t *transport) RoundTrip(request *http.Request) (*http.Response, error) {
middlewares := t.Client.getMiddlewares()
var next func(request *http.Request) (*http.Response, error)
next = func(request *http.Request) (*http.Response, error) {
var middleware Middleware
if len(middlewares) == 0 {
middleware = t.getDefault()
} else {
middleware = middlewares[0]
middlewares = middlewares[1:]
}
return middleware(request, next)
}
return next(request)
}
func (t *transport) getDefault() Middleware {
return func(request *http.Request, _ func(request *http.Request) (*http.Response, error)) (*http.Response, error) {
return t.Default.RoundTrip(request)
}
}