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 Reporting resources #690

Merged
merged 1 commit into from
Sep 5, 2018
Merged
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
8 changes: 8 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
@@ -41,6 +41,8 @@ import (
"github.com/stripe/stripe-go/product"
"github.com/stripe/stripe-go/recipient"
"github.com/stripe/stripe-go/refund"
"github.com/stripe/stripe-go/reporting/reportrun"
"github.com/stripe/stripe-go/reporting/reporttype"
"github.com/stripe/stripe-go/reversal"
"github.com/stripe/stripe-go/sigma/scheduledqueryrun"
"github.com/stripe/stripe-go/sku"
@@ -132,6 +134,10 @@ type API struct {
Recipients *recipient.Client
// Refunds is the client used to invoke /refunds APIs.
Refunds *refund.Client
// ReportRuns is the client used to invoke /reporting/report_runs APIs.
ReportRuns *reportrun.Client
// ReportTypes is the client used to invoke /reporting/report_types APIs.
ReportTypes *reporttype.Client
// Reversals is the client used to invoke /transfers/reversals APIs.
Reversals *reversal.Client
// SigmaScheduledQueryRuns is the client used to invoke /sigma/scheduled_query_runs APIs.
@@ -204,6 +210,8 @@ func (a *API) Init(key string, backends *stripe.Backends) {
a.Products = &product.Client{B: backends.API, Key: key}
a.Recipients = &recipient.Client{B: backends.API, Key: key}
a.Refunds = &refund.Client{B: backends.API, Key: key}
a.ReportRuns = &reportrun.Client{B: backends.API, Key: key}
a.ReportTypes = &reporttype.Client{B: backends.API, Key: key}
a.Reversals = &reversal.Client{B: backends.API, Key: key}
a.SigmaScheduledQueryRuns = &scheduledqueryrun.Client{B: backends.API, Key: key}
a.Skus = &sku.Client{B: backends.API, Key: key}
76 changes: 76 additions & 0 deletions reporting/reportrun/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Package reportrun provides API functions related to report runs.
//
// For more details, see: https://stripe.com/docs/api/go#reporting_report_run
package reportrun

import (
"net/http"

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

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

// New creates a new report run.
func New(params *stripe.ReportRunParams) (*stripe.ReportRun, error) {
return getC().New(params)
}

// New creates a new report run.
func (c Client) New(params *stripe.ReportRunParams) (*stripe.ReportRun, error) {
reportrun := &stripe.ReportRun{}
err := c.B.Call(http.MethodPost, "/reporting/report_runs", c.Key, params, reportrun)
return reportrun, err
}

// Get returns the details of a report run.
func Get(id string, params *stripe.ReportRunParams) (*stripe.ReportRun, error) {
return getC().Get(id, params)
}

// Get returns the details of a report run.
func (c Client) Get(id string, params *stripe.ReportRunParams) (*stripe.ReportRun, error) {
path := stripe.FormatURLPath("/reporting/report_runs/%s", id)
reportrun := &stripe.ReportRun{}
err := c.B.Call(http.MethodGet, path, c.Key, params, reportrun)
return reportrun, err
}

// List returns a list of report runs.
func List(params *stripe.ReportRunListParams) *Iter {
return getC().List(params)
}

// List returns a list of report runs.
func (c Client) List(listParams *stripe.ReportRunListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.ReportRunList{}
err := c.B.CallRaw(http.MethodGet, "/reporting/report_runs", 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 report runs.
type Iter struct {
*stripe.Iter
}

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

func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}
38 changes: 38 additions & 0 deletions reporting/reportrun/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package reportrun

import (
"testing"

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

func TestReportRunGet(t *testing.T) {
reportrun, err := Get("frr_123", nil)
assert.Nil(t, err)
assert.NotNil(t, reportrun)
assert.Equal(t, "reporting.report_run", reportrun.Object)
}

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

// Verify that we can get at least one reportrun
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.ReportRun())
assert.Equal(t, "reporting.report_run", i.ReportRun().Object)
}

func TestReportRunNew(t *testing.T) {
reportrun, err := New(&stripe.ReportRunParams{
Parameters: &stripe.ReportRunParametersParams{
ConnectedAccount: stripe.String("acct_123"),
},
ReportType: stripe.String("activity.summary.1"),
})
assert.Nil(t, err)
assert.NotNil(t, reportrun)
assert.Equal(t, "reporting.report_run", reportrun.Object)
}
64 changes: 64 additions & 0 deletions reporting/reporttype/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Package reporttype provides API functions related to report types.
//
// For more details, see: https://stripe.com/docs/api/go#reporting_report_type
package reporttype

import (
"net/http"

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

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

// Get returns the details of a report type.
func Get(id string, params *stripe.ReportTypeParams) (*stripe.ReportType, error) {
return getC().Get(id, params)
}

// Get returns the details of a report type.
func (c Client) Get(id string, params *stripe.ReportTypeParams) (*stripe.ReportType, error) {
path := stripe.FormatURLPath("/reporting/report_types/%s", id)
reporttype := &stripe.ReportType{}
err := c.B.Call(http.MethodGet, path, c.Key, params, reporttype)
return reporttype, err
}

// List returns a list of report types.
func List(params *stripe.ReportTypeListParams) *Iter {
return getC().List(params)
}

// List returns a list of report types.
func (c Client) List(listParams *stripe.ReportTypeListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.ReportTypeList{}
err := c.B.CallRaw(http.MethodGet, "/reporting/report_types", 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 report types.
type Iter struct {
*stripe.Iter
}

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

func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}
26 changes: 26 additions & 0 deletions reporting/reporttype/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package reporttype

import (
"testing"

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

func TestReportTestGet(t *testing.T) {
reporttype, err := Get("activity.summary.1", nil)
assert.Nil(t, err)
assert.NotNil(t, reporttype)
assert.Equal(t, "reporting.report_type", reporttype.Object)
}

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

// Verify that we can get at least one reporttype
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.ReportType())
assert.Equal(t, "reporting.report_type", i.ReportType().Object)
}
65 changes: 65 additions & 0 deletions reporting_reportrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package stripe

// ReportRunStatus is the possible values for status on a report run.
type ReportRunStatus string

// List of values that ReportRunStatus can take.
const (
ReportRunStatusFailed ReportRunStatus = "failed"
ReportRunStatusPending ReportRunStatus = "pending"
ReportRunStatusSucceeded ReportRunStatus = "succeeded"
)

// ReportRunParametersParams is the set of parameters that can be used when creating a report run.
type ReportRunParametersParams struct {
ConnectedAccount *string `form:"connected_account"`
Currency *string `form:"currency"`
IntervalEnd *int64 `form:"interval_end"`
IntervalStart *int64 `form:"interval_start"`
Payout *string `form:"payout"`
ReportingCategory *string `form:"reporting_category"`
}

// ReportRunParams is the set of parameters that can be used when creating a report run.
type ReportRunParams struct {
Params `form:"*"`
Parameters *ReportRunParametersParams `form:"parameters"`
ReportType *string `form:"report_type"`
}

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

// ReportRunParameters describes the parameters hash on a report run.
type ReportRunParameters struct {
ConnectedAccount string `json:"connected_account"`
Currency Currency `json:"currency"`
IntervalEnd int64 `json:"interval_end"`
IntervalStart int64 `json:"interval_start"`
Payout string `json:"payout"`
ReportingCategory string `json:"reporting_category"`
}

// ReportRun is the resource representing a report run.
type ReportRun struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I decided not to prefix everything with Reporting in this case because it felt redundant and unlikely we'd have a conflict in the future.

Let me know if you think it's cleaner to keep the prefix though like we do for Issuing

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, makes sense in this situation.

Created int64 `json:"created"`
Error string `json:"error"`
ID string `json:"id"`
Livemode bool `json:"livemode"`
Object string `json:"object"`
Parameters *ReportRunParameters `json:"parameters"`
ReportType string `json:"report_type"`
Result *FileUpload `json:"result"`
Status ReportRunStatus `json:"status"`
SucceededAt int64 `json:"succeeded_at"`
}

// ReportRunList is a list of report runs as retrieved from a list endpoint.
type ReportRunList struct {
ListMeta
Data []*ReportRun `json:"data"`
}
29 changes: 29 additions & 0 deletions reporting_reporttype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package stripe

// ReportTypeListParams is the set of parameters that can be used when listing report types.
type ReportTypeListParams struct {
ListParams `form:"*"`
}

// ReportTypeParams is the set of parameters that can be used when retrieving a report type.
type ReportTypeParams struct {
Params `form:"*"`
}

// ReportType is the resource representing a report type.
type ReportType struct {
Created int64 `json:"created"`
DataAvailableEnd int64 `json:"data_available_end"`
DataAvailableStart int64 `json:"data_available_start"`
ID string `json:"id"`
Name string `json:"name"`
Object string `json:"object"`
Updated int64 `json:"updated"`
Version int64 `json:"version"`
}

// ReportTypeList is a list of report types as retrieved from a list endpoint.
type ReportTypeList struct {
ListMeta
Data []*ReportType `json:"data"`
}