diff --git a/Makefile b/Makefile index 61f12c12b5..b62290223d 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ lint: golint -set_exit_status ./recipienttransfer golint -set_exit_status ./refund golint -set_exit_status ./reversal + golint -set_exit_status ./sigma/scheduledqueryrun golint -set_exit_status ./sku golint -set_exit_status ./source golint -set_exit_status ./sourcetransaction diff --git a/client/api.go b/client/api.go index f3c3df0b80..ff07ceb84f 100644 --- a/client/api.go +++ b/client/api.go @@ -41,6 +41,7 @@ import ( "github.com/stripe/stripe-go/recipient" "github.com/stripe/stripe-go/refund" "github.com/stripe/stripe-go/reversal" + "github.com/stripe/stripe-go/sigma/scheduledqueryrun" "github.com/stripe/stripe-go/sku" "github.com/stripe/stripe-go/source" "github.com/stripe/stripe-go/sourcetransaction" @@ -130,6 +131,8 @@ type API struct { Refunds *refund.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. + SigmaScheduledQueryRuns *scheduledqueryrun.Client // Skus is the client used to invoke /skus APIs. Skus *sku.Client // Sources is the client used to invoke /sources APIs. @@ -195,6 +198,7 @@ func (a *API) Init(key string, backends *Backends) { a.Recipients = &recipient.Client{B: backends.API, Key: key} a.Refunds = &refund.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} a.Sources = &source.Client{B: backends.API, Key: key} a.SourceTransactions = &sourcetransaction.Client{B: backends.API, Key: key} diff --git a/sigma/scheduledqueryrun/client.go b/sigma/scheduledqueryrun/client.go new file mode 100644 index 0000000000..b99aa38d8b --- /dev/null +++ b/sigma/scheduledqueryrun/client.go @@ -0,0 +1,64 @@ +// Package scheduledqueryrun provides API functions related to scheduled query runs. +// +// For more details, see: https://stripe.com/docs/api#scheduled_queries +package scheduledqueryrun + +import ( + "net/http" + + stripe "github.com/stripe/stripe-go" + "github.com/stripe/stripe-go/form" +) + +// Client is used to invoke /sigma/scheduled_query_runs APIs. +type Client struct { + B stripe.Backend + Key string +} + +// Get returns the details of an scheduled query run. +func Get(id string, params *stripe.SigmaScheduledQueryRunParams) (*stripe.SigmaScheduledQueryRun, error) { + return getC().Get(id, params) +} + +// Get returns the details of an scheduled query run. +func (c Client) Get(id string, params *stripe.SigmaScheduledQueryRunParams) (*stripe.SigmaScheduledQueryRun, error) { + path := stripe.FormatURLPath("/sigma/scheduled_query_runs/%s", id) + run := &stripe.SigmaScheduledQueryRun{} + err := c.B.Call(http.MethodGet, path, c.Key, params, run) + return run, err +} + +// List returns a list of scheduled query runs. +func List(params *stripe.SigmaScheduledQueryRunListParams) *Iter { + return getC().List(params) +} + +// List returns a list of scheduled query runs. +func (c Client) List(listParams *stripe.SigmaScheduledQueryRunListParams) *Iter { + return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) { + list := &stripe.SigmaScheduledQueryRunList{} + err := c.B.CallRaw(http.MethodGet, "/sigma/scheduled_query_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 scheduled query runs. +type Iter struct { + *stripe.Iter +} + +// SigmaScheduledQueryRun returns the scheduled query run which the iterator is currently pointing to. +func (i *Iter) SigmaScheduledQueryRun() *stripe.SigmaScheduledQueryRun { + return i.Current().(*stripe.SigmaScheduledQueryRun) +} + +func getC() Client { + return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key} +} diff --git a/sigma/scheduledqueryrun/client_test.go b/sigma/scheduledqueryrun/client_test.go new file mode 100644 index 0000000000..c21e098fd7 --- /dev/null +++ b/sigma/scheduledqueryrun/client_test.go @@ -0,0 +1,28 @@ +package scheduledqueryrun + +import ( + "testing" + + assert "github.com/stretchr/testify/require" + stripe "github.com/stripe/stripe-go" + _ "github.com/stripe/stripe-go/testing" +) + +func TestSigmaScheduledQueryRunGet(t *testing.T) { + t.Skip("skipping test as stripe-mock does not support this resource") + run, err := Get("sqr_123", nil) + assert.Nil(t, err) + assert.NotNil(t, run) + assert.Equal(t, "scheduled_query_run", run.Object) +} + +func TestSigmaScheduledQueryRunList(t *testing.T) { + t.Skip("skipping test as stripe-mock does not support this resource") + i := List(&stripe.SigmaScheduledQueryRunListParams{}) + + // Verify that we can get at least one scheduled query run + assert.True(t, i.Next()) + assert.Nil(t, i.Err()) + assert.NotNil(t, i.SigmaScheduledQueryRun()) + assert.Equal(t, "scheduled_query_run", i.SigmaScheduledQueryRun().Object) +} diff --git a/sigma_scheduledqueryrun.go b/sigma_scheduledqueryrun.go new file mode 100644 index 0000000000..d2ff08113c --- /dev/null +++ b/sigma_scheduledqueryrun.go @@ -0,0 +1,63 @@ +package stripe + +import "encoding/json" + +// SigmaScheduledQueryRunStatus is the possible values for status for a scheduled query run. +type SigmaScheduledQueryRunStatus string + +const ( + SigmaScheduledQueryRunStatusCanceled SigmaScheduledQueryRunStatus = "canceled" + SigmaScheduledQueryRunStatusCompleted SigmaScheduledQueryRunStatus = "completed" + SigmaScheduledQueryRunStatusFailed SigmaScheduledQueryRunStatus = "failed" + SigmaScheduledQueryRunStatusTimedOut SigmaScheduledQueryRunStatus = "timed_out" +) + +// SigmaScheduledQueryRunParams is the set of parameters that can be used when updating a scheduled query run. +type SigmaScheduledQueryRunParams struct { + Params `form:"*"` +} + +// SigmaScheduledQueryRunListParams is the set of parameters that can be used when listing scheduled query runs. +type SigmaScheduledQueryRunListParams struct { + ListParams `form:"*"` +} + +// SigmaScheduledQueryRun is the resource representing a scheduled query run. +type SigmaScheduledQueryRun struct { + Created int64 `json:"created"` + DataLoadTime int64 `json:"data_load_time"` + Error string `json:"error"` + File *FileUpload `json:"file"` + ID string `json:"id"` + Livemode bool `json:"livemode"` + Object string `json:"object"` + ResultAvailableUntil int64 `json:"result_available_until"` + SQL string `json:"sql"` + Status SigmaScheduledQueryRunStatus `json:"status"` + Query string `json:"query"` +} + +// SigmaScheduledQueryRunList is a list of scheduled query runs as retrieved from a list endpoint. +type SigmaScheduledQueryRunList struct { + ListMeta + Data []*SigmaScheduledQueryRun `json:"data"` +} + +// UnmarshalJSON handles deserialization of an SigmaScheduledQueryRun. +// This custom unmarshaling is needed because the resulting +// property may be an id or the full struct if it was expanded. +func (i *SigmaScheduledQueryRun) UnmarshalJSON(data []byte) error { + if id, ok := ParseID(data); ok { + i.ID = id + return nil + } + + type sigmaScheduledQueryRun SigmaScheduledQueryRun + var v sigmaScheduledQueryRun + if err := json.Unmarshal(data, &v); err != nil { + return err + } + + *i = SigmaScheduledQueryRun(v) + return nil +}