Skip to content

Commit

Permalink
Refactor/code improvements (#247)
Browse files Browse the repository at this point in the history
* Code improvements

* Revert log validation improvement
  • Loading branch information
felipe.fuerback authored Oct 7, 2022
1 parent defe3e0 commit f0575ee
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *Client) CreateBillingPlan(ctx context.Context, plan BillingPlan) (*Crea
// UpdateBillingPlan updates values inside a billing plan
// Endpoint: PATCH /v1/payments/billing-plans
func (c *Client) UpdateBillingPlan(ctx context.Context, planId string, pathValues map[string]map[string]interface{}) error {
patchData := []Patch{}
var patchData []Patch
for path, data := range pathValues {
patchData = append(patchData, Patch{
Operation: "replace",
Expand Down
16 changes: 10 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"time"
Expand Down Expand Up @@ -105,14 +104,19 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
if err != nil {
return err
}
defer resp.Body.Close()
defer func(Body io.ReadCloser) error {
return Body.Close()
}(resp.Body)

if resp.StatusCode < 200 || resp.StatusCode > 299 {
errResp := &ErrorResponse{Response: resp}
data, err = ioutil.ReadAll(resp.Body)
data, err = io.ReadAll(resp.Body)

if err == nil && len(data) > 0 {
json.Unmarshal(data, errResp)
err := json.Unmarshal(data, errResp)
if err != nil {
return err
}
}

return errResp
Expand All @@ -122,8 +126,8 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
}

if w, ok := v.(io.Writer); ok {
io.Copy(w, resp.Body)
return nil
_, err := io.Copy(w, resp.Body)
return err
}

return json.NewDecoder(resp.Body).Decode(v)
Expand Down
8 changes: 4 additions & 4 deletions unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -536,7 +536,7 @@ func (ts *webprofileTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request
func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
var data map[string]interface{}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -572,7 +572,7 @@ func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
func (ts *webprofileTestServer) createWithoutName(w http.ResponseWriter, r *http.Request) {
var data map[string]interface{}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -600,7 +600,7 @@ func (ts *webprofileTestServer) createWithoutName(w http.ResponseWriter, r *http
func (ts *webprofileTestServer) updatevalid(w http.ResponseWriter, r *http.Request) {
var data map[string]interface{}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
6 changes: 3 additions & 3 deletions webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -96,12 +96,12 @@ func (c *Client) VerifyWebhookSignature(ctx context.Context, httpReq *http.Reque
// Read the content
var bodyBytes []byte
if httpReq.Body != nil {
bodyBytes, _ = ioutil.ReadAll(httpReq.Body)
bodyBytes, _ = io.ReadAll(httpReq.Body)
} else {
return nil, errors.New("Cannot verify webhook for HTTP Request with empty body.")
}
// Restore the io.ReadCloser to its original state
httpReq.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
httpReq.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))

verifyRequest := verifyWebhookSignatureRequest{
AuthAlgo: httpReq.Header.Get("PAYPAL-AUTH-ALGO"),
Expand Down

0 comments on commit f0575ee

Please sign in to comment.