forked from VojtechVitek/go-trello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
153 lines (134 loc) · 4.34 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2014 go-trello authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package trello
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// Client - Trello Client Type
type Client struct {
client *http.Client
endpoint string
version string
}
// Version - Trello API Version
func (c *Client) Version() string {
return c.version
}
func (c *Client) do(req *http.Request) (body []byte, err error) {
resp, err := c.client.Do(req)
if err == nil {
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err == nil {
if resp.StatusCode != 200 {
err = fmt.Errorf("Received unexpected status %d while trying to retrieve the server data with \"%s\"", resp.StatusCode, string(body))
return nil, err
}
}
}
return
}
// Get - HTTP GET
func (c *Client) Get(resource string) (body []byte, err error) {
req, err := http.NewRequest("GET", c.endpoint+resource, nil)
if err == nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
body, err = c.do(req)
}
return
}
// Post - HTTP POST
func (c *Client) Post(resource string, data url.Values) (body []byte, err error) {
req, err := http.NewRequest("POST", c.endpoint+resource, strings.NewReader(data.Encode()))
if err == nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
body, err = c.do(req)
}
return
}
// Put - HTTP PUT
func (c *Client) Put(resource string, data url.Values) (body []byte, err error) {
req, err := http.NewRequest("PUT", c.endpoint+resource, strings.NewReader(data.Encode()))
if err == nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
body, err = c.do(req)
}
return
}
// Delete - HTTP DELETE
func (c *Client) Delete(resource string) (body []byte, err error) {
req, err := http.NewRequest("DELETE", c.endpoint+resource, nil)
if err == nil {
body, err = c.do(req)
}
return
}
// bearerRoundTripper Type
type bearerRoundTripper struct {
Delegate http.RoundTripper
key string
token *string
}
// RoundTrip encodes key and token as a delegate
func (b *bearerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if b.Delegate == nil {
b.Delegate = http.DefaultTransport
}
values := req.URL.Query()
values.Add("key", b.key)
values.Add("token", *b.token)
req.URL.RawQuery = values.Encode()
return b.Delegate.RoundTrip(req)
}
// newBearerTokenTransport will return an http.RoundTripper which will add the
// provided application id and token to API calls.
// If Delegate is left unset the http.DefaultTransport will be used.
// See https://trello.com/app-key to get your applicationKey
// See https://trello.com/1/connect?key=MYKEYFROMABOVE&name=MYAPPNAME&response_type=token&scope=read,write&expiration=1d
// to get a read/write token good for 1 day
func newBearerTokenTransport(applicationKey string, token *string) *bearerRoundTripper {
return &bearerRoundTripper{
key: applicationKey,
token: token,
}
}
// NewCustomClient can be used to implement your own client
func NewCustomClient(client *http.Client) (*Client, error) {
version := "1"
endpoint := "https://api.trello.com/" + version
return &Client{
client: client,
endpoint: endpoint,
version: version,
}, nil
}
// NewAuthClient will create a trello client which allows authentication. It uses
// NewBearerTokenTransport to create an http.Client which can be used as a trello
// client.
func NewAuthClient(applicationKey string, token *string) (*Client, error) {
rr := newBearerTokenTransport(applicationKey, token)
client := &http.Client{
Transport: rr,
}
return NewCustomClient(client)
}
// NewClient returns a client needed to make trello API calls. If transport is nil
// all API calls will be unauthenticated. If you have a bearer token, NewBearerTokenTransport()
// may be helpful in making calls authenticated.
func NewClient() (*Client, error) {
return NewCustomClient(http.DefaultClient)
}