Skip to content

Commit

Permalink
Cancel a conversion if delayed
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jan 10, 2020
1 parent d4d1798 commit 671b95a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (c *Client) GetCampaign(campaignID uint64, userSessionToken string) (campai

// ListCampaigns will return a list of active campaigns
// Use the customSessionToken if making request on behalf of another user / advertiser
// This will return an error if the campaign is not found (404)
//
// For more information: https://docs.tonicpow.com/#c1b17be6-cb10-48b3-a519-4686961ff41c
func (c *Client) ListCampaigns(customSessionToken string) (campaigns []*Campaign, err error) {
Expand All @@ -97,6 +98,7 @@ func (c *Client) ListCampaigns(customSessionToken string) (campaigns []*Campaign
}

// GetCampaignBalance will update the models's balance from the chain
// This will return an error if the campaign is not found (404)
//
// For more information: https://docs.tonicpow.com/#b6c60c63-8ac5-4c74-a4a2-cf3e858e5a8d
func (c *Client) GetCampaignBalance(campaignID uint64) (campaign *Campaign, err error) {
Expand Down
30 changes: 30 additions & 0 deletions conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,33 @@ func (c *Client) GetConversion(conversionID uint64) (conversion *Conversion, err
err = json.Unmarshal([]byte(response), &conversion)
return
}

// CancelConversion will cancel an existing conversion (if delay was set and > 1 minute remaining)
//
// For more information: https://docs.tonicpow.com/#e650b083-bbb4-4ff7-9879-c14b1ab3f753
func (c *Client) CancelConversion(conversionID uint64, cancelReason string) (conversion *Conversion, err error) {

// Must have an id
if conversionID == 0 {
err = fmt.Errorf("missing field: %s", fieldID)
return
}

// Start the post data
data := map[string]string{fieldID: fmt.Sprintf("%d", conversionID), fieldReason: cancelReason}

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/cancel", modelConversion), http.MethodPut, data, ""); err != nil {
return
}

// Only a 200 is treated as a success
if err = c.error(http.StatusOK, response); err != nil {
return
}

// Convert model response
err = json.Unmarshal([]byte(response), &conversion)
return
}
2 changes: 2 additions & 0 deletions definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
fieldPasswordConfirm = "password_confirm"
fieldPhone = "phone"
fieldPhoneCode = "phone_code"
fieldReason = "reason"
fieldShortCode = "short_code"
fieldToken = "token"
fieldUserID = "user_id"
Expand Down Expand Up @@ -149,6 +150,7 @@ type Conversion struct {
PayoutAfter string `json:"payout_after,omitempty"`
Status string `json:"status,omitempty"`
TxID string `json:"tx_id,omitempty"`
UserID uint64 `json:"user_id,omitempty"`
}

// Link is the link model (child of User) (relates Campaign to User)
Expand Down
12 changes: 12 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ func main() {

log.Printf("got conversion: %d", conversion.ID)

//
// Example: Cancel a Delayed Conversion
//
if conversion, err = TonicPowAPI.CancelConversion(conversion.ID, "not needed anymore"); err != nil {
os.Exit(1)
}

log.Printf("conversion status: %s", conversion.Status)

//
// Example: Create Conversion by User ID
//
if newConversion, err = TonicPowAPI.CreateConversionByUserID(1, 1, "", 0); err != nil {
os.Exit(1)
}
Expand Down

0 comments on commit 671b95a

Please sign in to comment.