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

Add support for the Review resource #730

Merged
merged 1 commit into from Nov 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -30,7 +30,7 @@ cache:
env:
global:
# If changing this number, please also change it in `testing/testing.go`.
- STRIPE_MOCK_VERSION=0.37.0
- STRIPE_MOCK_VERSION=0.38.0

go:
- "1.9"
Expand Down
37 changes: 31 additions & 6 deletions review.go
Expand Up @@ -15,15 +15,40 @@ const (
ReviewReasonRule ReviewReasonType = "rule"
)

// ReviewParams is the set of parameters that can be used when approving a review.
type ReviewParams struct {
Params `form:"*"`
}

// ReviewApproveParams is the set of parameters that can be used when approving a review.
type ReviewApproveParams struct {
Params `form:"*"`
}

// ReviewListParams is the set of parameters that can be used when listing reviews.
type ReviewListParams struct {
ListParams `form:"*"`
Created *int64 `form:"created"`
CreatedRange *RangeQueryParams `form:"created"`
}

// Review is the resource representing a Radar review.
// For more details see https://stripe.com/docs/api#reviews.
type Review struct {
Charge *Charge `json:"charge"`
Created int64 `json:"created"`
ID string `json:"id"`
Livemode bool `json:"livemode"`
Open bool `json:"open"`
Reason ReviewReasonType `json:"reason"`
Charge *Charge `json:"charge"`
Created int64 `json:"created"`
ID string `json:"id"`
Livemode bool `json:"livemode"`
Object string `json:"object"`
Open bool `json:"open"`
PaymentIntent *PaymentIntent `json:"payment_intent"`
Reason ReviewReasonType `json:"reason"`
}

// ReviewList is a list of reviews as retrieved from a list endpoint.
type ReviewList struct {
ListMeta
Data []*Review `json:"data"`
}

// UnmarshalJSON handles deserialization of a Review.
Expand Down
75 changes: 75 additions & 0 deletions review/client.go
@@ -0,0 +1,75 @@
// Package review provides the /reviews APIs
package review

import (
"net/http"

stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/form"
)

// Client is used to invoke /reviews APIs.
type Client struct {
B stripe.Backend
Key string
}

// Approve approves a review.
func Approve(id string, params *stripe.ReviewApproveParams) (*stripe.Review, error) {
return getC().Approve(id, params)
}

// Approve approves a review.
func (c Client) Approve(id string, params *stripe.ReviewApproveParams) (*stripe.Review, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for following convention with the empty params struct here! It feels a little funny to do, but I like that we're staying consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah and I do feel like we'll have params soon!

path := stripe.FormatURLPath("/reviews/%s/approve", id)
review := &stripe.Review{}
err := c.B.Call(http.MethodPost, path, c.Key, params, review)
return review, err
}

// Get returns the details of a review.
func Get(id string, params *stripe.ReviewParams) (*stripe.Review, error) {
return getC().Get(id, params)
}

// Get returns the details of a review.
func (c Client) Get(id string, params *stripe.ReviewParams) (*stripe.Review, error) {
path := stripe.FormatURLPath("/reviews/%s", id)
review := &stripe.Review{}
err := c.B.Call(http.MethodGet, path, c.Key, params, review)
return review, err
}

// List returns a list of reviews.
func List(params *stripe.ReviewListParams) *Iter {
return getC().List(params)
}

// List returns a list of reviews.
func (c Client) List(listParams *stripe.ReviewListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.ReviewList{}
err := c.B.CallRaw(http.MethodGet, "/reviews", c.Key, b, p, list)

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

return ret, list.ListMeta, err
})}
}

// Iter is an iterator for reviews.
type Iter struct {
*stripe.Iter
}

// Review returns the review which the iterator is currently pointing to.
func (i *Iter) Review() *stripe.Review {
return i.Current().(*stripe.Review)
}

func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}
30 changes: 30 additions & 0 deletions review/client_test.go
@@ -0,0 +1,30 @@
package review

import (
"testing"

assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go"
_ "github.com/stripe/stripe-go/testing"
)

func TestReviewApprove(t *testing.T) {
review, err := Approve("prv_123", &stripe.ReviewApproveParams{})
assert.Nil(t, err)
assert.NotNil(t, review)
}

func TestReviewGet(t *testing.T) {
review, err := Get("prv_123", nil)
assert.Nil(t, err)
assert.NotNil(t, review)
}

func TestReviewList(t *testing.T) {
i := List(&stripe.ReviewListParams{})

// Verify that we can get at least one review
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.Review())
}
2 changes: 1 addition & 1 deletion testing/testing.go
Expand Up @@ -25,7 +25,7 @@ const (
// added in a more recent version of stripe-mock, we can show people a
// better error message instead of the test suite crashing with a bunch of
// confusing 404 errors or the like.
MockMinimumVersion = "0.37.0"
MockMinimumVersion = "0.38.0"

// TestMerchantID is a token that can be used to represent a merchant ID in
// simple tests.
Expand Down