/
main.go
159 lines (130 loc) · 3.33 KB
/
main.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
154
155
156
157
158
159
package coda
import (
"bytes"
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
"io"
"log"
"net/http"
"net/url"
"path"
)
const BaseUrl = "https://coda.io/apis/v1"
type transport struct {
defaultTransport http.RoundTripper
token string
}
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", fmt.Sprintf("%s %s", "Bearer", t.token))
return t.defaultTransport.RoundTrip(req)
}
type Client struct {
BaseURL *url.URL
UserAgent string
HttpClient *http.Client
}
func DefaultClient(apiKey string) *Client {
httpClient := &http.Client{
Transport: &transport{
defaultTransport: http.DefaultTransport,
token: apiKey,
},
}
u, _ := url.Parse(BaseUrl)
codaClient := &Client{
UserAgent: "golang_bot/1.0",
HttpClient: httpClient,
BaseURL: u,
}
return codaClient
}
func (c *Client) apiCall(method, url string, body interface{}, response interface{}) error {
req, err := c.newRequest(method, url, body)
if err != nil {
return err
}
_, err = c.do(req, &response)
if err != nil {
log.Print("Unable to make request.")
return err
}
return err
}
func (c *Client) apiCallFull(method, url string, body, queryParams, response interface{}) error {
req, err := c.newRequestFull(method, url, body, queryParams)
if err != nil {
return err
}
_, err = c.do(req, &response)
if err != nil {
log.Print("Unable to make request.")
return err
}
return err
}
func (c *Client) newRequestFull(method, methodPath string, body interface{}, queryParams interface{}) (*http.Request, error) {
rel := &url.URL{Path: methodPath}
rel.Path = path.Join(c.BaseURL.Path, rel.Path)
u := c.BaseURL.ResolveReference(rel)
var buf io.ReadWriter
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
encodedParams, err := query.Values(queryParams)
if err != nil {
return nil, err
}
u.RawQuery = encodedParams.Encode()
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.UserAgent)
return req, nil
}
func (c *Client) newRequest(method, methodPath string, body interface{}) (*http.Request, error) {
rel := &url.URL{Path: methodPath}
rel.Path = path.Join(c.BaseURL.Path, rel.Path)
u := c.BaseURL.ResolveReference(rel)
var buf io.ReadWriter
if body != nil && (method == "POST" || method == "DELETE" || method == "PUT") {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
if body != nil && method == "GET" {
queryParams, _ := query.Values(body)
u.RawQuery = queryParams.Encode()
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.UserAgent)
return req, nil
}
func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return resp, buildError(resp)
}
err = json.NewDecoder(resp.Body).Decode(v)
return resp, err
}