Skip to content

Commit b9bd6d5

Browse files
committed
Update xhttp HandlerFunc
1 parent 7c3c13a commit b9bd6d5

File tree

4 files changed

+39
-33
lines changed

4 files changed

+39
-33
lines changed

src/xhttp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ Middleware configuration before or after.
142142

143143
```go
144144
logicMiddleware := func(next xhttp.HandlerFunc) xhttp.HandlerFunc {
145-
return func(req *xhttp.Request, opts *xhttp.RequestOptions) (*xhttp.Response, error) {
145+
return func(req *xhttp.Request) (*xhttp.Response, error) {
146146
// Before-logic
147147
fmt.Printf("Before: %s %s\n", req.Method, req.URL)
148148

149149
// Call the next handler
150-
resp, err := next(req, opts)
150+
resp, err := next(req)
151151

152152
// After-logic
153153
fmt.Printf("After: %s %s\n", req.Method, req.URL)

src/xhttp/client.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func DoRequest(ctx context.Context, req *http.Request, opts ...RequestOption) (*
3737
type Request struct {
3838
http.Request
3939

40+
Options *RequestOptions
41+
4042
Body Body
4143

4244
// Number of retries
@@ -115,7 +117,7 @@ func newResponse(r *http.Response) *Response {
115117
}
116118

117119
func (t *Client) Fetch(ctx context.Context, method string, u string, opts ...RequestOption) (*Response, error) {
118-
o := mergeOptions(t, opts)
120+
mergedOptions := mergeOptions(t, opts)
119121

120122
URL, err := url.Parse(u)
121123
if err != nil {
@@ -124,25 +126,25 @@ func (t *Client) Fetch(ctx context.Context, method string, u string, opts ...Req
124126
req := &http.Request{
125127
Method: method,
126128
URL: URL,
127-
Header: o.Header,
129+
Header: mergedOptions.Header,
128130
}
129-
if o.Body != nil {
131+
if mergedOptions.Body != nil {
130132
// xhttp body > std body
131-
req.Body = io.NopCloser(bytes.NewReader(o.Body))
133+
req.Body = io.NopCloser(bytes.NewReader(mergedOptions.Body))
132134
}
133135
xReq := newRequest(req, false)
134136

135-
if o.RetryOptions != nil {
136-
return doRetry(o, func() (*Response, error) {
137+
if mergedOptions.RetryOptions != nil {
138+
return doRetry(mergedOptions, func() (*Response, error) {
137139
xReq.RetryAttempts++
138-
return t.doXRequest(ctx, xReq, o)
140+
return t.doXRequest(ctx, xReq, mergedOptions)
139141
})
140142
}
141-
return t.doXRequest(ctx, xReq, o)
143+
return t.doXRequest(ctx, xReq, mergedOptions)
142144
}
143145

144146
func (t *Client) NewRequest(method string, u string, opts ...RequestOption) (*Request, error) {
145-
o := mergeOptions(t, opts)
147+
mergedOptions := mergeOptions(t, opts)
146148

147149
URL, err := url.Parse(u)
148150
if err != nil {
@@ -151,48 +153,50 @@ func (t *Client) NewRequest(method string, u string, opts ...RequestOption) (*Re
151153
req := &http.Request{
152154
Method: method,
153155
URL: URL,
154-
Header: o.Header,
156+
Header: mergedOptions.Header,
155157
}
156-
if o.Body != nil {
158+
if mergedOptions.Body != nil {
157159
// xhttp body > std body
158-
req.Body = io.NopCloser(bytes.NewReader(o.Body))
160+
req.Body = io.NopCloser(bytes.NewReader(mergedOptions.Body))
159161
}
160162
return newRequest(req, false), nil
161163
}
162164

163165
func (t *Client) Do(ctx context.Context, req *Request) (*Response, error) {
164-
o := mergeOptions(t, nil)
166+
mergedOptions := mergeOptions(t, nil)
165167

166-
if o.RetryOptions != nil {
167-
return doRetry(o, func() (*Response, error) {
168+
if mergedOptions.RetryOptions != nil {
169+
return doRetry(mergedOptions, func() (*Response, error) {
168170
req.RetryAttempts++
169-
return t.doXRequest(ctx, req, o)
171+
return t.doXRequest(ctx, req, mergedOptions)
170172
})
171173
}
172174

173-
return t.doXRequest(ctx, req, o)
175+
return t.doXRequest(ctx, req, mergedOptions)
174176
}
175177

176178
func (t *Client) DoRequest(ctx context.Context, req *http.Request, opts ...RequestOption) (*Response, error) {
177-
o := mergeOptions(t, opts)
179+
mergedOptions := mergeOptions(t, opts)
178180
xReq := newRequest(req, true)
179181

180-
if o.Header != nil {
181-
xReq.Header = o.Header
182+
if mergedOptions.Header != nil {
183+
xReq.Header = mergedOptions.Header
182184
}
183185

184-
if o.RetryOptions != nil {
185-
return doRetry(o, func() (*Response, error) {
186+
if mergedOptions.RetryOptions != nil {
187+
return doRetry(mergedOptions, func() (*Response, error) {
186188
xReq.RetryAttempts++
187-
return t.doXRequest(ctx, xReq, o)
189+
return t.doXRequest(ctx, xReq, mergedOptions)
188190
})
189191
}
190192

191-
return t.doXRequest(ctx, xReq, o)
193+
return t.doXRequest(ctx, xReq, mergedOptions)
192194
}
193195

194196
func (t *Client) doXRequest(ctx context.Context, xReq *Request, opts *RequestOptions) (*Response, error) {
195-
var finalHandler HandlerFunc = func(xReq *Request, opts *RequestOptions) (*Response, error) {
197+
xReq.Options = opts
198+
199+
var finalHandler HandlerFunc = func(xReq *Request) (*Response, error) {
196200
if !shutdownController.BeginRequest() {
197201
return nil, ErrShutdown
198202
}
@@ -215,5 +219,5 @@ func (t *Client) doXRequest(ctx context.Context, xReq *Request, opts *RequestOpt
215219
finalHandler = opts.Middlewares[i](finalHandler)
216220
}
217221

218-
return finalHandler(xReq, opts)
222+
return finalHandler(xReq)
219223
}

src/xhttp/client_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ func TestMiddlewares(t *testing.T) {
150150
a := assert.New(t)
151151

152152
logicMiddleware := func(next xhttp.HandlerFunc) xhttp.HandlerFunc {
153-
return func(req *xhttp.Request, opts *xhttp.RequestOptions) (*xhttp.Response, error) {
153+
return func(req *xhttp.Request) (*xhttp.Response, error) {
154154
// Before-logic
155155
fmt.Printf("Before: %s %s\n", req.Method, req.URL)
156156

157157
// Call the next handler
158-
resp, err := next(req, opts)
158+
resp, err := next(req)
159159

160160
// After-logic
161161
fmt.Printf("After: %s %s\n", req.Method, req.URL)
@@ -171,13 +171,14 @@ func TestMiddlewares(t *testing.T) {
171171

172172
func TestShutdown(t *testing.T) {
173173
a := assert.New(t)
174+
174175
logicMiddleware := func(next xhttp.HandlerFunc) xhttp.HandlerFunc {
175-
return func(req *xhttp.Request, opts *xhttp.RequestOptions) (*xhttp.Response, error) {
176+
return func(req *xhttp.Request) (*xhttp.Response, error) {
176177
// Before-logic
177178
fmt.Printf("Before: %s %s\n", req.Method, req.URL)
178179

179180
// Call the next handler
180-
resp, err := next(req, opts)
181+
resp, err := next(req)
181182

182183
// After-logic
183184
fmt.Printf("After: %s %s %v\n", req.Method, req.URL, err)
@@ -187,6 +188,7 @@ func TestShutdown(t *testing.T) {
187188
}
188189
_, err := xhttp.Fetch(context.Background(), "GET", "https://github.com/", xhttp.WithMiddleware(logicMiddleware))
189190
a.Nil(err)
191+
190192
wg := sync.WaitGroup{}
191193
wg.Add(3)
192194
for i := 0; i < 3; i++ {

src/xhttp/middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package xhttp
22

33
type Middleware func(next HandlerFunc) HandlerFunc
44

5-
type HandlerFunc func(*Request, *RequestOptions) (*Response, error)
5+
type HandlerFunc func(*Request) (*Response, error)

0 commit comments

Comments
 (0)