Skip to content

Commit f0575ee

Browse files
author
felipe.fuerback
authored
Refactor/code improvements (#247)
* Code improvements * Revert log validation improvement
1 parent defe3e0 commit f0575ee

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

billing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (c *Client) CreateBillingPlan(ctx context.Context, plan BillingPlan) (*Crea
7272
// UpdateBillingPlan updates values inside a billing plan
7373
// Endpoint: PATCH /v1/payments/billing-plans
7474
func (c *Client) UpdateBillingPlan(ctx context.Context, planId string, pathValues map[string]map[string]interface{}) error {
75-
patchData := []Patch{}
75+
var patchData []Patch
7676
for path, data := range pathValues {
7777
patchData = append(patchData, Patch{
7878
Operation: "replace",

client.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"io"
10-
"io/ioutil"
1110
"net/http"
1211
"net/http/httputil"
1312
"time"
@@ -105,14 +104,19 @@ func (c *Client) Send(req *http.Request, v interface{}) error {
105104
if err != nil {
106105
return err
107106
}
108-
defer resp.Body.Close()
107+
defer func(Body io.ReadCloser) error {
108+
return Body.Close()
109+
}(resp.Body)
109110

110111
if resp.StatusCode < 200 || resp.StatusCode > 299 {
111112
errResp := &ErrorResponse{Response: resp}
112-
data, err = ioutil.ReadAll(resp.Body)
113+
data, err = io.ReadAll(resp.Body)
113114

114115
if err == nil && len(data) > 0 {
115-
json.Unmarshal(data, errResp)
116+
err := json.Unmarshal(data, errResp)
117+
if err != nil {
118+
return err
119+
}
116120
}
117121

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

124128
if w, ok := v.(io.Writer); ok {
125-
io.Copy(w, resp.Body)
126-
return nil
129+
_, err := io.Copy(w, resp.Body)
130+
return err
127131
}
128132

129133
return json.NewDecoder(resp.Body).Decode(v)

unit_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"net/http/httptest"
1010
"testing"
@@ -536,7 +536,7 @@ func (ts *webprofileTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request
536536
func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
537537
var data map[string]interface{}
538538

539-
body, err := ioutil.ReadAll(r.Body)
539+
body, err := io.ReadAll(r.Body)
540540

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

575-
body, err := ioutil.ReadAll(r.Body)
575+
body, err := io.ReadAll(r.Body)
576576

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

603-
body, err := ioutil.ReadAll(r.Body)
603+
body, err := io.ReadAll(r.Body)
604604

605605
if err != nil {
606606
http.Error(w, err.Error(), http.StatusInternalServerError)

webhooks.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
)
1212

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

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

0 commit comments

Comments
 (0)