-
Notifications
You must be signed in to change notification settings - Fork 11
/
crisp.go
313 lines (243 loc) · 7.29 KB
/
crisp.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
// Copyright 2018 Crisp IM SAS All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package crisp
import (
"bytes"
"encoding/json"
"fmt"
"time"
"io"
"errors"
"io/ioutil"
"net/http"
"net/url"
)
const (
libraryVersion = "3.45.0"
defaultRestEndpointURL = "https://api.crisp.chat/v1/"
userAgent = "go-crisp-api/" + libraryVersion
acceptContentType = "application/json"
clientTimeout = 10
clientIdleConnTimeout = 45
clientMaxIdleConns = 16
clientMaxConnsPerHost = 64
clientMaxIdleConnsPerHost = 4
clientRequestRetryAttempts = 2
clientRequestRetryHoldMillis = 1000
)
var errorDoAllAttemptsExhausted = errors.New("all request attempts were exhausted")
var errorDoAttemptNilRequest = errors.New("request could not be constructed")
// ClientConfig mapping
type ClientConfig struct {
HTTPClient *http.Client
HTTPHeaders *map[string]string
RestEndpointURL string
}
type auth struct {
Available bool
Username string
Password string
Tier string
}
// Client maps an API client
type Client struct {
client *http.Client
headers *map[string]string
auth *auth
BaseURL *url.URL
UserAgent string
common service
Bucket *BucketService
Media *MediaService
Plugin *PluginService
Website *WebsiteService
Events *EventsService
}
type service struct {
client *Client
}
// Response maps an API HTTP response
type Response struct {
*http.Response
}
type errorResponse struct {
Response *http.Response
Reason string `json:"reason,omitempty"`
}
// Error prints an error response
func (response *errorResponse) Error() string {
return fmt.Sprintf("%v %v: %d %v",
response.Response.Request.Method, response.Response.Request.URL,
response.Response.StatusCode, response.Reason)
}
// NewWithConfig returns a new API client
func NewWithConfig(config ClientConfig) *Client {
// Defaults
if config.HTTPClient == nil {
// Build client transport
clientTransport := http.DefaultTransport.(*http.Transport).Clone()
clientTransport.IdleConnTimeout = time.Duration(clientIdleConnTimeout * time.Second)
clientTransport.MaxIdleConns = clientMaxIdleConns
clientTransport.MaxConnsPerHost = clientMaxConnsPerHost
clientTransport.MaxIdleConnsPerHost = clientMaxIdleConnsPerHost
// Create client
config.HTTPClient = &http.Client{
Timeout: time.Duration(clientTimeout * time.Second),
Transport: clientTransport,
}
}
if config.RestEndpointURL == "" {
config.RestEndpointURL = defaultRestEndpointURL
}
// Create client
baseURL, _ := url.Parse(config.RestEndpointURL)
client := &Client{client: config.HTTPClient, headers: config.HTTPHeaders, auth: &auth{}, BaseURL: baseURL, UserAgent: userAgent}
client.common.client = client
// Map services
client.Bucket = (*BucketService)(&client.common)
client.Media = (*MediaService)(&client.common)
client.Plugin = (*PluginService)(&client.common)
client.Website = (*WebsiteService)(&client.common)
client.Events = (*EventsService)(&client.common)
return client
}
// New returns a new API client
func New() *Client {
return NewWithConfig(ClientConfig{})
}
// AuthenticateTier saves authentication parameters for tier
func (client *Client) AuthenticateTier(tier string, username string, password string) {
client.auth.Tier = tier
client.auth.Username = username
client.auth.Password = password
client.auth.Available = true
}
// Authenticate saves authentication parameters for user (default)
func (client *Client) Authenticate(username string, password string) {
client.AuthenticateTier("user", username, password)
}
// NewRequest creates an API request
func (client *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
url := client.BaseURL.ResolveReference(rel)
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, url.String(), buf)
if err != nil {
return nil, err
}
// Append custom headers? (if any)
if client.headers != nil {
for headerKey, headerValue := range *client.headers {
req.Header.Add(headerKey, headerValue)
}
}
// Append authorization header? (if authenticated)
if client.auth.Available == true {
req.SetBasicAuth(client.auth.Username, client.auth.Password)
req.Header.Add("X-Crisp-Tier", client.auth.Tier)
}
// Append common headers
req.Header.Add("Accept", acceptContentType)
req.Header.Add("Content-Type", acceptContentType)
// Append user agent header? (if any)
if client.UserAgent != "" {
req.Header.Add("User-Agent", client.UserAgent)
}
return req, nil
}
// Do sends an API request
func (client *Client) Do(req *http.Request, v interface{}) (*Response, error) {
var lastErr error
attempts := 0
for attempts < clientRequestRetryAttempts {
// Hold before this attempt? (ie. not first attempt)
if attempts > 0 {
time.Sleep(clientRequestRetryHoldMillis * time.Millisecond)
}
// Dispatch request attempt
attempts++
resp, shouldRetry, err := client.doAttempt(req, v)
// Return response straight away? (we are done)
if shouldRetry == false {
return resp, err
}
// Should retry: store last error (we are not done)
lastErr = err
}
// Set default error? (all attempts failed, but no error is set)
if lastErr == nil {
lastErr = errorDoAllAttemptsExhausted
}
// All attempts failed, return last attempt error
return nil, lastErr
}
// doAttempt attempts an API request
func (client *Client) doAttempt(req *http.Request, v interface{}) (*Response, bool, error) {
if req == nil {
return nil, false, errorDoAttemptNilRequest
}
resp, err := client.client.Do(req)
if checkRequestRetry(resp, err) == true {
return nil, true, err
}
defer func() {
io.CopyN(ioutil.Discard, resp.Body, 512)
resp.Body.Close()
}()
response := newResponse(resp)
err = checkResponse(resp)
if err != nil {
return response, false, err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
io.Copy(w, resp.Body)
} else {
err = json.NewDecoder(resp.Body).Decode(v)
if err == io.EOF {
err = nil
}
}
}
return response, false, err
}
// newResponse creates an HTTP response
func newResponse(httpResponse *http.Response) *Response {
response := &Response{Response: httpResponse}
return response
}
// checkRequestRetry checks if should retry request
func checkRequestRetry(response *http.Response, err error) bool {
// Low-level error, or response status is a server error? (HTTP 5xx)
if err != nil || response.StatusCode >= 500 {
return true
}
// No low-level error (should not retry)
return false
}
// checkResponse checks response for errors
func checkResponse(response *http.Response) error {
// No error in response? (HTTP 2xx)
if code := response.StatusCode; 200 <= code && code <= 299 {
return nil
}
// Map response error data (eg. HTTP 4xx)
errorResponse := &errorResponse{Response: response}
data, err := ioutil.ReadAll(response.Body)
if err == nil && data != nil {
json.Unmarshal(data, errorResponse)
}
return errorResponse
}