grim/paytreon

get campaign and pledges working
draft
2018-05-27, Gary Kramlich
9f7ddaadd565
Parents cbc5ce1664ff
Children ca53823de404
get campaign and pledges working
  • +103 -1
    client.go
  • +0 -5
    types.go
  • --- a/client.go Sun May 27 01:38:02 2018 -0500
    +++ b/client.go Sun May 27 18:13:40 2018 -0500
    @@ -1,5 +1,107 @@
    package paytreon
    import (
    -// "net/http"
    + "context"
    + "fmt"
    + "net/http"
    + "reflect"
    +
    + "github.com/google/jsonapi"
    + "golang.org/x/oauth2"
    +)
    +
    +type Client struct {
    + client *http.Client
    + baseURL string
    +}
    +
    +const (
    + baseURL = "https://api.patreon.com/oauth2/api"
    )
    +
    +func NewClient(client *http.Client) *Client {
    + if client == nil {
    + client = http.DefaultClient
    + }
    +
    + return NewCustomClient(client, baseURL)
    +}
    +
    +func NewClientWithStaticToken(token string) *Client {
    + source := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
    + client := oauth2.NewClient(context.Background(), source)
    +
    + return NewCustomClient(client, baseURL)
    +}
    +
    +func NewCustomClient(client *http.Client, baseURL string) *Client {
    + return &Client{
    + client: client,
    + baseURL: baseURL,
    + }
    +}
    +
    +func (c *Client) makeRequest(path string) (*http.Response, error) {
    + resp, err := c.client.Get(c.baseURL + path)
    + if err != nil {
    + return nil, err
    + }
    +
    + return resp, nil
    +}
    +
    +func (c *Client) getSingle(path string, payload interface{}) error {
    + resp, err := c.makeRequest(path)
    + if err != nil {
    + return err
    + }
    +
    + return jsonapi.UnmarshalPayload(resp.Body, payload)
    +}
    +
    +func (c *Client) getMultiple(path string, payload reflect.Type) ([]interface{}, error) {
    + resp, err := c.makeRequest(path)
    + if err != nil {
    + return nil, err
    + }
    +
    + return jsonapi.UnmarshalManyPayload(resp.Body, payload)
    +}
    +
    +func (c *Client) CurrentUser() (User, error) {
    + var user User
    +
    + err := c.getSingle("/current_user", &user)
    +
    + return user, err
    +}
    +
    +func (c *Client) Campaigns() ([]*Campaign, error) {
    + objs, err := c.getMultiple("/current_user/campaigns", reflect.TypeOf(new(Campaign)))
    + if err != nil {
    + return nil, err
    + }
    +
    + campaigns := []*Campaign{}
    +
    + for _, obj := range objs {
    + campaigns = append(campaigns, obj.(*Campaign))
    + }
    +
    + return campaigns, nil
    +}
    +
    +func (c *Client) Pledges(campaign *Campaign) ([]*Pledge, error) {
    + url := fmt.Sprintf("/campaigns/%s/pledges", campaign.ID)
    + objs, err := c.getMultiple(url, reflect.TypeOf(new(Pledge)))
    + if err != nil {
    + return nil, err
    + }
    +
    + pledges := make([]*Pledge, len(objs))
    + for idx, obj := range objs {
    + pledges[idx] = obj.(*Pledge)
    + }
    +
    + return pledges, nil
    +}
    --- a/types.go Sun May 27 01:38:02 2018 -0500
    +++ b/types.go Sun May 27 18:13:40 2018 -0500
    @@ -1,8 +1,5 @@
    package paytreon
    -import ()
    -
    -// Address represents a Patreon's address.
    type Address struct {
    ID string `jsonapi:"primary,address"`
    @@ -110,12 +107,10 @@
    CreationCount int `jsonapi:"attr,creation_count"`
    OutstandingPaymentAmountCents int `jsonapi:"attr,outstanding_payment_amount_cents"`
    - // Categories *CategoriesRelationship `jsonapi:"relation,categories,omitempty"`
    Creator *User `jsonapi:"relation,creator,omitempty"`
    Rewards []*Reward `jsonapi:"relation,rewards,omitempty"`
    Goals []*Goal `jsonapi:"relation,goals,omitempty"`
    Pledges []*Pledge `jsonapi:"relation,pledges,omitempty"`
    - // PostAggregation *PostAggregationRelationship `jsonapi:"relation,post_aggregation,omitempty"`
    }
    type Goal struct {