Skip to content

Commit

Permalink
Merge pull request #4 from tonicpow/feature/v1
Browse files Browse the repository at this point in the history
Upgrade to V1 Requests
  • Loading branch information
mrz1836 authored Dec 28, 2019
2 parents 0eda40d + 05893aa commit 1ae051a
Show file tree
Hide file tree
Showing 13 changed files with 1,812 additions and 332 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# go-tonicpow
**go-tonicpow** is the official golang implementation for interacting with the TonicPow API
**go-tonicpow** is the official golang implementation for interacting with the [TonicPow API](https://docs.tonicpow.com)

[![Build Status](https://travis-ci.com/tonicpow/go-tonicpow.svg?branch=master)](https://travis-ci.com/tonicpow/go-tonicpow)
[![Report](https://goreportcard.com/badge/github.com/tonicpow/go-tonicpow?style=flat)](https://goreportcard.com/report/github.com/tonicpow/go-tonicpow)
Expand Down Expand Up @@ -29,14 +29,21 @@ $ go get -u github.com/tonicpow/go-tonicpow
You can view the generated [documentation here](https://godoc.org/github.com/tonicpow/go-tonicpow).

### Features
- Complete coverage for the [TonicPow.com](https://tonicpow.com/) API
- Client is completely configurable
- Customize API Key and User Agent per request
- [Client](client.go) is completely configurable
- Using [heimdall http client](https://github.com/gojek/heimdall) with exponential backoff & more
- Coverage for the [TonicPow.com API](https://docs.tonicpow.com/)
- [x] Authentication
- [x] Users
- [x] Advertiser Profiles
- [x] Campaigns
- [x] Goals
- [x] Links

## Examples & Tests
All unit tests and [examples](tonicpow_test.go) run via [Travis CI](https://travis-ci.org/tonicpow/go-tonicpow) and uses [Go version 1.13.x](https://golang.org/doc/go1.13). View the [deployment configuration file](.travis.yml).

View a [full example application](examples/examples.go).

Run all tests (including integration tests)
```bash
$ cd ../go-tonicpow
Expand Down Expand Up @@ -67,12 +74,13 @@ Basic implementation:
package main

import (
"os"
"github.com/tonicpow/go-tonicpow"
)

func main() {
client, _ := NewClient(privateGUID)
resp, _ = client.ConvertGoal("signup-goal", "f773c231ee9.....", 0, "")
api, _ := tonicpow.NewClient(os.Getenv("TONICPOW_API_KEY"), tonicpow.LiveEnvironment, nil)
_ = api.ConvertGoal("new-lead-goal", "s358wef983283...", "", "")
}
```

Expand Down
103 changes: 103 additions & 0 deletions advertiser_profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package tonicpow

import (
"encoding/json"
"fmt"
"net/http"
)

// permitFields will remove fields that cannot be used
func (a *AdvertiserProfile) permitFields() {
a.UserID = 0
}

// CreateAdvertiserProfile will make a new advertiser profile
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#153c0b65-2d4c-4972-9aab-f791db05b37b
func (c *Client) CreateAdvertiserProfile(profile *AdvertiserProfile, userSessionToken string) (createdProfile *AdvertiserProfile, err error) {

// Basic requirements
if profile.UserID == 0 {
err = fmt.Errorf("missing required attribute: %s", fieldUserID)
return
}

// Fire the request
var response string
if response, err = c.request(modelAdvertiser, http.MethodPost, profile, userSessionToken); err != nil {
return
}

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

// Convert model response
createdProfile = new(AdvertiserProfile)
err = json.Unmarshal([]byte(response), createdProfile)
return
}

// GetAdvertiserProfile will get an existing advertiser profile
// This will return an error if the profile is not found (404)
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#b3a62d35-7778-4314-9321-01f5266c3b51
func (c *Client) GetAdvertiserProfile(profileID uint64, userSessionToken string) (profile *AdvertiserProfile, err error) {

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

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/details/%d", modelAdvertiser, profileID), 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
profile = new(AdvertiserProfile)
err = json.Unmarshal([]byte(response), profile)
return
}

// UpdateAdvertiserProfile will update an existing profile
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#0cebd1ff-b1ce-4111-aff6-9d586f632a84
func (c *Client) UpdateAdvertiserProfile(profile *AdvertiserProfile, userSessionToken string) (updatedProfile *AdvertiserProfile, err error) {

// Basic requirements
if profile.ID == 0 {
err = fmt.Errorf("missing required attribute: %s", fieldID)
return
}

// Permit fields
profile.permitFields()

// Fire the request
var response string
if response, err = c.request(modelAdvertiser, http.MethodPut, profile, 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
updatedProfile = new(AdvertiserProfile)
err = json.Unmarshal([]byte(response), updatedProfile)
return
}
59 changes: 59 additions & 0 deletions authentication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package tonicpow

import (
"net/http"
)

// createSession will establish a new session with the api
// This is run in the NewClient() method
//
// For more information: https://docs.tonicpow.com/#632ed94a-3afd-4323-af91-bdf307a399d2
func (c *Client) createSession() (err error) {

// Start the post data with api key
data := map[string]string{fieldApiKey: c.Parameters.apiKey}

// Fire the request
var response string
if response, err = c.request("auth/session", http.MethodPost, data, ""); err != nil {
return
}

// Only a 201 is treated as a success
err = c.error(http.StatusCreated, response)
return
}

// ProlongSession will a session alive based on the forUser (user vs api session)
// Use customSessionToken for any token, user token, if empty it will use current api session token
//
// For more information: https://docs.tonicpow.com/#632ed94a-3afd-4323-af91-bdf307a399d2
func (c *Client) ProlongSession(customSessionToken string) (err error) {

// Fire the request
var response string
if response, err = c.request("auth/session", http.MethodGet, nil, customSessionToken); err != nil {
return
}

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

// EndSession will end a session based on the forUser (user vs api session)
// Use customSessionToken for any token, user token, if empty it will use current api session token
//
// For more information: https://docs.tonicpow.com/#632ed94a-3afd-4323-af91-bdf307a399d2
func (c *Client) EndSession(customSessionToken string) (err error) {

// Fire the request
var response string
if response, err = c.request("auth/session", http.MethodDelete, nil, customSessionToken); err != nil {
return
}

// Only a 200 is treated as a success
err = c.error(http.StatusOK, response)
return
}
129 changes: 129 additions & 0 deletions campaigns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package tonicpow

import (
"encoding/json"
"fmt"
"net/http"
)

// permitFields will remove fields that cannot be used
func (c *Campaign) permitFields() {
c.AdvertiserProfileID = 0
c.Balance = 0
c.BalanceSatoshis = 0
c.FundingAddress = ""
c.PublicGUID = ""
}

// CreateCampaign will make a new campaign
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#b67e92bf-a481-44f6-a31d-26e6e0c521b1
func (c *Client) CreateCampaign(campaign *Campaign, userSessionToken string) (createdCampaign *Campaign, err error) {

// Basic requirements
if campaign.AdvertiserProfileID == 0 {
err = fmt.Errorf("missing required attribute: %s", fieldAdvertiserProfileID)
return
}

// Fire the request
var response string
if response, err = c.request(modelCampaign, http.MethodPost, campaign, userSessionToken); err != nil {
return
}

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

// Convert model response
createdCampaign = new(Campaign)
err = json.Unmarshal([]byte(response), createdCampaign)
return
}

// GetCampaign will get an existing campaign
// This will return an error if the campaign is not found (404)
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#b827446b-be34-4678-b347-33c4f63dbf9e
func (c *Client) GetCampaign(campaignID uint64, userSessionToken string) (campaign *Campaign, err error) {

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

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/details/%d", modelCampaign, campaignID), 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
campaign = new(Campaign)
err = json.Unmarshal([]byte(response), campaign)
return
}

// GetCampaignBalance will update the models's balance from the chain
//
// For more information: https://docs.tonicpow.com/#b6c60c63-8ac5-4c74-a4a2-cf3e858e5a8d
func (c *Client) GetCampaignBalance(campaignID uint64) (campaign *Campaign, err error) {

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/balance/%d", modelCampaign, campaignID), 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
campaign = new(Campaign)
err = json.Unmarshal([]byte(response), campaign)
return
}

// UpdateCampaign will update an existing campaign
// Use the userSessionToken if making request on behalf of another user
//
// For more information: https://docs.tonicpow.com/#665eefd6-da42-4ca9-853c-fd8ca1bf66b2
func (c *Client) UpdateCampaign(campaign *Campaign, userSessionToken string) (updatedCampaign *Campaign, err error) {

// Basic requirements
if campaign.ID == 0 {
err = fmt.Errorf("missing required attribute: %s", fieldID)
return
}

// Permit fields
campaign.permitFields()

// Fire the request
var response string
if response, err = c.request(modelCampaign, http.MethodPut, campaign, 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
updatedCampaign = new(Campaign)
err = json.Unmarshal([]byte(response), updatedCampaign)
return
}
Loading

0 comments on commit 1ae051a

Please sign in to comment.