Skip to content

Commit

Permalink
Merge pull request #137 from XdpCs/fix-ioutil
Browse files Browse the repository at this point in the history
refactor(nethttp_request_adapter.go): As of Go 1.16, the functions in the ioutil package have been deprecated.
  • Loading branch information
baywet authored Jan 22, 2024
2 parents 7751d0c + dea4d65 commit 6cc53e2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.1.2] - 2024-01-20

### Changed

- Changed the code by replacing ioutil.ReadAll and ioutil.NopCloser with io.ReadAll and io.NopCloser, respectively, due to their deprecation.


## [1.1.1] - 2023-11-22

### Added
Expand Down
7 changes: 3 additions & 4 deletions compression_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"net/http"

abstractions "github.com/microsoft/kiota-abstractions-go"
Expand Down Expand Up @@ -82,7 +81,7 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r
span.SetAttributes(attribute.Bool("http.request_body_compressed", true))
}

unCompressedBody, err := ioutil.ReadAll(req.Body)
unCompressedBody, err := io.ReadAll(req.Body)
unCompressedContentLength := req.ContentLength
if err != nil {
if span != nil {
Expand Down Expand Up @@ -116,7 +115,7 @@ func (c *CompressionHandler) Intercept(pipeline Pipeline, middlewareIndex int, r
// If response has status 415 retry request with uncompressed body
if resp.StatusCode == 415 {
delete(req.Header, "Content-Encoding")
req.Body = ioutil.NopCloser(bytes.NewBuffer(unCompressedBody))
req.Body = io.NopCloser(bytes.NewBuffer(unCompressedBody))
req.ContentLength = unCompressedContentLength

if span != nil {
Expand All @@ -141,5 +140,5 @@ func compressReqBody(reqBody []byte) (io.ReadCloser, int, error) {
return nil, 0, err
}

return ioutil.NopCloser(&buffer), buffer.Len(), nil
return io.NopCloser(&buffer), buffer.Len(), nil
}
4 changes: 2 additions & 2 deletions decompression_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nethttplibrary
import (
"compress/gzip"
"encoding/json"
"io/ioutil"
"io"
nethttp "net/http"
httptest "net/http/httptest"
"testing"
Expand All @@ -30,7 +30,7 @@ func TestTransportDecompressesResponse(t *testing.T) {
client.Transport = NewCustomTransport(NewCompressionHandler())

resp, _ := client.Get(testServer.URL)
respBody, _ := ioutil.ReadAll(resp.Body)
respBody, _ := io.ReadAll(resp.Body)

assert.True(t, resp.Uncompressed)
assert.Equal(t, string(respBody), `{"email":"[email protected]","name":"Test"}`)
Expand Down
7 changes: 3 additions & 4 deletions nethttp_request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"io"
"io/ioutil"
nethttp "net/http"
"reflect"
"regexp"
Expand Down Expand Up @@ -566,7 +565,7 @@ func (a *NetHttpRequestAdapter) SendPrimitive(ctx context.Context, requestInfo *
return nil, nil
}
if typeName == "[]byte" {
res, err := ioutil.ReadAll(response.Body)
res, err := io.ReadAll(response.Body)
if err != nil {
span.RecordError(err)
return nil, err
Expand Down Expand Up @@ -702,7 +701,7 @@ func (a *NetHttpRequestAdapter) SendNoContent(ctx context.Context, requestInfo *
func (a *NetHttpRequestAdapter) getRootParseNode(ctx context.Context, response *nethttp.Response, spanForAttributes trace.Span) (absser.ParseNode, context.Context, error) {
ctx, span := otel.GetTracerProvider().Tracer(a.observabilityOptions.GetTracerInstrumentationName()).Start(ctx, "getRootParseNode")
defer span.End()
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
spanForAttributes.RecordError(err)
return nil, ctx, err
Expand All @@ -718,7 +717,7 @@ func (a *NetHttpRequestAdapter) getRootParseNode(ctx context.Context, response *
return rootNode, ctx, err
}
func (a *NetHttpRequestAdapter) purge(response *nethttp.Response) error {
_, _ = ioutil.ReadAll(response.Body) //we don't care about errors comming from reading the body, just trying to purge anything that maybe left
_, _ = io.ReadAll(response.Body) //we don't care about errors comming from reading the body, just trying to purge anything that maybe left
err := response.Body.Close()
if err != nil {
return err
Expand Down

0 comments on commit 6cc53e2

Please sign in to comment.