Skip to content

Commit

Permalink
New GetLinksByUserID request
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jan 18, 2020
1 parent 26cfa8e commit 0758523
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
58 changes: 55 additions & 3 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,27 @@ func main() {
}

//
// Example: Create a Link (making a custom short code using the user's name)
// Example: Create a Link
//
link := &tonicpow.Link{
CampaignID: campaign.ID,
UserID: user.ID,
}
if link, err = createLink(link, userSessionToken); err != nil {
log.Fatal(err.Error())
}

//
// Example: Get Link
//
if link, err = getLink(link.ID, userSessionToken); err != nil {
log.Fatal(err.Error())
}

//
// Example: Create a Link (making a custom short code using the user's name)
//
link = &tonicpow.Link{
CampaignID: campaign.ID,
UserID: user.ID,
CustomShortCode: fmt.Sprintf("%s%d", user.FirstName, rand.Intn(100000)),
Expand All @@ -284,11 +302,27 @@ func main() {
}

//
// Example: Get Link
// Example: Create another custom link
//
if link, err = getLink(link.ID, userSessionToken); err != nil {
link = &tonicpow.Link{
CampaignID: campaign.ID,
UserID: user.ID,
CustomShortCode: fmt.Sprintf("%s%d", user.FirstName+"zzz", rand.Intn(100000)),
}
if link, err = createLink(link, userSessionToken); err != nil {
log.Fatal(err.Error())
}

//
// Example: Get Link(s) by user
//
var links []*tonicpow.Link
if links, err = getLinks(link.ID, userSessionToken); err != nil {
log.Fatal(err.Error())
}
if len(links) > 0 {
log.Printf("found links %x", links)
}

//
// Example: List active campaigns
Expand Down Expand Up @@ -376,6 +410,15 @@ func main() {

log.Printf("successful conversion event: %d payout after: %s", newConversion.ID, newConversion.PayoutAfter)

//
// Example: Fire a conversion on a goal (by name)
//
//if newConversion, err = TonicPowAPI.CreateConversionByGoalName(goal.Name, visitorSession.TncpwSession, "", 0.00, 10); err != nil {
// log.Fatal(err.Error())
//}

//log.Printf("successful conversion event: %d payout after: %s", newConversion.ID, newConversion.PayoutAfter)

//
// Example: Get conversion
//
Expand Down Expand Up @@ -489,6 +532,15 @@ func getLink(linkID uint64, userSessionToken string) (link *tonicpow.Link, err e
return
}

func getLinks(userID uint64, userSessionToken string) (links []*tonicpow.Link, err error) {
if links, err = TonicPowAPI.GetLinksByUserID(userID, userSessionToken); err != nil {
log.Fatalf("get link failed - api error: %s", TonicPowAPI.LastRequest.Error.Message)
} else {
log.Printf("got link(s) %d", len(links))
}
return
}

func createVisitorSession(visitorSession *tonicpow.VisitorSession) (createdSession *tonicpow.VisitorSession, err error) {
if createdSession, err = TonicPowAPI.CreateVisitorSession(visitorSession); err != nil {
log.Fatalf("create visitor session failed - api error: %s data: %s", TonicPowAPI.LastRequest.Error.Message, TonicPowAPI.LastRequest.Error.Data)
Expand Down
29 changes: 29 additions & 0 deletions links.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,32 @@ func (c *Client) CheckLink(shortCode string) (link *Link, err error) {
err = json.Unmarshal([]byte(response), &link)
return
}

// GetLinksByUserID will get links associated to the user id
// This will return an error if the link(s) are not found (404)
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#23d068f1-4f0e-476a-a802-50b7edccd0b2
func (c *Client) GetLinksByUserID(userID uint64, userSessionToken string) (links []*Link, err error) {

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

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/user/%d", modelLink, userID), http.MethodGet, nil, userSessionToken); 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), &links)
return
}

0 comments on commit 0758523

Please sign in to comment.