code style efficiencies added

This commit is contained in:
Daniel Melton 2019-06-15 22:39:08 -04:00
parent 17ba889ca0
commit 2c6b470ed7
8 changed files with 60 additions and 63 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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=<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
}

View File

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

View File

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

View File

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