-
Notifications
You must be signed in to change notification settings - Fork 0
/
wukong.go
145 lines (111 loc) · 2.63 KB
/
wukong.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package wukong
import (
"net/http"
"time"
)
type (
WuKong struct {
baseUrl string
client *http.Client
basicAuth BasicAuth
before []Before
after []After
}
BasicAuth struct {
Username string
Password string
}
Before func(*Request) error
After func(*Request, *Response) error
)
func New(baseUrl string) *WuKong {
w := &WuKong{
baseUrl: baseUrl,
client: &http.Client{
Transport: DefaultTransport,
},
}
return w
}
func (wk *WuKong) UseBefore(fns ...Before) *WuKong {
wk.before = append(wk.before, fns...)
return wk
}
func (wk *WuKong) UseAfter(fns ...After) *WuKong {
wk.after = append(wk.after, fns...)
return wk
}
func (wk *WuKong) SetTransport(transport *http.Transport) *WuKong {
wk.client.Transport = transport
return wk
}
func (wk *WuKong) SetBasicAuth(auth BasicAuth) *WuKong {
wk.basicAuth = auth
return wk
}
func (wk *WuKong) Get(path string) *Request {
return wk.initRequest(NewRequest(wk, http.MethodGet, path))
}
func (wk *WuKong) Post(path string) *Request {
return wk.initRequest(NewRequest(wk, http.MethodPost, path))
}
func (wk *WuKong) Put(path string) *Request {
return wk.initRequest(NewRequest(wk, http.MethodPut, path))
}
func (wk *WuKong) Patch(path string) *Request {
return wk.initRequest(NewRequest(wk, http.MethodPatch, path))
}
func (wk *WuKong) Delete(path string) *Request {
return wk.initRequest(NewRequest(wk, http.MethodDelete, path))
}
func (wk *WuKong) Head(path string) *Request {
return wk.initRequest(NewRequest(wk, http.MethodHead, path))
}
func (wk *WuKong) Options(path string) *Request {
return wk.initRequest(NewRequest(wk, http.MethodOptions, path))
}
func (wk *WuKong) Trace(path string) *Request {
return wk.initRequest(NewRequest(wk, http.MethodTrace, path))
}
func (wk *WuKong) Timeout(t time.Duration) *WuKong {
wk.client.Timeout = t
return wk
}
func (wk *WuKong) initRequest(request *Request) *Request {
request.SetBasicAuth(wk.basicAuth)
return request
}
func (wk *WuKong) do(req *Request) (resp *Response) {
var (
err error
rawReq *http.Request
rawResp *http.Response
startAt = time.Now()
)
for _, before := range wk.before {
if err = before(req); err != nil {
break
}
}
for i := 0; i < 1; i++ {
if err != nil {
resp = NewResponse(err, nil)
break
}
rawReq, err = req.RawRequest()
if err != nil {
resp = NewResponse(err, nil)
break
}
rawResp, err = wk.client.Do(rawReq)
req.TraceInfo.ElapsedTime = time.Since(startAt)
resp = NewResponse(err, rawResp)
}
for _, after := range wk.after {
if err = after(req, resp); err != nil {
resp = NewResponse(err, rawResp)
break
}
}
return resp
}