Skip to content

Commit

Permalink
Add support for ScheduledQueryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Jul 28, 2018
1 parent 1d99956 commit 6def002
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Expand Down
64 changes: 64 additions & 0 deletions sigma/scheduledqueryrun/client.go
Original file line number Diff line number Diff line change
@@ -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}
}
28 changes: 28 additions & 0 deletions sigma/scheduledqueryrun/client_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
63 changes: 63 additions & 0 deletions sigma_scheduledqueryrun.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6def002

Please sign in to comment.