Skip to content

Commit

Permalink
Add support for Subscription Events (#51)
Browse files Browse the repository at this point in the history
* Add SubscriptionEvents definitions
Add MetaCursor struct

* Add deleteWithData function to delete without providing UUID

* Add subscription_events

* Update README

* Change type from uint32 to uint64 to handle BigInt on the server

* Update subscription events tests

* Make sure that the setup function returns error on failure
  • Loading branch information
sbrych authored Sep 26, 2022
1 parent b19a670 commit b79eb50
Show file tree
Hide file tree
Showing 5 changed files with 410 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ api.AddTagsToCustomersWithEmail("[email protected]", []string{})
api.AddCustomAttributesToCustomer("customerUUID", []*cm.CustomAttribute{})
```

#### [Subscription Events](https://dev.chartmogul.com/reference/subscription-events)
```go
api.ListSubscriptionEvents(filters *FilterSubscriptionEvents, cursor *MetaCursor)
api.CreateSubscriptionEvent(newSubscriptionEvent *SubscriptionEvent)
api.UpdateSubscriptionEvent(subscriptionEvent *SubscriptionEvent)
api.DeleteSubscriptionEvent(deleteParams *DeleteSubscriptionEvent)
```


### [Metrics API](https://dev.chartmogul.com/docs/introduction-metrics-api)

Expand Down
14 changes: 14 additions & 0 deletions chartmogul.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ type IApi interface {

// Account
RetrieveAccount() (*Account, error)

// Subscription Events
ListSubscriptionEvents(filters *FilterSubscriptionEvents, cursor *MetaCursor) (*SubscriptionEvents, error)
CreateSubscriptionEvent(newSubscriptionEvent *SubscriptionEvent) (*SubscriptionEvent, error)
UpdateSubscriptionEvent(subscriptionEvent *SubscriptionEvent) (*SubscriptionEvent, error)
DeleteSubscriptionEvent(deleteParams *DeleteSubscriptionEvent) error
}

// API is the handle for communicating with Chartmogul.
Expand All @@ -151,6 +157,14 @@ type AnchorCursor struct {
StartAfter string `json:"start-after,omitempty"`
}

type MetaCursor struct {
NextKey uint64 `json:"next_key,omitempty"`
PrevKey uint64 `json:"prev_key,omitempty"`
BeforeKey string `json:"before_key,omitempty"`
Page uint64 `json:"page,omitempty"`
TotalPages uint64 `json:"total_pages,omitempty"`
}

// Errors contains error feedback from ChartMogul
type Errors map[string]string

Expand Down
21 changes: 21 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,24 @@ func (api API) deleteWhat(path string, uuid string, input interface{}, output in

return wrapErrors(res, body, errs)
}

func (api API) deleteWithData(path string, input interface{}) error {
var res gorequest.Response
var body string
var errs []error

backoff.Retry(func() error {
res, body, errs = api.req(gorequest.New().
Delete(prepareURL(path))).
SendStruct(input).
End()

if networkErrors(errs) || isHTTPStatusRetryable(res) {
return errRetry
}
return nil
}, backoff.NewExponentialBackOff())

// wrapping []errors into compatible error & making HTTPError
return wrapErrors(res, []byte(body), errs)
}
84 changes: 84 additions & 0 deletions subscription_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package chartmogul

const subscriptionEventsEndpoint = "subscription_events"

type SubscriptionEvent struct {
ID uint64 `json:"id,omitempty"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`
CustomerExternalID string `json:"customer_external_id,omitempty"`
SubscriptionSetExternalID string `json:"subscription_set_external_id,omitempty"`
SubscriptionExternalID string `json:"subscription_external_id,omitempty"`
PlanExternalID string `json:"plan_external_id,omitempty"`
EventDate string `json:"event_date,omitempty"`
EffectiveDate string `json:"effective_date,omitempty"`
EventType string `json:"event_type,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Errors interface{} `json:"errors,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Quantity int32 `json:"quantity,omitempty"`
Currency string `json:"currency,omitempty"`
AmountInCents int32 `json:"amount_in_cents,omitempty"`
TaxAmountInCents int32 `json:"tax_amount_in_cents,omitempty"`
RetractedEventId string `json:"retracted_event_id,omitempty"`
}

type SubscriptionEvents struct {
SubscriptionEvents []*SubscriptionEvent `json:"subscription_events"`
Meta MetaCursor `json:"meta"`
}

type DeleteSubscriptionEvent struct {
ID uint64 `json:"id,omitempty"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`
ExternalID string `json:"external_id,omitempty"`
}

type FilterSubscriptionEvents struct {
CustomerExternalID string `json:"customer_external_id,omitempty"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`
EffectiveDate string `json:"effective_date,omitempty"`
EventDate string `json:"event_date,omitempty"`
EventType string `json:"event_type,omitempty"`
ExternalID string `json:"external_id,omitempty"`
PlanExternalID string `json:"plan_external_id,omitempty"`
SubscriptionExternalID string `json:"subscription_external_id,omitempty"`
}

type DeleteSubscriptionEventParams struct {
Params *DeleteSubscriptionEvent `json:"subscription_event"`
}

type SubscriptionEventParams struct {
Params *SubscriptionEvent `json:"subscription_event"`
}

func (api API) ListSubscriptionEvents(filters *FilterSubscriptionEvents, cursor *MetaCursor) (*SubscriptionEvents, error) {
result := &SubscriptionEvents{}
query := make([]interface{}, 0, 1)
if cursor != nil {
query = append(query, *cursor)
}
if filters != nil {
query = append(query, *filters)
}

return result, api.list(subscriptionEventsEndpoint, result, query...)
}

func (api API) CreateSubscriptionEvent(newSubscriptionEvent *SubscriptionEvent) (*SubscriptionEvent, error) {
result := &SubscriptionEvent{}
return result, api.create(subscriptionEventsEndpoint, SubscriptionEventParams{Params: newSubscriptionEvent}, result)
}

func (api API) UpdateSubscriptionEvent(subscriptionEvent *SubscriptionEvent) (*SubscriptionEvent, error) {
result := &SubscriptionEvent{}
return result, api.update(subscriptionEventsEndpoint, "", SubscriptionEventParams{Params: subscriptionEvent}, result)
}

func (api API) DeleteSubscriptionEvent(deleteParams *DeleteSubscriptionEvent) error {
return api.deleteWithData(
subscriptionEventsEndpoint,
DeleteSubscriptionEventParams{Params: deleteParams},
)
}
Loading

0 comments on commit b79eb50

Please sign in to comment.