From 2c6b470ed7a39b877223c3cc49a325eb968084bb Mon Sep 17 00:00:00 2001 From: Daniel Melton Date: Sat, 15 Jun 2019 22:39:08 -0400 Subject: [PATCH 1/2] code style efficiencies added --- authorization.go | 20 ++++++++++++-------- billing.go | 16 ++++++++-------- client.go | 16 +++++++--------- filter.go | 7 ++----- identity.go | 12 ++++++------ payout.go | 23 ++++++++++------------- vault.go | 24 ++++++++++++------------ webprofile.go | 5 +++-- 8 files changed, 60 insertions(+), 63 deletions(-) diff --git a/authorization.go b/authorization.go index 93c1913..cc792d8 100644 --- a/authorization.go +++ b/authorization.go @@ -12,11 +12,12 @@ import ( func (c *Client) GetAuthorization(authID string) (*Authorization, error) { buf := bytes.NewBuffer([]byte("")) req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/payments/authorization/", authID), buf) + auth := &Authorization{} + if err != nil { - return &Authorization{}, err + return auth, err } - auth := &Authorization{} err = c.SendWithAuth(req, auth) return auth, err } @@ -29,11 +30,12 @@ func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture b buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `,"total":"` + a.Total + `"},"is_final_capture":` + isFinalStr + `}`)) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/capture"), buf) + capture := &Capture{} + if err != nil { - return &Capture{}, err + return capture, err } - capture := &Capture{} err = c.SendWithAuth(req, capture) return capture, err } @@ -43,11 +45,12 @@ func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture b func (c *Client) VoidAuthorization(authID string) (*Authorization, error) { buf := bytes.NewBuffer([]byte("")) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/void"), buf) + auth := &Authorization{} + if err != nil { - return &Authorization{}, err + return auth, err } - auth := &Authorization{} err = c.SendWithAuth(req, auth) return auth, err } @@ -58,11 +61,12 @@ func (c *Client) VoidAuthorization(authID string) (*Authorization, error) { func (c *Client) ReauthorizeAuthorization(authID string, a *Amount) (*Authorization, error) { buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `","total":"` + a.Total + `"}}`)) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/reauthorize"), buf) + auth := &Authorization{} + if err != nil { - return &Authorization{}, err + return auth, err } - auth := &Authorization{} err = c.SendWithAuth(req, auth) return auth, err } diff --git a/billing.go b/billing.go index 36201fa..390cbb5 100644 --- a/billing.go +++ b/billing.go @@ -94,24 +94,24 @@ func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementRes // Endpoint: POST /v2/payments/billing-agreements/token/agreement-execute func (c *Client) ExecuteApprovedAgreement(token string) (*ExecuteAgreementResponse, error) { req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/billing-agreements/"+token+"/agreement-execute"), nil) + response := &ExecuteAgreementResponse{} + if err != nil { - return &ExecuteAgreementResponse{}, err + return response, err } req.SetBasicAuth(c.ClientID, c.Secret) req.Header.Set("Authorization", "Bearer "+c.Token.Token) - e := ExecuteAgreementResponse{} - - if err = c.SendWithAuth(req, &e); err != nil { - return &e, err + if err = c.SendWithAuth(req, response); err != nil { + return response, err } - if e.ID == "" { - return &e, errors.New("Unable to execute agreement with token=" + token) + if response.ID == "" { + return response, errors.New("Unable to execute agreement with token=" + token) } - return &e, err + return response, err } // ListBillingPlans lists billing-plans diff --git a/client.go b/client.go index ee78652..617b491 100644 --- a/client.go +++ b/client.go @@ -39,16 +39,16 @@ func (c *Client) GetAccessToken() (*TokenResponse, error) { req.Header.Set("Content-type", "application/x-www-form-urlencoded") - t := TokenResponse{} - err = c.SendWithBasicAuth(req, &t) + response := &TokenResponse{} + err = c.SendWithBasicAuth(req, response) // Set Token fur current Client - if t.Token != "" { - c.Token = &t - c.tokenExpiresAt = time.Now().Add(time.Duration(t.ExpiresIn) * time.Second) + if response.Token != "" { + c.Token = response + c.tokenExpiresAt = time.Now().Add(time.Duration(response.ExpiresIn) * time.Second) } - return &t, err + return response, err } // SetHTTPClient sets *http.Client to current client @@ -107,7 +107,6 @@ func (c *Client) Send(req *http.Request, v interface{}) error { return errResp } - if v == nil { return nil } @@ -130,7 +129,7 @@ func (c *Client) SendWithAuth(req *http.Request, v interface{}) error { // to happen outside of the locked section. if c.Token != nil { - if !c.tokenExpiresAt.IsZero() && c.tokenExpiresAt.Sub(time.Now()) < RequestNewTokenBeforeExpiresIn { + if !c.tokenExpiresAt.IsZero() && time.Until(c.tokenExpiresAt) < RequestNewTokenBeforeExpiresIn { // c.Token will be updated in GetAccessToken call if _, err := c.GetAccessToken(); err != nil { c.Unlock() @@ -159,7 +158,6 @@ func (c *Client) SendWithBasicAuth(req *http.Request, v interface{}) error { func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Request, error) { var buf io.Reader if payload != nil { - var b []byte b, err := json.Marshal(&payload) if err != nil { return nil, err diff --git a/filter.go b/filter.go index 81d6f1f..d067c65 100644 --- a/filter.go +++ b/filter.go @@ -7,16 +7,13 @@ import ( const format = "2006-01-02T15:04:05Z" -type stringable interface { - String() string -} type Filter struct { - fields []stringable + fields []fmt.Stringer } func (s *Filter) String() string { - filter := "" + var filter string for i, f := range s.fields { if i == 0 { filter = "?" + f.String() diff --git a/identity.go b/identity.go index 0328308..f59fa98 100644 --- a/identity.go +++ b/identity.go @@ -9,7 +9,7 @@ import ( // GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code. // Endpoint: POST /v1/identity/openidconnect/tokenservice -func (c *Client) GrantNewAccessTokenFromAuthCode(code string, redirectURI string) (*TokenResponse, error) { +func (c *Client) GrantNewAccessTokenFromAuthCode(code, redirectURI string) (*TokenResponse, error) { token := &TokenResponse{} q := url.Values{} @@ -57,16 +57,16 @@ func (c *Client) GrantNewAccessTokenFromRefreshToken(refreshToken string) (*Toke // Endpoint: GET /v1/identity/openidconnect/userinfo/?schema= // Pass the schema that is used to return as per openidconnect protocol. The only supported schema value is openid. func (c *Client) GetUserInfo(schema string) (*UserInfo, error) { - u := UserInfo{} + u := &UserInfo{} req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/identity/openidconnect/userinfo/?schema=", schema), nil) if err != nil { - return &u, err + return u, err } - if err = c.SendWithAuth(req, &u); err != nil { - return &u, err + if err = c.SendWithAuth(req, u); err != nil { + return u, err } - return &u, nil + return u, nil } diff --git a/payout.go b/payout.go index 8db6046..86b5056 100644 --- a/payout.go +++ b/payout.go @@ -9,12 +9,12 @@ import ( // Endpoint: POST /v1/payments/payouts func (c *Client) CreateSinglePayout(p Payout) (*PayoutResponse, error) { req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts"), p) - if err != nil { - return &PayoutResponse{}, err - } - response := &PayoutResponse{} + if err != nil { + return response, err + } + if err = c.SendWithAuth(req, response); err != nil { return response, err } @@ -27,13 +27,12 @@ func (c *Client) CreateSinglePayout(p Payout) (*PayoutResponse, error) { // Endpoint: GET /v1/payments/payouts/ID func (c *Client) GetPayout(payoutBatchID string) (*PayoutResponse, error) { req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts/"+payoutBatchID), nil) + response := &PayoutResponse{} if err != nil { - return &PayoutResponse{}, err + return response, err } - response := &PayoutResponse{} - if err = c.SendWithAuth(req, response); err != nil { return response, err } @@ -46,13 +45,12 @@ func (c *Client) GetPayout(payoutBatchID string) (*PayoutResponse, error) { // Endpoint: GET /v1/payments/payouts-item/ID func (c *Client) GetPayoutItem(payoutItemID string) (*PayoutItemResponse, error) { req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts-item/"+payoutItemID), nil) + response := &PayoutItemResponse{} if err != nil { - return &PayoutItemResponse{}, err + return response, err } - response := &PayoutItemResponse{} - if err = c.SendWithAuth(req, response); err != nil { return response, err } @@ -65,13 +63,12 @@ func (c *Client) GetPayoutItem(payoutItemID string) (*PayoutItemResponse, error) // Endpoint: POST /v1/payments/payouts-item/ID/cancel func (c *Client) CancelPayoutItem(payoutItemID string) (*PayoutItemResponse, error) { req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts-item/"+payoutItemID+"/cancel"), nil) + response := &PayoutItemResponse{} if err != nil { - return &PayoutItemResponse{}, err + return response, err } - response := &PayoutItemResponse{} - if err = c.SendWithAuth(req, response); err != nil { return response, err } diff --git a/vault.go b/vault.go index 634683e..ec361cb 100644 --- a/vault.go +++ b/vault.go @@ -12,13 +12,13 @@ func (c *Client) StoreCreditCard(cc CreditCard) (*CreditCard, error) { return nil, err } - response := CreditCard{} + response := &CreditCard{} - if err = c.SendWithAuth(req, &response); err != nil { + if err = c.SendWithAuth(req, response); err != nil { return nil, err } - return &response, nil + return response, nil } // DeleteCreditCard func @@ -44,13 +44,13 @@ func (c *Client) GetCreditCard(id string) (*CreditCard, error) { return nil, err } - response := CreditCard{} + response := &CreditCard{} - if err = c.SendWithAuth(req, &response); err != nil { + if err = c.SendWithAuth(req, response); err != nil { return nil, err } - return &response, nil + return response, nil } // GetCreditCards func @@ -70,13 +70,13 @@ func (c *Client) GetCreditCards(ccf *CreditCardsFilter) (*CreditCards, error) { return nil, err } - response := CreditCards{} + response := &CreditCards{} - if err = c.SendWithAuth(req, &response); err != nil { + if err = c.SendWithAuth(req, response); err != nil { return nil, err } - return &response, nil + return response, nil } // PatchCreditCard func @@ -87,11 +87,11 @@ func (c *Client) PatchCreditCard(id string, ccf []CreditCardField) (*CreditCard, return nil, err } - response := CreditCard{} + response := &CreditCard{} - if err = c.SendWithAuth(req, &response); err != nil { + if err = c.SendWithAuth(req, response); err != nil { return nil, err } - return &response, nil + return response, nil } diff --git a/webprofile.go b/webprofile.go index f2feca0..15161f5 100644 --- a/webprofile.go +++ b/webprofile.go @@ -13,11 +13,12 @@ import ( func (c *Client) CreateWebProfile(wp WebProfile) (*WebProfile, error) { url := fmt.Sprintf("%s%s", c.APIBase, "/v1/payment-experience/web-profiles") req, err := c.NewRequest("POST", url, wp) + response := &WebProfile{} + if err != nil { - return &WebProfile{}, err + return response, err } - response := &WebProfile{} if err = c.SendWithAuth(req, response); err != nil { return response, err From cc05027dd1dd99727a224d494462444b998892de Mon Sep 17 00:00:00 2001 From: Daniel Melton Date: Mon, 17 Jun 2019 07:39:03 -0400 Subject: [PATCH 2/2] revert of time.Until which fails on 1.5,1.6, & 1.7 --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index 617b491..159c785 100644 --- a/client.go +++ b/client.go @@ -129,7 +129,7 @@ func (c *Client) SendWithAuth(req *http.Request, v interface{}) error { // to happen outside of the locked section. if c.Token != nil { - if !c.tokenExpiresAt.IsZero() && time.Until(c.tokenExpiresAt) < RequestNewTokenBeforeExpiresIn { + if !c.tokenExpiresAt.IsZero() && c.tokenExpiresAt.Sub(time.Now()) < RequestNewTokenBeforeExpiresIn { // c.Token will be updated in GetAccessToken call if _, err := c.GetAccessToken(); err != nil { c.Unlock()