Refactor/code improvements (#247)

* Code improvements

* Revert log validation improvement
This commit is contained in:
felipe.fuerback 2022-10-07 17:07:27 -03:00 committed by GitHub
parent defe3e09a4
commit f0575ee562
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 14 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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)

View File

@ -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"),