From f363ee5ab29d685d1997764f3f0a3d37e7cfe9e1 Mon Sep 17 00:00:00 2001 From: mrz1836 Date: Mon, 30 Dec 2019 23:24:58 -0500 Subject: [PATCH] Implemented list campaigns request --- campaigns.go | 23 +++++++++++++++++++++++ definitions.go | 29 ++++++++++++++++------------- examples/examples.go | 10 ++++++++++ 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/campaigns.go b/campaigns.go index 891a13b..5857a3d 100644 --- a/campaigns.go +++ b/campaigns.go @@ -11,7 +11,9 @@ func (c *Campaign) permitFields() { c.AdvertiserProfileID = 0 c.Balance = 0 c.BalanceSatoshis = 0 + c.Clicks = 0 c.FundingAddress = "" + c.LinksCreated = 0 c.PublicGUID = "" } @@ -72,6 +74,27 @@ func (c *Client) GetCampaign(campaignID uint64, userSessionToken string) (campai return } +// ListCampaigns will return a list of active campaigns +// +// For more information: https://docs.tonicpow.com/#c1b17be6-cb10-48b3-a519-4686961ff41c +func (c *Client) ListCampaigns() (campaigns []*Campaign, err error) { + + // Fire the request + var response string + if response, err = c.request(fmt.Sprintf("%s/list", modelCampaign), http.MethodGet, nil, ""); 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), &campaigns) + return +} + // GetCampaignBalance will update the models's balance from the chain // // For more information: https://docs.tonicpow.com/#b6c60c63-8ac5-4c74-a4a2-cf3e858e5a8d diff --git a/definitions.go b/definitions.go index a6b8257..04215d3 100644 --- a/definitions.go +++ b/definitions.go @@ -98,19 +98,22 @@ type AdvertiserProfile struct { // // For more information: https://docs.tonicpow.com/#5aca2fc7-b3c8-445b-aa88-f62a681f8e0c type Campaign struct { - AdvertiserProfileID uint64 `json:"advertiser_profile_id,omitempty"` - Balance float64 `json:"balance,omitempty"` - BalanceSatoshis int64 `json:"balance_satoshis,omitempty"` - Currency string `json:"currency,omitempty"` - Description string `json:"description,omitempty"` - FundingAddress string `json:"funding_address,omitempty"` - Goals []*Goal `json:"goals,omitempty"` - ID uint64 `json:"id,omitempty"` - ImageURL string `json:"image_url,omitempty"` - PayPerClickRate float64 `json:"pay_per_click_rate,omitempty"` - PublicGUID string `json:"public_guid,omitempty"` - TargetURL string `json:"target_url,omitempty"` - Title string `json:"title,omitempty"` + AdvertiserProfile *AdvertiserProfile `json:"advertiser_profile,omitempty"` + AdvertiserProfileID uint64 `json:"advertiser_profile_id,omitempty"` + Balance float64 `json:"balance,omitempty"` + BalanceSatoshis int64 `json:"balance_satoshis,omitempty"` + Clicks uint64 `json:"clicks,omitempty"` + Currency string `json:"currency,omitempty"` + Description string `json:"description,omitempty"` + FundingAddress string `json:"funding_address,omitempty"` + Goals []*Goal `json:"goals,omitempty"` + ID uint64 `json:"id,omitempty"` + ImageURL string `json:"image_url,omitempty"` + LinksCreated uint64 `json:"links_created,omitempty"` + PayPerClickRate float64 `json:"pay_per_click_rate,omitempty"` + PublicGUID string `json:"public_guid,omitempty"` + TargetURL string `json:"target_url,omitempty"` + Title string `json:"title,omitempty"` } // Goal is the goal model (child of Campaign) diff --git a/examples/examples.go b/examples/examples.go index a4c35c3..34c480b 100644 --- a/examples/examples.go +++ b/examples/examples.go @@ -280,4 +280,14 @@ func main() { } else { log.Printf("got link by id %d", goal.ID) } + + // + // Example: List active campaigns + // + var campaigns []*tonicpow.Campaign + if campaigns, err = TonicPowAPI.ListCampaigns(); err != nil { + log.Fatalf("list campaign failed - api error: %s", TonicPowAPI.LastRequest.Error.Message) + } else { + log.Printf("campaigns found: %d", len(campaigns)) + } }