@@ -37,6 +37,8 @@ func DoRequest(ctx context.Context, req *http.Request, opts ...RequestOption) (*
3737type 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
117119func (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
144146func (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
163165func (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
176178func (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
194196func (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}
0 commit comments