-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from tonicpow/feature/v1
Upgrade to V1 Requests
- Loading branch information
Showing
13 changed files
with
1,812 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.