Skip to content

Commit

Permalink
Use Payload() for reading response bodies (#16911)
Browse files Browse the repository at this point in the history
Poller.FinalResponse() wasn't restoring the response body after reading.
Removed duplicate definitions from GetJSON and bodyDownloadPolicy.
  • Loading branch information
jhendrixMSFT authored Jan 27, 2022
1 parent d653770 commit e6b2988
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 12 deletions.
1 change: 1 addition & 0 deletions sdk/azcore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Breaking Changes

### Bugs Fixed
* Restore response body after reading in `Poller.FinalResponse()`

### Other Changes
* `BearerTokenPolicy` is more resilient to transient authentication failures
Expand Down
4 changes: 1 addition & 3 deletions sdk/azcore/internal/pollers/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"time"
Expand Down Expand Up @@ -154,8 +153,7 @@ func (l *Poller) FinalResponse(ctx context.Context, respType interface{}) (*http
log.Write(log.EventLRO, "final response specifies a response type but no payload was received")
return l.resp, nil
}
body, err := ioutil.ReadAll(l.resp.Body)
l.resp.Body.Close()
body, err := shared.Payload(l.resp)
if err != nil {
return nil, err
}
Expand Down
6 changes: 1 addition & 5 deletions sdk/azcore/internal/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package shared

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -76,16 +75,13 @@ var ErrNoBody = errors.New("the response did not contain a body")
// GetJSON reads the response body into a raw JSON object.
// It returns ErrNoBody if there was no content.
func GetJSON(resp *http.Response) (map[string]interface{}, error) {
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
body, err := Payload(resp)
if err != nil {
return nil, err
}
if len(body) == 0 {
return nil, ErrNoBody
}
// put the body back so it's available to others
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
// unmarshall the body to get the value
var jsonBody map[string]interface{}
if err = json.Unmarshal(body, &jsonBody); err != nil {
Expand Down
5 changes: 1 addition & 4 deletions sdk/azcore/runtime/policy_body_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package runtime

import (
"fmt"
"io/ioutil"
"net/http"
"strings"

Expand All @@ -30,12 +29,10 @@ func bodyDownloadPolicy(req *policy.Request) (*http.Response, error) {
}
// Either bodyDownloadPolicyOpValues was not specified (so skip is false)
// or it was specified and skip is false: don't skip downloading the body
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
_, err = shared.Payload(resp)
if err != nil {
return resp, newBodyDownloadError(err, req)
}
resp.Body = shared.NewNopClosingBytesReader(b)
return resp, err
}

Expand Down

0 comments on commit e6b2988

Please sign in to comment.