From f0575ee5626df03ec9845d78a3eb143b37980aaf Mon Sep 17 00:00:00 2001 From: "felipe.fuerback" Date: Fri, 7 Oct 2022 17:07:27 -0300 Subject: [PATCH] Refactor/code improvements (#247) * Code improvements * Revert log validation improvement --- billing.go | 2 +- client.go | 16 ++++++++++------ unit_test.go | 8 ++++---- webhooks.go | 6 +++--- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/billing.go b/billing.go index fb06e9b..9b9d25e 100644 --- a/billing.go +++ b/billing.go @@ -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", diff --git a/client.go b/client.go index 0347167..43015b3 100644 --- a/client.go +++ b/client.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/http/httputil" "time" @@ -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 @@ -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) diff --git a/unit_test.go b/unit_test.go index bb3d8fd..c261af8 100644 --- a/unit_test.go +++ b/unit_test.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "testing" @@ -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) @@ -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) @@ -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) diff --git a/webhooks.go b/webhooks.go index a6aae5c..b76c75f 100644 --- a/webhooks.go +++ b/webhooks.go @@ -6,7 +6,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" ) @@ -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"),