Skip to content

Commit

Permalink
Put /v1/ prefix as part of all paths instead of URL
Browse files Browse the repository at this point in the history
Traditionally, stripe-go has done something a little weird in that it
considered the `/v1/` in API URLs as part of the URL (e.g., `APIURL` or
`UploadURL`) instead of part of the paths of individual API calls. So
when calling `POST /v1/charges` we'd call `POST /charges` and rely on
the fact that `/v1/` was expected to be present in the URL.

This has always rubbed me the wrong way a little bit, and caused a
little bit of trouble in #733 as we'd been doing something a little
hacky when initializing new configs for backends.

This has some potential backwards compatibility problems in that people
may have been injecting custom URLs (e.g., if they had a testing setup a
little like we do for stripe-mock) that included the `/v1/` suffix. I've
tried to address this by trimming a `/v1/` suffix when creating a
backend with a configured URL. For example:

``` go
switch backendType {
case APIBackend:
        if config.URL == "" {
                config.URL = apiURL
        }

        // For a long time we had the `/v1` suffix as part of a configured URL
        // rather than in the per-package URLs throughout the library. Continue
        // to support this for the time being by stripping one that's been
        // passed for better backwards compatibility.
        config.URL = strings.TrimSuffix(config.URL, "/v1")
        config.URL = strings.TrimSuffix(config.URL, "/v1/")
```

I believe that will address the problem.

There's some risk that I missed a URL string in the project because
there are a lot of them. I tried to minimize the risk there by using a
tool-assisted project-wide find + replace, then manually examining every
string in the project that looks like a URL path (e.g., starts with
`"/`).
  • Loading branch information
brandur committed Nov 30, 2018
1 parent 310dd3b commit 8365214
Show file tree
Hide file tree
Showing 63 changed files with 303 additions and 260 deletions.
14 changes: 7 additions & 7 deletions account/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func New(params *stripe.AccountParams) (*stripe.Account, error) {
// New creates a new account.
func (c Client) New(params *stripe.AccountParams) (*stripe.Account, error) {
acct := &stripe.Account{}
err := c.B.Call(http.MethodPost, "/accounts", c.Key, params, acct)
err := c.B.Call(http.MethodPost, "/v1/accounts", c.Key, params, acct)
return acct, err
}

Expand All @@ -36,7 +36,7 @@ func Get() (*stripe.Account, error) {
// Get retrieves the authenticating account.
func (c Client) Get() (*stripe.Account, error) {
account := &stripe.Account{}
err := c.B.Call(http.MethodGet, "/account", c.Key, nil, account)
err := c.B.Call(http.MethodGet, "/v1/account", c.Key, nil, account)
return account, err
}

Expand All @@ -47,7 +47,7 @@ func GetByID(id string, params *stripe.AccountParams) (*stripe.Account, error) {

// GetByID retrieves an account.
func (c Client) GetByID(id string, params *stripe.AccountParams) (*stripe.Account, error) {
path := stripe.FormatURLPath("/accounts/%s", id)
path := stripe.FormatURLPath("/v1/accounts/%s", id)
account := &stripe.Account{}
err := c.B.Call(http.MethodGet, path, c.Key, params, account)
return account, err
Expand All @@ -60,7 +60,7 @@ func Update(id string, params *stripe.AccountParams) (*stripe.Account, error) {

// Update updates an account.
func (c Client) Update(id string, params *stripe.AccountParams) (*stripe.Account, error) {
path := stripe.FormatURLPath("/accounts/%s", id)
path := stripe.FormatURLPath("/v1/accounts/%s", id)
acct := &stripe.Account{}
err := c.B.Call(http.MethodPost, path, c.Key, params, acct)
return acct, err
Expand All @@ -73,7 +73,7 @@ func Del(id string, params *stripe.AccountParams) (*stripe.Account, error) {

// Del deletes an account.
func (c Client) Del(id string, params *stripe.AccountParams) (*stripe.Account, error) {
path := stripe.FormatURLPath("/accounts/%s", id)
path := stripe.FormatURLPath("/v1/accounts/%s", id)
acct := &stripe.Account{}
err := c.B.Call(http.MethodDelete, path, c.Key, params, acct)
return acct, err
Expand All @@ -86,7 +86,7 @@ func Reject(id string, params *stripe.AccountRejectParams) (*stripe.Account, err

// Reject rejects an account.
func (c Client) Reject(id string, params *stripe.AccountRejectParams) (*stripe.Account, error) {
path := stripe.FormatURLPath("/accounts/%s/reject", id)
path := stripe.FormatURLPath("/v1/accounts/%s/reject", id)
acct := &stripe.Account{}
err := c.B.Call(http.MethodPost, path, c.Key, params, acct)
return acct, err
Expand All @@ -101,7 +101,7 @@ func List(params *stripe.AccountListParams) *Iter {
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.CallRaw(http.MethodGet, "/accounts", c.Key, b, p, list)
err := c.B.CallRaw(http.MethodGet, "/v1/accounts", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
Expand Down
8 changes: 4 additions & 4 deletions applepaydomain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func New(params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain, error) {
// New creates a new Apple Pay domain.
func (c Client) New(params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain, error) {
domain := &stripe.ApplePayDomain{}
err := c.B.Call(http.MethodPost, "/apple_pay/domains", c.Key, params, domain)
err := c.B.Call(http.MethodPost, "/v1/apple_pay/domains", c.Key, params, domain)
return domain, err
}

Expand All @@ -33,7 +33,7 @@ func Get(id string, params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain

// Get retrieves an Apple Pay domain.
func (c Client) Get(id string, params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain, error) {
path := stripe.FormatURLPath("/apple_pay/domains/%s", id)
path := stripe.FormatURLPath("/v1/apple_pay/domains/%s", id)
domain := &stripe.ApplePayDomain{}
err := c.B.Call(http.MethodGet, path, c.Key, params, domain)
return domain, err
Expand All @@ -46,7 +46,7 @@ func Del(id string, params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain

// Del removes an Apple Pay domain.
func (c Client) Del(id string, params *stripe.ApplePayDomainParams) (*stripe.ApplePayDomain, error) {
path := stripe.FormatURLPath("/apple_pay/domains/%s", id)
path := stripe.FormatURLPath("/v1/apple_pay/domains/%s", id)
domain := &stripe.ApplePayDomain{}
err := c.B.Call(http.MethodDelete, path, c.Key, params, domain)
return domain, err
Expand All @@ -61,7 +61,7 @@ func List(params *stripe.ApplePayDomainListParams) *Iter {
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.CallRaw(http.MethodGet, "/apple_pay/domains", c.Key, b, p, list)
err := c.B.CallRaw(http.MethodGet, "/v1/apple_pay/domains", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
Expand Down
6 changes: 3 additions & 3 deletions balance/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Get(params *stripe.BalanceParams) (*stripe.Balance, error) {
// Get retrieves an account's balance
func (c Client) Get(params *stripe.BalanceParams) (*stripe.Balance, error) {
balance := &stripe.Balance{}
err := c.B.Call(http.MethodGet, "/balance", c.Key, params, balance)
err := c.B.Call(http.MethodGet, "/v1/balance", c.Key, params, balance)
return balance, err
}

Expand All @@ -33,7 +33,7 @@ func GetBalanceTransaction(id string, params *stripe.BalanceTransactionParams) (

// GetBalanceTransaction retrieves a balance transaction
func (c Client) GetBalanceTransaction(id string, params *stripe.BalanceTransactionParams) (*stripe.BalanceTransaction, error) {
path := stripe.FormatURLPath("/balance/history/%s", id)
path := stripe.FormatURLPath("/v1/balance/history/%s", id)
balance := &stripe.BalanceTransaction{}
err := c.B.Call(http.MethodGet, path, c.Key, params, balance)
return balance, err
Expand All @@ -48,7 +48,7 @@ func List(params *stripe.BalanceTransactionListParams) *Iter {
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.CallRaw(http.MethodGet, "/balance/history", c.Key, b, p, list)
err := c.B.CallRaw(http.MethodGet, "/v1/balance/history", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
Expand Down
20 changes: 10 additions & 10 deletions bankaccount/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func (c Client) New(params *stripe.BankAccountParams) (*stripe.BankAccount, erro

var path string
if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources", stripe.StringValue(params.Customer))
path = stripe.FormatURLPath("/v1/customers/%s/sources", stripe.StringValue(params.Customer))
} else if params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts", stripe.StringValue(params.Account))
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts", stripe.StringValue(params.Account))
} else {
return nil, errors.New("Invalid bank account params: either Customer or Account need to be set")
}
Expand Down Expand Up @@ -63,9 +63,9 @@ func (c Client) Get(id string, params *stripe.BankAccountParams) (*stripe.BankAc

var path string
if params != nil && params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
} else if params != nil && params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
} else {
return nil, errors.New("Invalid bank account params: either Customer or Account need to be set")
}
Expand All @@ -88,9 +88,9 @@ func (c Client) Update(id string, params *stripe.BankAccountParams) (*stripe.Ban

var path string
if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
} else if params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
} else {
return nil, errors.New("Invalid bank account params: either Customer or Account need to be set")
}
Expand All @@ -113,9 +113,9 @@ func (c Client) Del(id string, params *stripe.BankAccountParams) (*stripe.BankAc

var path string
if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
} else if params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
} else {
return nil, errors.New("Invalid bank account params: either Customer or Account need to be set")
}
Expand All @@ -142,10 +142,10 @@ func (c Client) List(listParams *stripe.BankAccountListParams) *Iter {
if listParams == nil {
outerErr = errors.New("params should not be nil")
} else if listParams.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources",
path = stripe.FormatURLPath("/v1/customers/%s/sources",
stripe.StringValue(listParams.Customer))
} else if listParams.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts",
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts",
stripe.StringValue(listParams.Account))
} else {
outerErr = errors.New("Invalid bank account params: either Customer or Account need to be set")
Expand Down
4 changes: 2 additions & 2 deletions bitcoinreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Get(id string) (*stripe.BitcoinReceiver, error) {

// Get returns the details of a bitcoin receiver.
func (c Client) Get(id string) (*stripe.BitcoinReceiver, error) {
path := stripe.FormatURLPath("/bitcoin/receivers/%s", id)
path := stripe.FormatURLPath("/v1/bitcoin/receivers/%s", id)
bitcoinReceiver := &stripe.BitcoinReceiver{}
err := c.B.Call(http.MethodGet, path, c.Key, nil, bitcoinReceiver)
return bitcoinReceiver, err
Expand All @@ -39,7 +39,7 @@ func List(params *stripe.BitcoinReceiverListParams) *Iter {
func (c Client) List(listParams *stripe.BitcoinReceiverListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.BitcoinReceiverList{}
err := c.B.CallRaw(http.MethodGet, "/bitcoin/receivers", c.Key, b, p, list)
err := c.B.CallRaw(http.MethodGet, "/v1/bitcoin/receivers", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
Expand Down
2 changes: 1 addition & 1 deletion bitcointransaction/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func List(params *stripe.BitcoinTransactionListParams) *Iter {
// List returns a list of bitcoin transactions.
func (c Client) List(listParams *stripe.BitcoinTransactionListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
path := stripe.FormatURLPath("/bitcoin/receivers/%s/transactions",
path := stripe.FormatURLPath("/v1/bitcoin/receivers/%s/transactions",
stripe.StringValue(listParams.Receiver))
list := &stripe.BitcoinTransactionList{}
err := c.B.CallRaw(http.MethodGet, path, c.Key, b, p, list)
Expand Down
30 changes: 15 additions & 15 deletions card/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func (c Client) New(params *stripe.CardParams) (*stripe.Card, error) {

var path string
if params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts",
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts",
stripe.StringValue(params.Account))
} else if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources",
path = stripe.FormatURLPath("/v1/customers/%s/sources",
stripe.StringValue(params.Customer))
} else if params.Recipient != nil {
path = stripe.FormatURLPath("/recipients/%s/cards",
path = stripe.FormatURLPath("/v1/recipients/%s/cards",
stripe.StringValue(params.Recipient))
} else {
return nil, errors.New("Invalid card params: either account, customer or recipient need to be set")
Expand Down Expand Up @@ -68,13 +68,13 @@ func (c Client) Get(id string, params *stripe.CardParams) (*stripe.Card, error)

var path string
if params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts/%s",
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s",
stripe.StringValue(params.Account), id)
} else if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s",
path = stripe.FormatURLPath("/v1/customers/%s/sources/%s",
stripe.StringValue(params.Customer), id)
} else if params.Recipient != nil {
path = stripe.FormatURLPath("/recipients/%s/cards/%s",
path = stripe.FormatURLPath("/v1/recipients/%s/cards/%s",
stripe.StringValue(params.Recipient), id)
} else {
return nil, errors.New("Invalid card params: either account, customer or recipient need to be set")
Expand All @@ -98,13 +98,13 @@ func (c Client) Update(id string, params *stripe.CardParams) (*stripe.Card, erro

var path string
if params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts/%s",
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s",
stripe.StringValue(params.Account), id)
} else if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s",
path = stripe.FormatURLPath("/v1/customers/%s/sources/%s",
stripe.StringValue(params.Customer), id)
} else if params.Recipient != nil {
path = stripe.FormatURLPath("/recipients/%s/cards/%s",
path = stripe.FormatURLPath("/v1/recipients/%s/cards/%s",
stripe.StringValue(params.Recipient), id)
} else {
return nil, errors.New("Invalid card params: either account, customer or recipient need to be set")
Expand All @@ -128,11 +128,11 @@ func (c Client) Del(id string, params *stripe.CardParams) (*stripe.Card, error)

var path string
if params.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
} else if params.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
} else if params.Recipient != nil {
path = stripe.FormatURLPath("/recipients/%s/cards/%s", stripe.StringValue(params.Recipient), id)
path = stripe.FormatURLPath("/v1/recipients/%s/cards/%s", stripe.StringValue(params.Recipient), id)
} else {
return nil, errors.New("Invalid card params: either account, customer or recipient need to be set")
}
Expand All @@ -158,13 +158,13 @@ func (c Client) List(listParams *stripe.CardListParams) *Iter {
if listParams == nil {
outerErr = errors.New("params should not be nil")
} else if listParams.Account != nil {
path = stripe.FormatURLPath("/accounts/%s/external_accounts",
path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts",
stripe.StringValue(listParams.Account))
} else if listParams.Customer != nil {
path = stripe.FormatURLPath("/customers/%s/sources",
path = stripe.FormatURLPath("/v1/customers/%s/sources",
stripe.StringValue(listParams.Customer))
} else if listParams.Recipient != nil {
path = stripe.FormatURLPath("/recipients/%s/cards", stripe.StringValue(listParams.Recipient))
path = stripe.FormatURLPath("/v1/recipients/%s/cards", stripe.StringValue(listParams.Recipient))
} else {
outerErr = errors.New("Invalid card params: either account, customer or recipient need to be set")
}
Expand Down
10 changes: 5 additions & 5 deletions charge/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func New(params *stripe.ChargeParams) (*stripe.Charge, error) {
// New creates a new charge.
func (c Client) New(params *stripe.ChargeParams) (*stripe.Charge, error) {
charge := &stripe.Charge{}
err := c.B.Call(http.MethodPost, "/charges", c.Key, params, charge)
err := c.B.Call(http.MethodPost, "/v1/charges", c.Key, params, charge)
return charge, err
}

Expand All @@ -35,7 +35,7 @@ func Get(id string, params *stripe.ChargeParams) (*stripe.Charge, error) {

// Get retrieves a charge.
func (c Client) Get(id string, params *stripe.ChargeParams) (*stripe.Charge, error) {
path := stripe.FormatURLPath("/charges/%s", id)
path := stripe.FormatURLPath("/v1/charges/%s", id)
charge := &stripe.Charge{}
err := c.B.Call(http.MethodGet, path, c.Key, params, charge)
return charge, err
Expand All @@ -48,7 +48,7 @@ func Update(id string, params *stripe.ChargeParams) (*stripe.Charge, error) {

// Update updates a charge.
func (c Client) Update(id string, params *stripe.ChargeParams) (*stripe.Charge, error) {
path := stripe.FormatURLPath("/charges/%s", id)
path := stripe.FormatURLPath("/v1/charges/%s", id)
charge := &stripe.Charge{}
err := c.B.Call(http.MethodPost, path, c.Key, params, charge)
return charge, err
Expand All @@ -61,7 +61,7 @@ func Capture(id string, params *stripe.CaptureParams) (*stripe.Charge, error) {

// Capture captures a charge that's not yet captured.
func (c Client) Capture(id string, params *stripe.CaptureParams) (*stripe.Charge, error) {
path := stripe.FormatURLPath("/charges/%s/capture", id)
path := stripe.FormatURLPath("/v1/charges/%s/capture", id)
charge := &stripe.Charge{}
err := c.B.Call(http.MethodPost, path, c.Key, params, charge)
return charge, err
Expand All @@ -76,7 +76,7 @@ func List(params *stripe.ChargeListParams) *Iter {
func (c Client) List(listParams *stripe.ChargeListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.ChargeList{}
err := c.B.CallRaw(http.MethodGet, "/charges", c.Key, b, p, list)
err := c.B.CallRaw(http.MethodGet, "/v1/charges", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
Expand Down
4 changes: 2 additions & 2 deletions countryspec/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Get(country string, params *stripe.CountrySpecParams) (*stripe.CountrySpec,

// Get returns a Country Spec for a given country code.
func (c Client) Get(country string, params *stripe.CountrySpecParams) (*stripe.CountrySpec, error) {
path := stripe.FormatURLPath("/country_specs/%s", country)
path := stripe.FormatURLPath("/v1/country_specs/%s", country)
countrySpec := &stripe.CountrySpec{}
err := c.B.Call(http.MethodGet, path, c.Key, params, countrySpec)
return countrySpec, err
Expand All @@ -36,7 +36,7 @@ func List(params *stripe.CountrySpecListParams) *Iter {
func (c Client) List(listParams *stripe.CountrySpecListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.CountrySpecList{}
err := c.B.CallRaw(http.MethodGet, "/country_specs", c.Key, b, p, list)
err := c.B.CallRaw(http.MethodGet, "/v1/country_specs", c.Key, b, p, list)

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

0 comments on commit 8365214

Please sign in to comment.