-
Notifications
You must be signed in to change notification settings - Fork 940
/
Copy pathapi.go
99 lines (77 loc) · 2.64 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package patreonapi
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
const APIBase = "https://www.patreon.com/api/oauth2/v2"
const (
// AuthorizationURL specifies Patreon's OAuth2 authorization endpoint (see https://tools.ietf.org/html/rfc6749#section-3.1).
// See Example_refreshToken for examples.
AuthorizationURL = "https://www.patreon.com/oauth2/authorize"
// AccessTokenURL specifies Patreon's OAuth2 token endpoint (see https://tools.ietf.org/html/rfc6749#section-3.2).
// See Example_refreshToken for examples.
AccessTokenURL = "https://api.patreon.com/oauth2/token"
)
type Client struct {
httpClient *http.Client
}
func NewClient(client *http.Client) *Client {
return &Client{httpClient: client}
}
func (c *Client) Get(path string, dataDst interface{}) error {
resp, err := c.httpClient.Get(APIBase + path)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
return c.reqError("Bad response code "+resp.Status, resp)
}
if dataDst != nil {
fullbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// fmt.Println(string(fullbody))
err = json.Unmarshal(fullbody, dataDst)
return err
}
return nil
}
func (c *Client) reqError(msg string, resp *http.Response) error {
fullbody, _ := ioutil.ReadAll(resp.Body)
return errors.New(msg + ": " + string(fullbody))
}
// Sample response with email scope for /identity?fields[user]=about,created,email,first_name,full_name,image_url,last_name,social_connections,thumb_url,url,vanity
func (c *Client) FetchUser() (r *UserResponse, err error) {
v := url.Values(make(map[string][]string))
v.Set("fields[user]", "about,created,email,first_name,full_name,image_url,last_name,social_connections,thumb_url,url,vanity")
err = c.Get("/identity?"+v.Encode(), &r)
return
}
func (c *Client) FetchCampaigns() (r *CampaignsResponse, err error) {
err = c.Get("/campaigns", &r)
return
}
func (c *Client) FetchMembers(campaign string, count int, cursor string) (r *MembersResponse, err error) {
uri := "/campaigns/" + campaign + "/members?"
v := url.Values(make(map[string][]string))
v.Set("fields[member]", "full_name,is_follower,last_charge_date,last_charge_status,lifetime_support_cents,currently_entitled_amount_cents,patron_status")
v.Set("fields[user]", "about,created,first_name,full_name,image_url,last_name,social_connections,thumb_url,url,vanity")
v.Set("include", "user")
if cursor != "" {
v.Set("page[cursor]", cursor)
}
if count != 0 {
v.Set("page[count]", strconv.Itoa(count))
}
err = c.Get(uri+v.Encode(), &r)
if err == nil {
err = DecodeIncludes(r.Included)
}
return
}