Skip to content

Commit df9e8e5

Browse files
mbroshi-striperamya-stripe
authored andcommitted
Add usage data for stripe client usage (#1982)
* Add some client tests * Add usageBackend * Move UsageBackend to stripe package * Update usage string to stripe_client
1 parent 23aad4d commit df9e8e5

File tree

6 files changed

+84
-26
lines changed

6 files changed

+84
-26
lines changed

account/client_test.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1-
package account
1+
package account_test
22

33
import (
44
"testing"
55

66
assert "github.com/stretchr/testify/require"
77
stripe "github.com/stripe/stripe-go/v81"
8-
_ "github.com/stripe/stripe-go/v81/testing"
8+
"github.com/stripe/stripe-go/v81/client"
9+
. "github.com/stripe/stripe-go/v81/testing"
910
)
1011

1112
func TestAccountDel(t *testing.T) {
12-
account, err := Del("acct_123", nil)
13+
sc := client.New(TestAPIKey, nil)
14+
account, err := sc.Accounts.Del("acct_123", nil)
1315
assert.Nil(t, err)
1416
assert.NotNil(t, account)
1517
}
1618

1719
func TestAccountGet(t *testing.T) {
18-
account, err := Get()
20+
sc := client.New(TestAPIKey, nil)
21+
account, err := sc.Accounts.Get()
1922
assert.Nil(t, err)
2023
assert.NotNil(t, account)
2124
}
2225

2326
func TestAccountGetByID(t *testing.T) {
24-
account, err := GetByID("acct_123", nil)
27+
sc := client.New(TestAPIKey, nil)
28+
account, err := sc.Accounts.GetByID("acct_123", nil)
2529
assert.Nil(t, err)
2630
assert.NotNil(t, account)
2731
}
2832

2933
func TestAccountList(t *testing.T) {
30-
i := List(&stripe.AccountListParams{})
34+
sc := client.New(TestAPIKey, nil)
35+
i := sc.Accounts.List(&stripe.AccountListParams{})
3136

3237
// Verify that we can get at least one account
3338
assert.True(t, i.Next())
@@ -37,7 +42,8 @@ func TestAccountList(t *testing.T) {
3742
}
3843

3944
func TestAccountNew(t *testing.T) {
40-
account, err := New(&stripe.AccountParams{
45+
sc := client.New(TestAPIKey, nil)
46+
account, err := sc.Accounts.New(&stripe.AccountParams{
4147
BusinessProfile: &stripe.AccountBusinessProfileParams{
4248
Name: stripe.String("name"),
4349
SupportEmail: stripe.String("[email protected]"),
@@ -103,15 +109,17 @@ func TestAccountNew(t *testing.T) {
103109
}
104110

105111
func TestAccountReject(t *testing.T) {
106-
account, err := Reject("acct_123", &stripe.AccountRejectParams{
112+
sc := client.New(TestAPIKey, nil)
113+
account, err := sc.Accounts.Reject("acct_123", &stripe.AccountRejectParams{
107114
Reason: stripe.String("fraud"),
108115
})
109116
assert.Nil(t, err)
110117
assert.NotNil(t, account)
111118
}
112119

113120
func TestAccountUpdate(t *testing.T) {
114-
account, err := Update("acct_123", &stripe.AccountParams{
121+
sc := client.New(TestAPIKey, nil)
122+
account, err := sc.Accounts.Update("acct_123", &stripe.AccountParams{
115123
Company: &stripe.AccountCompanyParams{
116124
Address: &stripe.AddressParams{
117125
Country: stripe.String("CA"),

charge/client_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
package charge
1+
package charge_test
22

33
import (
44
"testing"
55

66
assert "github.com/stretchr/testify/require"
77
stripe "github.com/stripe/stripe-go/v81"
8-
_ "github.com/stripe/stripe-go/v81/testing"
8+
"github.com/stripe/stripe-go/v81/client"
9+
. "github.com/stripe/stripe-go/v81/testing"
910
)
1011

1112
func TestChargeCapture(t *testing.T) {
12-
charge, err := Capture("ch_123", &stripe.ChargeCaptureParams{
13+
sc := client.New(TestAPIKey, nil)
14+
charge, err := sc.Charges.Capture("ch_123", &stripe.ChargeCaptureParams{
1315
Amount: stripe.Int64(123),
1416
})
1517
assert.Nil(t, err)
1618
assert.NotNil(t, charge)
1719
}
1820

1921
func TestChargeGet(t *testing.T) {
20-
charge, err := Get("ch_123", nil)
22+
sc := client.New(TestAPIKey, nil)
23+
charge, err := sc.Charges.Get("ch_123", nil)
2124
assert.Nil(t, err)
2225
assert.NotNil(t, charge)
2326
}
2427

2528
func TestChargeList(t *testing.T) {
26-
i := List(&stripe.ChargeListParams{})
29+
sc := client.New(TestAPIKey, nil)
30+
i := sc.Charges.List(&stripe.ChargeListParams{})
2731

2832
// Verify that we can get at least one charge
2933
assert.True(t, i.Next())
@@ -33,7 +37,8 @@ func TestChargeList(t *testing.T) {
3337
}
3438

3539
func TestChargeSearch(t *testing.T) {
36-
i := Search(&stripe.ChargeSearchParams{SearchParams: stripe.SearchParams{
40+
sc := client.New(TestAPIKey, nil)
41+
i := sc.Charges.Search(&stripe.ChargeSearchParams{SearchParams: stripe.SearchParams{
3742
Query: "currency:\"USD\"",
3843
}})
3944

@@ -46,7 +51,8 @@ func TestChargeSearch(t *testing.T) {
4651
}
4752

4853
func TestChargeNew(t *testing.T) {
49-
charge, err := New(&stripe.ChargeParams{
54+
sc := client.New(TestAPIKey, nil)
55+
charge, err := sc.Charges.New(&stripe.ChargeParams{
5056
Amount: stripe.Int64(11700),
5157
Currency: stripe.String(string(stripe.CurrencyUSD)),
5258
Source: &stripe.PaymentSourceSourceParams{Token: stripe.String("src_123")},
@@ -64,7 +70,8 @@ func TestChargeNew(t *testing.T) {
6470
}
6571

6672
func TestChargeUpdate(t *testing.T) {
67-
charge, err := Update("ch_123", &stripe.ChargeParams{
73+
sc := client.New(TestAPIKey, nil)
74+
charge, err := sc.Charges.Update("ch_123", &stripe.ChargeParams{
6875
Description: stripe.String("Updated description"),
6976
})
7077
assert.Nil(t, err)

coupon/client_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
package coupon
1+
package coupon_test
22

33
import (
44
"testing"
55

66
assert "github.com/stretchr/testify/require"
77
stripe "github.com/stripe/stripe-go/v81"
8-
_ "github.com/stripe/stripe-go/v81/testing"
8+
"github.com/stripe/stripe-go/v81/client"
9+
. "github.com/stripe/stripe-go/v81/testing"
910
)
1011

1112
func TestCouponDel(t *testing.T) {
12-
coupon, err := Del("25OFF", nil)
13+
sc := client.New(TestAPIKey, nil)
14+
coupon, err := sc.Coupons.Del("25OFF", nil)
1315
assert.Nil(t, err)
1416
assert.NotNil(t, coupon)
1517
}
1618

1719
func TestCouponGet(t *testing.T) {
18-
coupon, err := Get("25OFF", nil)
20+
sc := client.New(TestAPIKey, nil)
21+
coupon, err := sc.Coupons.Get("25OFF", nil)
1922
assert.Nil(t, err)
2023
assert.NotNil(t, coupon)
2124
}
2225

2326
func TestCouponList(t *testing.T) {
24-
i := List(&stripe.CouponListParams{})
27+
sc := client.New(TestAPIKey, nil)
28+
i := sc.Coupons.List(&stripe.CouponListParams{})
2529

2630
// Verify that we can get at least one coupon
2731
assert.True(t, i.Next())
@@ -31,7 +35,8 @@ func TestCouponList(t *testing.T) {
3135
}
3236

3337
func TestCouponNew(t *testing.T) {
34-
coupon, err := New(&stripe.CouponParams{
38+
sc := client.New(TestAPIKey, nil)
39+
coupon, err := sc.Coupons.New(&stripe.CouponParams{
3540
AppliesTo: &stripe.CouponAppliesToParams{
3641
Products: stripe.StringSlice([]string{
3742
"prod_123",
@@ -49,7 +54,8 @@ func TestCouponNew(t *testing.T) {
4954
}
5055

5156
func TestCouponUpdate(t *testing.T) {
52-
coupon, err := Update("25OFF", &stripe.CouponParams{
57+
sc := client.New(TestAPIKey, nil)
58+
coupon, err := sc.Coupons.Update("25OFF", &stripe.CouponParams{
5359
Params: stripe.Params{
5460
Metadata: map[string]string{
5561
"foo": "bar",

params.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (p *Params) AddExpand(f string) {
220220
// InternalSetUsage sets the usage field on the Params struct.
221221
// Unstable: for internal stripe-go usage only.
222222
func (p *Params) InternalSetUsage(usage []string) {
223-
p.usage = usage
223+
p.usage = append(p.usage, usage...)
224224
}
225225

226226
// AddExtra adds a new arbitrary key-value pair to the request data

stripe.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,3 +1619,37 @@ func RawRequest(method, path string, content string, params *RawParams) (*APIRes
16191619
}
16201620
return nil, fmt.Errorf("Error: cannot call RawRequest if backends.API is initialized with a backend that doesn't implement RawRequestBackend")
16211621
}
1622+
1623+
// UsageBackend is a wrapper for stripe.Backend that sets the usage parameter
1624+
type UsageBackend struct {
1625+
B Backend
1626+
Usage []string
1627+
}
1628+
1629+
func (u *UsageBackend) Call(method, path, key string, params ParamsContainer, v LastResponseSetter) error {
1630+
if r := reflect.ValueOf(params); r.Kind() == reflect.Ptr && !r.IsNil() {
1631+
params.GetParams().InternalSetUsage(u.Usage)
1632+
}
1633+
return u.B.Call(method, path, key, params, v)
1634+
}
1635+
1636+
func (u *UsageBackend) CallRaw(method, path, key string, body *form.Values, params *Params, v LastResponseSetter) error {
1637+
params.GetParams().InternalSetUsage(u.Usage)
1638+
return u.B.CallRaw(method, path, key, body, params, v)
1639+
}
1640+
1641+
func (u *UsageBackend) CallMultipart(method, path, key, boundary string, body *bytes.Buffer, params *Params, v LastResponseSetter) error {
1642+
params.GetParams().InternalSetUsage(u.Usage)
1643+
return u.B.CallMultipart(method, path, key, boundary, body, params, v)
1644+
}
1645+
1646+
func (u *UsageBackend) CallStreaming(method, path, key string, params ParamsContainer, v StreamingLastResponseSetter) error {
1647+
if r := reflect.ValueOf(params); r.Kind() == reflect.Ptr && !r.IsNil() {
1648+
params.GetParams().InternalSetUsage(u.Usage)
1649+
}
1650+
return u.B.CallStreaming(method, path, key, params, v)
1651+
}
1652+
1653+
func (u *UsageBackend) SetMaxNetworkRetries(maxNetworkRetries int64) {
1654+
u.B.SetMaxNetworkRetries(maxNetworkRetries)
1655+
}

testing/testing.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const (
3030
// TestMerchantID is a token that can be used to represent a merchant ID in
3131
// simple tests.
3232
TestMerchantID = "acct_123"
33+
34+
// TestAPIKey is an API key that can be used against stripe-mock in tests.
35+
TestAPIKey = "sk_test_myTestKey"
3336
)
3437

3538
func init() {
@@ -82,7 +85,7 @@ func init() {
8285
os.Exit(1)
8386
}
8487

85-
stripe.Key = "sk_test_myTestKey"
88+
stripe.Key = TestAPIKey
8689

8790
// Configure a backend for stripe-mock and set it for both the API and
8891
// Uploads (unlike the real Stripe API, stripe-mock supports both these

0 commit comments

Comments
 (0)