-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaircall.go
322 lines (254 loc) · 7.96 KB
/
aircall.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
314
315
316
317
318
319
320
321
322
package aircall
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
const (
libraryVersion = "1.1"
defaultAuthHeaderName = "Authorization"
defaultOAuthPrefix = "Bearer"
defaultAuthPrefix = "Basic"
defaultRestEndpointURL = "https://api.aircall.io/v1/"
acceptedContentType = "application/json"
userAgent = "go-aircall-api/" + libraryVersion
clientRequestRetryAttempts = 2
clientRequestRetryHoldMillis = 1000
)
var (
errorDoAllAttemptsExhausted = errors.New("all request attempts were exhausted")
errorDoAttemptNilRequest = errors.New("request could not be constructed")
)
type ApiType string
type ClientConfig struct {
HttpClient *http.Client
RestEndpointURL string
}
type auth struct {
Available bool
AccessToken string
HeaderName string
Prefix string
}
type Client struct {
config *ClientConfig
client *http.Client
auth *auth
baseURL *url.URL
A2PCampaignAssociations *A2PCampaignAssociationsService
Call *CallsService
Contact *ContactsService
ConversationIntelligence *ConversationIntelligenceService
Company *CompaniesService
Integration *IntegrationService
DialerCampaign *DialerCampaignsService
Number *NumbersService
Message *MessagesService
User *UsersService
Tag *TagsService
Team *TeamsService
Webhook *WebhookService
}
type service struct {
client *Client
}
type ErrorResponse struct {
Response *http.Response
Message string `json:"message,omitempty"`
ErrorMessage string `json:"error,omitempty"`
Troubleshoot string `json:"troubleshoot,omitempty"`
Success bool `json:"success,omitempty"`
}
type Response struct {
*http.Response
}
func (response *ErrorResponse) Error() string {
errorString := fmt.Sprintf("%v %v: %d %s",
response.Response.Request.Method, response.Response.Request.URL,
response.Response.StatusCode, response.ErrorMessage)
if response.Troubleshoot != "" {
errorString = fmt.Sprintf("%s [%s]", errorString, response.Troubleshoot)
}
if response.Message != "" {
errorString = fmt.Sprintf("%s [%s]", errorString, response.Message)
}
return errorString
}
func NewWithConfig(config ClientConfig) *Client {
if config.HttpClient == nil {
config.HttpClient = http.DefaultClient
}
if config.RestEndpointURL == "" {
config.RestEndpointURL = defaultRestEndpointURL
}
// Create client
baseURL, _ := url.Parse(config.RestEndpointURL)
client := &Client{config: &config, client: config.HttpClient, auth: &auth{}, baseURL: baseURL}
// Map services
client.A2PCampaignAssociations = &A2PCampaignAssociationsService{client: client}
client.Call = &CallsService{client: client}
client.Contact = &ContactsService{client: client}
client.ConversationIntelligence = &ConversationIntelligenceService{client: client}
client.Company = &CompaniesService{client: client}
client.DialerCampaign = &DialerCampaignsService{client: client}
client.Integration = &IntegrationService{client: client}
client.Number = &NumbersService{client: client}
client.Message = &MessagesService{client: client}
client.Tag = &TagsService{client: client}
client.Team = &TeamsService{client: client}
client.User = &UsersService{client: client}
client.Webhook = &WebhookService{client: client}
return client
}
func New() *Client {
return NewWithConfig(ClientConfig{})
}
func (client *Client) Authenticate(accessToken string) {
client.auth.HeaderName = defaultAuthHeaderName
client.auth.AccessToken = accessToken
client.auth.Available = true
client.auth.Prefix = defaultOAuthPrefix
}
func (client *Client) AuthenricateBasic(apiID string, apiToken string) {
client.auth.HeaderName = defaultAuthHeaderName
client.auth.Available = true
client.auth.Prefix = defaultAuthPrefix
client.auth.AccessToken = base64.StdEncoding.EncodeToString([]byte(apiID + ":" + apiToken))
}
// NewRequest creates an API request
func (client *Client) NewRequest(method, urlStr string, opts interface{}, body interface{}) (*http.Request, error) {
// Append Query Params to URL
if opts, ok := isPointerWithQueryValues(opts); ok {
if v, ok := opts.(QueryValues); ok {
urlStr += v.getQueryValues().encode()
}
}
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
}
if client.auth.Available {
req.Header.Add(client.auth.HeaderName, fmt.Sprintf("%s %s", client.auth.Prefix, client.auth.AccessToken))
}
req.Header.Add("Accept", acceptedContentType)
req.Header.Add("Content-type", acceptedContentType)
req.Header.Add("User-Agent", 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 {
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
}
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) {
return nil, true, err
}
defer 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
}
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 := io.ReadAll(response.Body)
if err == nil && data != nil {
_ = json.Unmarshal(data, errorResponse)
}
return errorResponse
}
func (client *Client) Get(url string, opts interface{}, v interface{}) (*Response, error) {
req, _ := client.NewRequest("GET", url, opts, nil)
return client.Do(req, v)
}
func (client *Client) Post(url string, body interface{}, v interface{}) (*Response, error) {
req, _ := client.NewRequest("POST", url, nil, body)
return client.Do(req, v)
}
func (client *Client) Put(url string, body interface{}, v interface{}) (*Response, error) {
req, _ := client.NewRequest("PUT", url, nil, body)
return client.Do(req, v)
}
func (client *Client) Delete(url string, v ...interface{}) (*Response, error) {
req, _ := client.NewRequest("DELETE", url, nil, nil)
if len(v) > 0 {
return client.Do(req, v[0])
}
return client.Do(req, nil)
}