-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
73 lines (57 loc) · 1.31 KB
/
client.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
package trello
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
const trellourl = "https://api.trello.com/1/"
type Client struct {
apikey string
apisecret string
apitoken string
}
func New(key, secret, token string) *Client {
return &Client{key, secret, token}
}
func (c *Client) Request(method, function string, postbody io.Reader, extra url.Values) ([]byte, error) {
postdata := url.Values{"key": {c.apikey}, "token": {c.apitoken}}
for k, v := range extra {
postdata[k] = v
}
url := trellourl + function + "?" + postdata.Encode()
req, err := http.NewRequest(method, url, postbody)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode > 300 {
return nil, fmt.Errorf("%s: %s", resp.Status, body)
}
return body, nil
}
func getfield(js []byte, field string) (string, error) {
var i interface{}
err := json.Unmarshal(js, &i)
if err != nil {
return "", err
}
ma, ok := i.(map[string]interface{})
if !ok {
return "", fmt.Errorf("json not a dictionary")
}
f, ok := ma[field]
if !ok {
return "", fmt.Errorf("no field %s", field)
}
str, ok := f.(string)
if !ok {
return "", fmt.Errorf("field %s not a string", field)
}
return str, nil
}