-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgonb.go
78 lines (61 loc) · 1.51 KB
/
gonb.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
package gonb
import (
"encoding/json"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"io"
"net/http"
)
func NewAPI(slug string, token string) *API {
a := new(API)
a.Init(slug, token)
return a
}
type API struct {
access_token string
slug string
cli *http.Client
ctx context.Context
}
func (a *API) Init(slug string, token string) {
a.access_token = token
a.slug = slug
a.ctx = *new(context.Context)
a.cli = oauth2.NewClient(a.ctx, a)
}
func (a API) Token() (*oauth2.Token, error) {
tkn := new(oauth2.Token)
tkn.AccessToken = a.access_token
return tkn, nil
}
/*
Endpoint describes just the final part of the url (e.g. "people").
Res is whatever type should accept the result of the API query.
*/
func (a *API) Exec(endpoint string, res interface{}, method string, params *map[string]string) error {
var body io.Reader
var err error
var resp *http.Response
var req *http.Request
var key, val, url string
if endpoint != "" {
url = "https://" + a.slug + ".nationbuilder.com/api/v1/" + endpoint + "?access_token=" + a.access_token
for key, val = range *params {
url += "&" + key + "=" + val
}
}
if req, err = http.NewRequest(method, url, body); err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if resp, err = a.cli.Do(req); err != nil {
return err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(res); err != nil {
return err
}
return nil
}