-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocbase.go
313 lines (266 loc) · 7.54 KB
/
docbase.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package docbase
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
)
const (
defaultBaseURL = "https://api.docbase.io/teams/%s"
apiVersion = "2"
userAgent = "DocBase Go" + version
// https://help.docbase.io/posts/45703#利用制限
headerRateLimit = "X-RateLimit-Limit"
headerRateRemaining = "X-RateLimit-Remaining"
headerRateReset = "X-RateLimit-Reset"
)
const (
publicScope = "public"
groupScope = "group"
privateScope = "private"
)
type Client struct {
BaseURL *url.URL
AccessToken string
Team string
Client *http.Client
rateMu sync.Mutex
rateLimit Rate
Posts PostService
Users UserService
Groups GroupService
GroupUsers GroupUserService
Tags TagService
Comments CommentService
Attachments AttachmentService
}
// Response is http response wrapper for DocBase
type Response struct {
*http.Response
Rate
Meta
}
func newResponse(r *http.Response) *Response {
res := &Response{Response: r}
res.Rate = parseRate(r)
return res
}
// NewClient returns client API
func NewClient(httpClient *http.Client, team, token string) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
baseURL, err := url.Parse(fmt.Sprintf(defaultBaseURL, team))
if err != nil {
log.Fatal(err)
}
cli := &Client{
AccessToken: token,
Team: team,
Client: httpClient,
BaseURL: baseURL,
}
cli.Posts = &postService{cli}
cli.Groups = &groupService{cli}
cli.Users = &userService{cli}
cli.Comments = &commentService{cli}
cli.Tags = &tagService{cli}
cli.Attachments = &attachmentService{cli}
cli.GroupUsers = &groupUserService{cli}
return cli
}
// NewRequest creates a API request with HTTP method, endpoint path and payload
func (c *Client) NewRequest(method, path string, body interface{}) (*http.Request, error) {
u, err := url.Parse(fmt.Sprintf("%s%s", c.BaseURL.String(), path))
if err != nil {
return nil, err
}
buf, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-DocBaseToken", c.AccessToken)
req.Header.Add("X-Api-Version", apiVersion)
req.Header.Add("USER_AGENT", userAgent)
return req, nil
}
// Do sends request and returns API response
func (c *Client) Do(r *http.Request, v interface{}) (*Response, error) {
if err := c.checkRateLimitBeforeDo(r); err != nil {
return &Response{
Response: err.Response,
Rate: err.Rate,
}, err
}
resp, err := c.Client.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
response := newResponse(resp)
err = CheckResponse(resp)
if err != nil {
return response, err
}
if v == nil {
return response, nil
}
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return response, err
}
return response, nil
}
func (c *Client) DoUpload(r *http.Request) (FileContent, *Response, error) {
resp, err := c.Client.Do(r)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
response := newResponse(resp)
err = CheckResponse(resp)
if err != nil {
return nil, response, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, response, err
}
return body, response, nil
}
// CheckResponse checks response for errors
func CheckResponse(r *http.Response) error {
if c := r.StatusCode; c == http.StatusOK || c == http.StatusCreated || c == http.StatusNoContent {
return nil
}
errorResponse := &ErrorResponse{Response: r}
data, err := ioutil.ReadAll(r.Body)
if err == nil && data != nil {
err = json.Unmarshal(data, errorResponse)
if err != nil {
errorResponse.err = fmt.Errorf("not json structure")
}
}
switch r.StatusCode {
case http.StatusTooManyRequests:
return &RateLimitError{
Rate: parseRate(r),
Response: errorResponse.Response,
Messages: errorResponse.Messages,
}
default:
return errorResponse
}
}
// parseRate referenced from https://github.com/google/go-github/blob/master/github/github.go#L495
func parseRate(r *http.Response) Rate {
var (
rate Rate
err error
)
if limit := r.Header.Get(headerRateLimit); limit != "" {
rate.Limit, err = strconv.Atoi(limit)
if err != nil {
rate.err = err
}
}
if remaining := r.Header.Get(headerRateRemaining); remaining != "" {
rate.Remaining, err = strconv.Atoi(remaining)
if err != nil {
rate.err = err
}
}
if reset := r.Header.Get(headerRateReset); reset != "" {
v, e := strconv.ParseInt(reset, 10, 64)
if e != nil {
rate.err = e
} else if v != 0 {
rate.Reset = Timestamp{time.Unix(v, 0)}
}
}
return rate
}
// checkRateLimitBeforeDo referenced from https://github.com/google/go-github/blob/master/github/github.go#L627
func (c *Client) checkRateLimitBeforeDo(req *http.Request) *RateLimitError {
c.rateMu.Lock()
rate := c.rateLimit
c.rateMu.Unlock()
if !rate.Reset.Time.IsZero() && rate.Remaining == 0 && time.Now().Before(rate.Reset.Time) {
// Create a fake response.
resp := &http.Response{
Status: http.StatusText(http.StatusForbidden),
StatusCode: http.StatusForbidden,
Request: req,
Header: make(http.Header),
Body: ioutil.NopCloser(strings.NewReader("")),
}
return &RateLimitError{
Rate: rate,
Response: resp,
Messages: []string{fmt.Sprintf("API rate limit of %v still exceeded until %v, not making remote request.", rate.Limit, rate.Reset.Time)},
}
}
return nil
}
// Rate referenced from https://github.com/google/go-github/blob/master/github/github.go#L861
type Rate struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset Timestamp `json:"reset"`
err error
}
// RateLimitError referenced from https://github.com/google/go-github/blob/master/github/github.go#L687
type RateLimitError struct {
Rate Rate // Rate specifies last known rate limit for the client
Response *http.Response // HTTP response that caused this error
Messages []string `json:"message"` // error message
}
type Meta struct {
PreviousPage string `json:"previous_page"`
NextPage string `json:"next_page"`
Total int `json:"total"`
}
// Error referenced from https://github.com/google/go-github/blob/master/github/github.go#L693
func (r *RateLimitError) Error() string {
return fmt.Sprintf("%v %v: %d %v %v",
r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
r.Response.StatusCode, r.Messages, r.Rate.Reset.Time.Sub(time.Now()))
}
// sanitizeURL referenced from https://github.com/google/go-github/blob/master/github/github.go#L734
func sanitizeURL(uri *url.URL) *url.URL {
if uri == nil {
return nil
}
params := uri.Query()
if len(params.Get("client_secret")) > 0 {
params.Set("client_secret", "REDACTED")
uri.RawQuery = params.Encode()
}
return uri
}
// ErrorResponse referenced from https://github.com/google/go-github/blob/master/github/github.go#L655
type ErrorResponse struct {
Response *http.Response // HTTP response that caused this error
Messages []string `json:"messages"` // error message
ErrorStr string `json:"error"` // more detail about an error
err error // CheckResponse error
}
// ErrorResponse referenced from https://github.com/google/go-github/blob/master/github/github.go#L655
func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%v %v: %d %v %+v",
r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
r.Response.StatusCode, r.Messages, r.ErrorStr)
}