Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push parameter encoding into BackendConfiguration.Call #581

Merged
merged 5 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,5 +444,6 @@ type PayoutSchedule struct {

// AccountRejectParams is the structure for the Reject function.
type AccountRejectParams struct {
Params `form:"*"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this so that it's compliant with the new ParamsContainer. It should've been there anyway.

Reason *string `form:"reason"`
}
81 changes: 13 additions & 68 deletions account/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,8 @@ func New(params *stripe.AccountParams) (*stripe.Account, error) {
}

func (c Client) New(params *stripe.AccountParams) (*stripe.Account, error) {
body := &form.Values{}

// Type is now required on creation and not allowed on update
// It can't be passed if you pass `from_recipient` though
if params.FromRecipient != nil {
body.Add("type", stripe.StringValue(params.Type))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that type already had a form tag, I don't think this was doing anything useful (just duplicating work), so I just removed it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirmed that it did not do what I hoped it did at the time so removing was the right call.

}

form.AppendTo(body, params)

acct := &stripe.Account{}
err := c.B.Call("POST", "/accounts", c.Key, body, &params.Params, acct)

err := c.B.Call("POST", "/accounts", c.Key, params, acct)
return acct, err
}

Expand All @@ -40,8 +29,7 @@ func Get() (*stripe.Account, error) {

func (c Client) Get() (*stripe.Account, error) {
account := &stripe.Account{}
err := c.B.Call("GET", "/account", c.Key, nil, nil, account)

err := c.B.Call("GET", "/account", c.Key, nil, account)
return account, err
}

Expand All @@ -51,18 +39,9 @@ func GetByID(id string, params *stripe.AccountParams) (*stripe.Account, error) {
}

func (c Client) GetByID(id string, params *stripe.AccountParams) (*stripe.Account, error) {
var body *form.Values
var commonParams *stripe.Params

if params != nil {
commonParams = &params.Params
body = &form.Values{}
form.AppendTo(body, params)
}

path := stripe.FormatURLPath("/accounts/%s", id)
account := &stripe.Account{}
err := c.B.Call("GET", stripe.FormatURLPath("/accounts/%s", id), c.Key, body, commonParams, account)

err := c.B.Call("GET", path, c.Key, params, account)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random but is there a reason Call can't get to c.Key on its own?

Copy link
Contributor

@brandur-stripe brandur-stripe Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Every client package (e.g., charge/, sub/, etc.) defines its own Client which is where a Key is configured:

type Client struct {
	B   stripe.Backend
	Key string
}

Client has a reference to a Backend, but not vice versa, so that backend can't reach back into the client to get at the Key field. That's why it's injected with every call.

I don't think I would have designed it this way, and there might be something we can do to refactor to remove even more boilerplate, but I don't want to mess around with this too much for now.

return account, err
}

Expand All @@ -72,18 +51,9 @@ func Update(id string, params *stripe.AccountParams) (*stripe.Account, error) {
}

func (c Client) Update(id string, params *stripe.AccountParams) (*stripe.Account, error) {
var body *form.Values
var commonParams *stripe.Params

if params != nil {
commonParams = &params.Params
body = &form.Values{}
form.AppendTo(body, params)
}

path := stripe.FormatURLPath("/accounts/%s", id)
acct := &stripe.Account{}
err := c.B.Call("POST", stripe.FormatURLPath("/accounts/%s", id), c.Key, body, commonParams, acct)

err := c.B.Call("POST", path, c.Key, params, acct)
return acct, err
}

Expand All @@ -93,18 +63,9 @@ func Del(id string, params *stripe.AccountParams) (*stripe.Account, error) {
}

func (c Client) Del(id string, params *stripe.AccountParams) (*stripe.Account, error) {
var body *form.Values
var commonParams *stripe.Params

if params != nil {
body = &form.Values{}
form.AppendTo(body, params)
commonParams = &params.Params
}

path := stripe.FormatURLPath("/accounts/%s", id)
acct := &stripe.Account{}
err := c.B.Call("DELETE", stripe.FormatURLPath("/accounts/%s", id), c.Key, body, commonParams, acct)

err := c.B.Call("DELETE", path, c.Key, params, acct)
return acct, err
}

Expand All @@ -114,13 +75,9 @@ func Reject(id string, params *stripe.AccountRejectParams) (*stripe.Account, err
}

func (c Client) Reject(id string, params *stripe.AccountRejectParams) (*stripe.Account, error) {
body := &form.Values{}
if params.Reason != nil {
body.Add("reason", stripe.StringValue(params.Reason))
}
path := stripe.FormatURLPath("/accounts/%s/reject", id)
acct := &stripe.Account{}
err := c.B.Call("POST", stripe.FormatURLPath("/accounts/%s/reject", id), c.Key, body, nil, acct)

err := c.B.Call("POST", path, c.Key, params, acct)
return acct, err
}

Expand All @@ -129,22 +86,10 @@ func List(params *stripe.AccountListParams) *Iter {
return getC().List(params)
}

func (c Client) List(params *stripe.AccountListParams) *Iter {
var body *form.Values
var lp *stripe.ListParams
var p *stripe.Params

if params != nil {
body = &form.Values{}

form.AppendTo(body, params)
lp = &params.ListParams
p = params.ToParams()
}

return &Iter{stripe.GetIter(lp, body, func(b *form.Values) ([]interface{}, stripe.ListMeta, error) {
func (c Client) List(listParams *stripe.AccountListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.AccountList{}
err := c.B.Call("GET", "/accounts", c.Key, b, p, list)
err := c.B.CallRaw("GET", "/accounts", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
Expand Down
48 changes: 8 additions & 40 deletions applepaydomain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ func New(params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain, error) {
}

func (c Client) New(params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain, error) {
body := &form.Values{}
form.AppendTo(body, params)

domain := &stripe.ApplePayDomain{}
err := c.B.Call("POST", "/apple_pay/domains", c.Key, body, &params.Params, domain)
err := c.B.Call("POST", "/apple_pay/domains", c.Key, params, domain)
return domain, err
}

Expand All @@ -32,18 +29,9 @@ func Get(id string, params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain
}

func (c Client) Get(id string, params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain, error) {
var body *form.Values
var commonParams *stripe.Params

if params != nil {
body = &form.Values{}
commonParams = &params.Params
form.AppendTo(body, params)
}

path := stripe.FormatURLPath("/apple_pay/domains/%s", id)
domain := &stripe.ApplePayDomain{}
err := c.B.Call("GET", stripe.FormatURLPath("/apple_pay/domains/%s", id), c.Key, body, commonParams, domain)

err := c.B.Call("GET", path, c.Key, params, domain)
return domain, err
}

Expand All @@ -53,18 +41,9 @@ func Del(id string, params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain
}

func (c Client) Del(id string, params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain, error) {
var body *form.Values
var commonParams *stripe.Params

if params != nil {
body = &form.Values{}
form.AppendTo(body, params)
commonParams = &params.Params
}

path := stripe.FormatURLPath("/apple_pay/domains/%s", id)
domain := &stripe.ApplePayDomain{}
err := c.B.Call("DELETE", stripe.FormatURLPath("/apple_pay/domains/%s", id), c.Key, body, commonParams, domain)

err := c.B.Call("DELETE", path, c.Key, params, domain)
return domain, err
}

Expand All @@ -73,21 +52,10 @@ func List(params *stripe.ApplePayDomainListParams) *Iter {
return getC().List(params)
}

func (c Client) List(params *stripe.ApplePayDomainListParams) *Iter {
var body *form.Values
var lp *stripe.ListParams
var p *stripe.Params

if params != nil {
body = &form.Values{}
form.AppendTo(body, params)
lp = &params.ListParams
p = params.ToParams()
}

return &Iter{stripe.GetIter(lp, body, func(b *form.Values) ([]interface{}, stripe.ListMeta, error) {
func (c Client) List(listParams *stripe.ApplePayDomainListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.ApplePayDomainList{}
err := c.B.Call("GET", "/apple_pay/domains", c.Key, b, p, list)
err := c.B.CallRaw("GET", "/apple_pay/domains", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
Expand Down
42 changes: 6 additions & 36 deletions balance/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,8 @@ func Get(params *stripe.BalanceParams) (*stripe.Balance, error) {
}

func (c Client) Get(params *stripe.BalanceParams) (*stripe.Balance, error) {
var body *form.Values
var commonParams *stripe.Params

if params != nil {
commonParams = &params.Params
body = &form.Values{}
form.AppendTo(body, params)
}

balance := &stripe.Balance{}
err := c.B.Call("GET", "/balance", c.Key, body, commonParams, balance)

err := c.B.Call("GET", "/balance", c.Key, params, balance)
return balance, err
}

Expand All @@ -41,18 +31,9 @@ func GetBalanceTransaction(id string, params *stripe.BalanceTransactionParams) (
}

func (c Client) GetBalanceTransaction(id string, params *stripe.BalanceTransactionParams) (*stripe.BalanceTransaction, error) {
var body *form.Values
var commonParams *stripe.Params

if params != nil {
commonParams = &params.Params
body = &form.Values{}
form.AppendTo(body, params)
}

path := stripe.FormatURLPath("/balance/history/%s", id)
balance := &stripe.BalanceTransaction{}
err := c.B.Call("GET", stripe.FormatURLPath("/balance/history/%s", id), c.Key, body, commonParams, balance)

err := c.B.Call("GET", path, c.Key, params, balance)
return balance, err
}

Expand All @@ -62,21 +43,10 @@ func List(params *stripe.BalanceTransactionListParams) *Iter {
return getC().List(params)
}

func (c Client) List(params *stripe.BalanceTransactionListParams) *Iter {
var body *form.Values
var lp *stripe.ListParams
var p *stripe.Params

if params != nil {
body = &form.Values{}
form.AppendTo(body, params)
lp = &params.ListParams
p = params.ToParams()
}

return &Iter{stripe.GetIter(lp, body, func(b *form.Values) ([]interface{}, stripe.ListMeta, error) {
func (c Client) List(listParams *stripe.BalanceTransactionListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.BalanceTransactionList{}
err := c.B.Call("GET", "/balance/history", c.Key, b, p, list)
err := c.B.CallRaw("GET", "/balance/history", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
Expand Down
Loading