forked from joaoh82/aircall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
330 lines (246 loc) · 6.83 KB
/
api.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
323
324
325
326
327
328
329
330
package aircall
import (
"encoding/json"
"fmt"
"strconv"
)
// Ping API request
func (client *Client) Ping() (*PingResponse, error) {
data, err := client.Get("/ping", map[string]string{})
if err != nil {
return nil, err
}
response := &PingResponse{}
json.Unmarshal(data, &response)
return response, nil
}
// Company API request
//
// res, err := client.Company()
func (client *Client) Company() (CompanyResponse, error) {
data, err := client.Get("/company", map[string]string{})
response := CompanyResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// Users API request
func (client *Client) Users(paginate Paginate) (UsersResponse, error) {
params := buildPaginateParamsMap(paginate)
fmt.Println("%v", params)
data, err := client.Get("/users", params)
response := UsersResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// User API request
func (client *Client) User(ID int) (UserResponse, error) {
data, err := client.Get("/users/"+strconv.Itoa(ID), map[string]string{})
response := UserResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// Numbers API request
func (client *Client) Numbers(paginate Paginate) (NumbersResponse, error) {
params := buildPaginateParamsMap(paginate)
data, err := client.Get("/numbers", params)
response := NumbersResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// Number API request
func (client *Client) Number(ID int) (NumberResponse, error) {
data, err := client.Get("/numbers/"+strconv.Itoa(ID), map[string]string{})
response := NumberResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// Calls API request
func (client *Client) Calls(paginate Paginate) (CallsResponse, error) {
params := buildPaginateParamsMap(paginate)
data, err := client.Get("/calls", params)
response := CallsResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// Call API request
func (client *Client) Call(ID int) (CallResponse, error) {
data, err := client.Get("/calls/"+strconv.Itoa(ID), map[string]string{})
response := CallResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// SearchCalls API request
func (client *Client) SearchCalls(paginate Paginate, search Search) (CallsResponse, error) {
params := buildPaginateParamsMap(paginate)
searchParams := buildSearchParamsMap(search)
for k, v := range searchParams {
params[k] = v
}
data, err := client.Get("/calls", params)
response := CallsResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// LinkCall API Request
func (client *Client) LinkCall(ID int, link string) (CallResponse, error) {
data, err := client.Post("/calls/"+strconv.Itoa(ID)+"/link", LinkCallRequest{
Link: link,
})
response := CallResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// TransferCall API request
func (client *Client) TransferCall(ID int, userID int) (CallResponse, error) {
data, err := client.Post("/calls/"+strconv.Itoa(ID)+"/transfers", TransferCallRequest{
UserID: userID,
})
response := CallResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// DeleteRecording API request
func (client *Client) DeleteRecording(callID int) (Response, error) {
data, err := client.Delete("/calls/"+strconv.Itoa(callID)+"/recording", map[string]string{})
response := Response{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// DeleteVoicemail API request
func (client *Client) DeleteVoicemail(callID int) (Response, error) {
data, err := client.Delete("/calls/"+strconv.Itoa(callID)+"/voicemail", map[string]string{})
response := Response{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// Contacts API request
func (client *Client) Contacts(paginate Paginate) (ContactsResponse, error) {
params := buildPaginateParamsMap(paginate)
data, err := client.Get("/contacts", params)
response := ContactsResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// Contact API request
func (client *Client) Contact(ID int) (ContactResponse, error) {
data, err := client.Get("/contacts/"+strconv.Itoa(ID), map[string]string{})
response := ContactResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// CreateContact API request
func (client *Client) CreateContact(contact ContactRequest) (ContactResponse, error) {
data, err := client.Post("/contacts", contact)
response := ContactResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// UpdateContact API request
func (client *Client) UpdateContact(ID int, contact ContactRequest) (ContactResponse, error) {
data, err := client.Post("/contacts/"+strconv.Itoa(ID), contact)
response := ContactResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// SearchContacts API request
func (client *Client) SearchContacts(paginate Paginate, search Search) (ContactsResponse, error) {
params := buildPaginateParamsMap(paginate)
searchParams := buildSearchParamsMap(search)
for k, v := range searchParams {
params[k] = v
}
data, err := client.Get("/contacts/search", params)
response := ContactsResponse{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
// DeleteContact API request
func (client *Client) DeleteContact(ID int) (Response, error) {
data, err := client.Delete("/contacts/"+strconv.Itoa(ID), map[string]string{})
response := Response{}
if err != nil {
return response, err
}
json.Unmarshal(data, &response)
return response, nil
}
func buildSearchParamsMap(search Search) map[string]string {
params := make(map[string]string)
if search.PhoneNumber != "" {
params["phone_number"] = search.PhoneNumber
}
if search.Email != "" {
params["email"] = search.Email
}
return params
}
func buildPaginateParamsMap(paginate Paginate) map[string]string {
params := make(map[string]string)
if paginate.Page > 0 {
params["page"] = strconv.Itoa(paginate.Page)
}
if paginate.PerPage > 0 {
params["per_page"] = strconv.Itoa(paginate.PerPage)
}
if paginate.Order != "" {
params["order"] = paginate.Order
}
if paginate.From > 0 {
params["from"] = strconv.Itoa(paginate.From)
}
if paginate.To > 0 {
params["to"] = strconv.Itoa(paginate.To)
}
return params
}