@@ -512,16 +512,15 @@ func parseRate(r *http.Response) Rate {
512
512
return rate
513
513
}
514
514
515
- // Do sends an API request and returns the API response. The API response is
516
- // JSON decoded and stored in the value pointed to by v, or returned as an
517
- // error if an API error has occurred. If v implements the io.Writer
518
- // interface, the raw response body will be written to v, without attempting to
519
- // first decode it. If rate limit is exceeded and reset time is in the future,
520
- // Do returns *RateLimitError immediately without making a network API call.
515
+ // BareDo sends an API request and lets you handle the api response. If an error
516
+ // or API Error occurs, the error will contain more information. Otherwise you
517
+ // are supposed to read and close the response's Body. If rate limit is exceeded
518
+ // and reset time is in the future, BareDo returns *RateLimitError immediately
519
+ // without making a network API call.
521
520
//
522
- // The provided ctx must be non-nil, if it is nil an error is returned. If it is canceled or times out,
523
- // ctx.Err() will be returned.
524
- func (c * Client ) Do (ctx context.Context , req * http.Request , v interface {} ) (* Response , error ) {
521
+ // The provided ctx must be non-nil, if it is nil an error is returned. If it is
522
+ // canceled or times out, ctx.Err() will be returned.
523
+ func (c * Client ) BareDo (ctx context.Context , req * http.Request ) (* Response , error ) {
525
524
if ctx == nil {
526
525
return nil , errors .New ("context must be non-nil" )
527
526
}
@@ -558,8 +557,6 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
558
557
return nil , err
559
558
}
560
559
561
- defer resp .Body .Close ()
562
-
563
560
response := newResponse (resp )
564
561
565
562
c .rateMu .Lock ()
@@ -568,6 +565,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
568
565
569
566
err = CheckResponse (resp )
570
567
if err != nil {
568
+ defer resp .Body .Close ()
571
569
// Special case for AcceptedErrors. If an AcceptedError
572
570
// has been encountered, the response's payload will be
573
571
// added to the AcceptedError and returned.
@@ -581,27 +579,43 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
581
579
}
582
580
583
581
aerr .Raw = b
584
- return response , aerr
582
+ err = aerr
585
583
}
584
+ }
585
+ return response , err
586
+ }
586
587
587
- return response , err
588
+ // Do sends an API request and returns the API response. The API response is
589
+ // JSON decoded and stored in the value pointed to by v, or returned as an
590
+ // error if an API error has occurred. If v implements the io.Writer interface,
591
+ // the raw response body will be written to v, without attempting to first
592
+ // decode it. If v is nil, and no error hapens, the response is returned as is.
593
+ // If rate limit is exceeded and reset time is in the future, Do returns
594
+ // *RateLimitError immediately without making a network API call.
595
+ //
596
+ // The provided ctx must be non-nil, if it is nil an error is returned. If it
597
+ // is canceled or times out, ctx.Err() will be returned.
598
+ func (c * Client ) Do (ctx context.Context , req * http.Request , v interface {}) (* Response , error ) {
599
+ resp , err := c .BareDo (ctx , req )
600
+ if err != nil {
601
+ return resp , err
588
602
}
603
+ defer resp .Body .Close ()
589
604
590
- if v != nil {
591
- if w , ok := v .(io. Writer ); ok {
592
- io .Copy ( w , resp . Body )
593
- } else {
594
- decErr := json . NewDecoder ( resp . Body ). Decode ( v )
595
- if decErr == io . EOF {
596
- decErr = nil // ignore EOF errors caused by empty response body
597
- }
598
- if decErr != nil {
599
- err = decErr
600
- }
605
+ switch v := v .( type ) {
606
+ case nil :
607
+ case io.Writer :
608
+ _ , err = io . Copy ( v , resp . Body )
609
+ default :
610
+ decErr := json . NewDecoder ( resp . Body ). Decode ( v )
611
+ if decErr == io . EOF {
612
+ decErr = nil // ignore EOF errors caused by empty response body
613
+ }
614
+ if decErr != nil {
615
+ err = decErr
601
616
}
602
617
}
603
-
604
- return response , err
618
+ return resp , err
605
619
}
606
620
607
621
// checkRateLimitBeforeDo does not make any network calls, but uses existing knowledge from
0 commit comments