Skip to content

Commit

Permalink
Convert Params and ListParams fields to pointers as well for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
brandur authored and remi-stripe committed Jun 6, 2018
1 parent 537b35b commit 22d1bb9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 39 deletions.
12 changes: 6 additions & 6 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func GetIter(params *ListParams, qs *form.Values, query Query) *Iter {

func (it *Iter) getPage() {
it.values, it.meta, it.err = it.query(it.qs)
if it.params.EndingBefore != "" {
if it.params.EndingBefore != nil {
// We are moving backward,
// but items arrive in forward order.
reverse(it.values)
Expand All @@ -65,12 +65,12 @@ func (it *Iter) getPage() {
func (it *Iter) Next() bool {
if len(it.values) == 0 && it.meta.HasMore && !it.params.Single {
// determine if we're moving forward or backwards in paging
if it.params.EndingBefore != "" {
it.params.EndingBefore = listItemID(it.cur)
it.qs.Set(EndingBefore, it.params.EndingBefore)
if it.params.EndingBefore != nil {
it.params.EndingBefore = String(listItemID(it.cur))
it.qs.Set(EndingBefore, *it.params.EndingBefore)
} else {
it.params.StartingAfter = listItemID(it.cur)
it.qs.Set(StartingAfter, it.params.StartingAfter)
it.params.StartingAfter = String(listItemID(it.cur))
it.qs.Set(StartingAfter, *it.params.StartingAfter)
}
it.getPage()
}
Expand Down
4 changes: 2 additions & 2 deletions iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestIterTwoPagesErr(t *testing.T) {
func TestIterReversed(t *testing.T) {
tq := testQuery{{[]interface{}{1, 2}, ListMeta{}, nil}}
want := []interface{}{2, 1}
g, gerr := collect(GetIter(&ListParams{EndingBefore: "x"}, nil, tq.query))
g, gerr := collect(GetIter(&ListParams{EndingBefore: String("x")}, nil, tq.query))
assert.Equal(t, 0, len(tq))
assert.Equal(t, want, g)
assert.NoError(t, gerr)
Expand All @@ -105,7 +105,7 @@ func TestIterReversedTwoPages(t *testing.T) {
{[]interface{}{1, 2}, ListMeta{}, nil},
}
want := []interface{}{4, &item{"3"}, 2, 1}
g, gerr := collect(GetIter(&ListParams{EndingBefore: "x"}, nil, tq.query))
g, gerr := collect(GetIter(&ListParams{EndingBefore: String("x")}, nil, tq.query))
assert.Equal(t, 0, len(tq))
assert.Equal(t, want, g)
assert.NoError(t, gerr)
Expand Down
3 changes: 2 additions & 1 deletion order/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func TestOrderReturn_RequestParams(t *testing.T) {
})

p := &stripe.OrderReturnParams{}
p.StripeAccount = "acct_123"
p.SetStripeAccount("acct_123")

order, err := Return("or_123", p)
assert.Nil(t, err)
assert.NotNil(t, order)
Expand Down
29 changes: 17 additions & 12 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ type Params struct {
// key or query the state of the API.
Context context.Context `form:"-"`

Expand []string `form:"expand"`
Expand []*string `form:"expand"`
Extra *ExtraValues `form:"*"`

// Headers may be used to provide extra header lines on the HTTP request.
Headers http.Header `form:"-"`

IdempotencyKey string `form:"-"` // Passed as header
IdempotencyKey *string `form:"-"` // Passed as header
Metadata map[string]string `form:"metadata"`

// StripeAccount may contain the ID of a connected account. By including
// this field, the request is made as if it originated from the connected
// account instead of under the account of the owner of the configured
// Stripe key.
StripeAccount string `form:"-"` // Passed as header
StripeAccount *string `form:"-"` // Passed as header
}

// ExtraValues are extra parameters that are attached to an API request.
Expand Down Expand Up @@ -75,24 +75,24 @@ type ListParams struct {
// key or query the state of the API.
Context context.Context `form:"-"`

EndingBefore string `form:"ending_before"`
Expand []string `form:"expand"`
Filters Filters `form:"*"`
Limit int64 `form:"limit"`
EndingBefore *string `form:"ending_before"`
Expand []*string `form:"expand"`
Filters Filters `form:"*"`
Limit *int64 `form:"limit"`

// Single specifies whether this is a single page iterator. By default,
// listing through an iterator will automatically grab additional pages as
// the query progresses. To change this behavior and just load a single
// page, set this to true.
Single bool `form:"-"` // Not an API parameter

StartingAfter string `form:"starting_after"`
StartingAfter *string `form:"starting_after"`

// StripeAccount may contain the ID of a connected account. By including
// this field, the request is made as if it originated from the connected
// account instead of under the account of the owner of the configured
// Stripe key.
StripeAccount string `form:"-"` // Passed as header
StripeAccount *string `form:"-"` // Passed as header
}

// ListMeta is the structure that contains the common properties
Expand Down Expand Up @@ -162,14 +162,19 @@ func NewIdempotencyKey() string {
return fmt.Sprintf("%v_%v", now, base64.URLEncoding.EncodeToString(buf)[:6])
}

// SetIdempotencyKey sets a value for the Idempotency-Key header.
func (p *Params) SetIdempotencyKey(val string) {
p.IdempotencyKey = &val
}

// SetStripeAccount sets a value for the Stripe-Account header.
func (p *Params) SetStripeAccount(val string) {
p.StripeAccount = val
p.StripeAccount = &val
}

// AddExpand appends a new field to expand.
func (p *Params) AddExpand(f string) {
p.Expand = append(p.Expand, f)
p.Expand = append(p.Expand, &f)
}

// AddMetadata adds a new key-value pair to the Metadata.
Expand All @@ -192,7 +197,7 @@ func (p *Params) AddExtra(key, value string) {

// AddExpand appends a new field to expand.
func (p *ListParams) AddExpand(f string) {
p.Expand = append(p.Expand, f)
p.Expand = append(p.Expand, &f)
}

// ToParams converts a ListParams to a Params by moving over any fields that
Expand Down
21 changes: 12 additions & 9 deletions params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func TestListParams_Nested(t *testing.T) {
params := &testListParams{
Field: "field_value",
ListParams: stripe.ListParams{
EndingBefore: "acct_123",
Limit: 100,
StartingAfter: "acct_123",
EndingBefore: stripe.String("acct_123"),
Limit: stripe.Int64(100),
StartingAfter: stripe.String("acct_123"),
},
}

Expand Down Expand Up @@ -216,20 +216,23 @@ func TestListParams_Expand(t *testing.T) {
func TestListParams_ToParams(t *testing.T) {
listParams := &stripe.ListParams{
Context: context.Background(),
StripeAccount: TestMerchantID,
StripeAccount: stripe.String(TestMerchantID),
}
params := listParams.ToParams()
assert.Equal(t, listParams.Context, params.Context)
assert.Equal(t, listParams.StripeAccount, params.StripeAccount)
assert.Equal(t, *listParams.StripeAccount, *params.StripeAccount)
}

func TestParams_SetIdempotencyKey(t *testing.T) {
p := &stripe.Params{}
p.SetIdempotencyKey("my-idempotency-key")
assert.Equal(t, "my-idempotency-key", *p.IdempotencyKey)
}

func TestParams_SetStripeAccount(t *testing.T) {
p := &stripe.Params{}
p.SetStripeAccount(TestMerchantID)

if p.StripeAccount != TestMerchantID {
t.Fatalf("Expected Account of %v but got %v.", TestMerchantID, p.StripeAccount)
}
assert.Equal(t, TestMerchantID, *p.StripeAccount)
}

//
Expand Down
15 changes: 8 additions & 7 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,18 @@ func (s *BackendConfiguration) NewRequest(method, path, key, contentType string,
req = req.WithContext(params.Context)
}

if idempotency := strings.TrimSpace(params.IdempotencyKey); idempotency != "" {
if len(idempotency) > 255 {
return nil, errors.New("Cannot use an IdempotencyKey longer than 255 characters long.")
if params.IdempotencyKey != nil {
idempotencyKey := strings.TrimSpace(*params.IdempotencyKey)

if len(idempotencyKey) > 255 {
return nil, errors.New("Cannot use an idempotency key longer than 255 characters.")
}

req.Header.Add("Idempotency-Key", idempotency)
req.Header.Add("Idempotency-Key", idempotencyKey)
}

// But prefer StripeAccount.
if stripeAccount := strings.TrimSpace(params.StripeAccount); stripeAccount != "" {
req.Header.Add("Stripe-Account", stripeAccount)
if params.StripeAccount != nil {
req.Header.Add("Stripe-Account", strings.TrimSpace(*params.StripeAccount))
}

for k, v := range params.Headers {
Expand Down
4 changes: 2 additions & 2 deletions stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestMultipleAPICalls(t *testing.T) {

func TestIdempotencyKey(t *testing.T) {
c := &stripe.BackendConfiguration{URL: stripe.APIURL}
p := &stripe.Params{IdempotencyKey: "idempotency-key"}
p := &stripe.Params{IdempotencyKey: stripe.String("idempotency-key")}

req, err := c.NewRequest("", "", "", "", nil, p)
assert.NoError(t, err)
Expand All @@ -99,7 +99,7 @@ func TestIdempotencyKey(t *testing.T) {

func TestStripeAccount(t *testing.T) {
c := &stripe.BackendConfiguration{URL: stripe.APIURL}
p := &stripe.Params{StripeAccount: TestMerchantID}
p := &stripe.Params{StripeAccount: stripe.String(TestMerchantID)}

req, err := c.NewRequest("", "", "", "", nil, p)
assert.NoError(t, err)
Expand Down

0 comments on commit 22d1bb9

Please sign in to comment.