Skip to content

Commit accc5da

Browse files
committed
optimize SetError and SetResult
1 parent 029862c commit accc5da

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,8 @@ func (c *Client) do(r *Request) (resp *Response, err error) {
10841084
r.trace = nil
10851085
r.ctx = nil
10861086
resp.body = nil
1087+
resp.result = nil
1088+
resp.error = nil
10871089
}
10881090

10891091
for _, f := range r.client.afterResponse {

middleware.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -274,25 +274,16 @@ func parseResponseBody(c *Client, r *Response) (err error) {
274274
if r.StatusCode == http.StatusNoContent {
275275
return
276276
}
277-
// Handles only JSON or XML content type
278-
if r.Request.Result != nil {
279-
if r.IsSuccess() {
280-
err = unmarshalBody(c, r, r.Request.Result)
281-
if err != nil {
282-
r.Request.Result = nil
283-
}
284-
} else {
285-
r.Request.Result = nil
277+
if r.Request.Result != nil && r.IsSuccess() {
278+
err = unmarshalBody(c, r, r.Request.Result)
279+
if err == nil {
280+
r.result = r.Request.Result
286281
}
287282
}
288-
if r.Request.Error != nil {
289-
if r.IsError() {
290-
err = unmarshalBody(c, r, r.Request.Error)
291-
if err != nil {
292-
r.Request.Error = nil
293-
}
294-
} else {
295-
r.Request.Error = nil
283+
if r.Request.Error != nil && r.IsError() {
284+
err = unmarshalBody(c, r, r.Request.Error)
285+
if err == nil {
286+
r.error = r.Request.Error
296287
}
297288
}
298289
return

request.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ type Request struct {
3232
RawRequest *http.Request
3333
StartTime time.Time
3434
RetryAttempt int
35+
RawURL string // read only
36+
Method string
3537

36-
RawURL string // read only
37-
Method string
38+
isMultiPart bool
39+
isSaveResponse bool
3840
URL *urlpkg.URL
3941
getBody GetContentFunc
4042
uploadCallback UploadCallback
@@ -48,11 +50,9 @@ type Request struct {
4850
dumpOptions *DumpOptions
4951
marshalBody interface{}
5052
ctx context.Context
51-
isMultiPart bool
5253
uploadFiles []*FileUpload
5354
uploadReader []io.ReadCloser
5455
outputFile string
55-
isSaveResponse bool
5656
output io.Writer
5757
trace *clientTrace
5858
dumpBuffer *bytes.Buffer
@@ -313,14 +313,14 @@ func (r *Request) SetDownloadCallbackWithInterval(callback DownloadCallback, min
313313
return r
314314
}
315315

316-
// SetResult set the result that response body will be unmarshaled to if
316+
// SetResult set the result that response body will be unmarshalled to if
317317
// request is success (status `code >= 200 and <= 299`).
318318
func (r *Request) SetResult(result interface{}) *Request {
319319
r.Result = util.GetPointer(result)
320320
return r
321321
}
322322

323-
// SetError set the result that response body will be unmarshaled to if
323+
// SetError set the result that response body will be unmarshalled to if
324324
// request is error ( status `code >= 400`).
325325
func (r *Request) SetError(error interface{}) *Request {
326326
r.Error = util.GetPointer(error)

response.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Response struct {
1313
Request *Request
1414
body []byte
1515
receivedAt time.Time
16+
error interface{}
17+
result interface{}
1618
}
1719

1820
// IsSuccess method returns true if HTTP status `code >= 200 and <= 299` otherwise false.
@@ -41,12 +43,12 @@ func (r *Response) GetContentType() string {
4143

4244
// Result returns the response value as an object if it has one
4345
func (r *Response) Result() interface{} {
44-
return r.Request.Result
46+
return r.result
4547
}
4648

4749
// Error returns the error object if it has one.
4850
func (r *Response) Error() interface{} {
49-
return r.Request.Error
51+
return r.error
5052
}
5153

5254
// TraceInfo returns the TraceInfo from Request.

0 commit comments

Comments
 (0)