-
Notifications
You must be signed in to change notification settings - Fork 23
/
powerpacks.go
482 lines (430 loc) · 15.6 KB
/
powerpacks.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package plivo
import (
"fmt"
"strings"
)
type PowerpackService struct {
client *Client
Powerpack
}
type Priority struct {
Priority1 *string `json:"priority1"`
Priority2 *string `json:"priority2"`
Priority3 *string `json:"priority3"`
}
type NumberPriority struct {
ServiceType string `json:"service_type"`
CountryISO string `json:"country_iso"`
Priority Priority `json:"priority"`
}
type PowerackCreateParams struct {
Name string `json:"name,omitempty"`
// Optional parameters.
StickySender string `json:"sticky_sender,omitempty"`
LocalConnect string `json:"local_connect,omitempty"`
ApplicationType string `json:"application_type,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
NumberPriorities []NumberPriority `json:"number_priority,omitempty"`
}
type PowerackUpdateParams struct {
// Optional parameters.
Name string `json:"name,omitempty"`
StickySender string `json:"sticky_sender,omitempty"`
LocalConnect string `json:"local_connect,omitempty"`
ApplicationType string `json:"application_type,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
NumberPriorities []NumberPriority `json:"number_priority,omitempty"`
}
type NumberPoolResponse struct {
Number_pool_uuid string `json:"number_pool_uuid,omitempty"`
Number string `json:"number,omitempty"`
Type string `json:"Type,omitempty"`
Country_iso2 string `json:"country_iso2,omitempty"`
Service string `json:"service,omitempty"`
Added_on string `json:"added_on,omitempty"`
Account_phone_number_resource string `json:"account_phone_number_resource,omitempty"`
}
type NumberResponse struct {
ApiID string `json:"api_id,omitempty"`
NumberPoolResponse
Error string `json:"error,omitempty"`
}
type ShortCode struct {
Number_pool_uuid string `json:"number_pool_uuid,omitempty"`
Shortcode string `json:"shortcode,omitempty"`
Country_iso2 string `json:"country_iso2,omitempty"`
Added_on string `json:"added_on,omitempty"`
Service string `json:"service,omitempty"`
}
type Tollfree struct {
NumberPoolUUID string `json:"number_pool_uuid,omitempty"`
Tollfree string `json:"number,omitempty"`
Country_iso2 string `json:"country_iso2,omitempty"`
Added_on string `json:"added_on,omitempty"`
Service string `json:"service,omitempty"`
}
type ShortCodeResponse struct {
BaseListPPKResponse
Objects []ShortCode `json:"objects" url:"objects"`
}
type TollfreeResponse struct {
BaseListPPKResponse
Objects []Tollfree `json:"objects" url:"objects"`
}
type FindShortCodeResponse struct {
ApiID string `json:"api_id,omitempty"`
ShortCode
Error string `json:"error,omitempty"`
}
type FindTollfreeResponse struct {
ApiID string `json:"api_id,omitempty"`
Tollfree
Error string `json:"error,omitempty"`
}
type Powerpack struct {
UUID string `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
StickySender bool `json:"sticky_sender,omitempty"`
LocalConnect bool `json:"local_connect,omitempty"`
ApplicationType string `json:"application_type,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
NumberPoolUUID string `json:"number_pool,omitempty"`
CreatedOn string `json:"created_on,omitempty"`
NumberPriorities []NumberPriority `json:"number_priority,omitempty"`
}
type PowerpackResponse struct {
CreatedOn string `json:"created_on,omitempty"`
LocalConnect bool `json:"local_connect,omitempty"`
Name string `json:"name,omitempty"`
NumberPoolUUID string `json:"number_pool,omitempty"`
PowerpackResourceURI string `json:"powerpack_resource_uri,omitempty"`
StickySender bool `json:"sticky_sender,omitempty"`
UUID string `json:"uuid,omitempty"`
}
type PowerpackDeleteResponse struct {
ApiID string `json:"api_id,omitempty"`
Response string `json:"response,omitempty"`
Error string `json:"error,omitempty"`
}
type NumberDeleteResponse struct {
PowerpackDeleteResponse
}
type ShortcodeDeleteResponse struct {
PowerpackDeleteResponse
}
type TollfreeDeleteResponse struct {
PowerpackDeleteResponse
}
type PowerpackDeleteParams struct {
UnrentNumbers bool `json:"unrent_numbers,omitempty"`
}
type NumberRemoveParams struct {
Unrent bool `json:"unrent,omitempty"`
}
type PowerpackAddNumberOptions struct {
//Service can be 'sms' or 'mms'. Defaults to 'sms' when not set.
Service string `json:"service,omitempty" url:"service,omitempty"`
}
type PowerpackFindNumberOptions struct {
//Service can be 'sms' or 'mms'. Defaults to 'sms' when not set.
Service string `json:"service,omitempty" url:"service,omitempty"`
}
type PowerpackCreateResponseBody struct {
ApiID string `json:"api_id,omitempty"`
Error *string `json:"error,omitempty" url:"error"`
Powerpack
}
type PowerpackUpdateResponse struct {
PowerpackCreateResponseBody
}
//powerpack list
type PowerpackList struct {
BaseListPPKResponse
Objects []Powerpack `json:"objects" url:"objects"`
}
type PowerpackListParams struct {
Limit int `url:"limit,omitempty"`
Offset int `url:"offset,omitempty"`
Service string `url:"service,omitempty"`
}
type PowerpackSearchParam struct {
Starts_with string `json:"starts_with,omitempty" url:"starts_with,omitempty"`
Country_iso2 string `json:"country_iso2,omitempty" url:"country_iso2,omitempty"`
Type string `json:"type,omitempty" url:"type,omitempty"`
Limit string `json:"limit,omitempty" url:"limit,omitempty"`
Offset string `json:"offset,omitempty" url:"offset,omitempty"`
Service string `json:"service,omitempty" url:"service,omitempty"`
}
type PowerpackPhoneResponseBody struct {
BaseListPPKResponse
Objects []NumberPoolResponse `json:"objects" url:"objects"`
}
type PPKMeta struct {
Previous *string
Next *string
TotalCount int `json:"total_count" url:"api_id"`
Offset int
Limit int
}
type BaseListPPKResponse struct {
ApiID string `json:"api_id" url:"api_id"`
Meta PPKMeta `json:"meta" url:"meta"`
}
type BuyPhoneNumberParam struct {
Number string `json:"number,omitempty"`
Country_iso2 string `json:"country_iso,omitempty"`
Type string `json:"type,omitempty"`
Region string `json:"region,omitempty"`
Pattern string `json:"pattern,omitempty"`
Service string `json:"service,omitempty"`
}
type RentNumber struct {
Rent string `json:"rent,omitempty"`
Service string `json:"service,omitempty"`
}
// returns the List.. of all powerpack
func (service *PowerpackService) List(params PowerpackListParams) (response *PowerpackList, err error) {
req, err := service.client.NewRequest("GET", params, "Powerpack/")
if err != nil {
return
}
response = &PowerpackList{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Get(powerpackUUID string) (response *PowerpackService, err error) {
req, err := service.client.NewRequest("GET", nil, "Powerpack/%s/", powerpackUUID)
if err != nil {
return
}
resp := &PowerpackCreateResponseBody{}
err = service.client.ExecuteRequest(req, resp)
if err != nil {
fmt.Println(err)
return
}
response = &PowerpackService{}
response.client = service.client
response.Powerpack = resp.Powerpack
return
}
func (service *PowerpackService) Create(params PowerackCreateParams) (response *PowerpackCreateResponseBody, err error) {
req, err := service.client.NewRequest("POST", params, "Powerpack")
if err != nil {
return
}
response = &PowerpackCreateResponseBody{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Update(params PowerackUpdateParams) (response *PowerpackUpdateResponse, err error) {
uuid := service.Powerpack.UUID
req, err := service.client.NewRequest("POST", params, "Powerpack/%s", uuid)
if err != nil {
return
}
response = &PowerpackUpdateResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Delete(params PowerpackDeleteParams) (response *PowerpackDeleteResponse, err error) {
uuid := service.Powerpack.UUID
req, err := service.client.NewRequest("DELETE", params, "Powerpack/%s", uuid)
if err != nil {
return
}
response = &PowerpackDeleteResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) List_numbers(params PowerpackSearchParam) (response *PowerpackPhoneResponseBody, err error) {
numberpool_path := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpool_path, "/")
req, err := service.client.NewRequest("GET", params, "NumberPool/%s/Number/", uriSegments[5])
if err != nil {
return
}
response = &PowerpackPhoneResponseBody{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Count_numbers(params PowerpackSearchParam) (count int, err error) {
numberpool_path := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpool_path, "/")
req, err := service.client.NewRequest("GET", params, "NumberPool/%s/Number/", uriSegments[5])
if err != nil {
return
}
response := &PowerpackPhoneResponseBody{}
err = service.client.ExecuteRequest(req, response)
if err != nil {
return
}
count = response.BaseListPPKResponse.Meta.TotalCount
return count, nil
}
func (service *PowerpackService) Find_numbers(number string) (response *NumberResponse, err error) {
params := PowerpackFindNumberOptions{}
response, err = service.FindNumbersWithOptions(number, params)
return
}
func (service *PowerpackService) FindNumbersWithOptions(number string, params PowerpackFindNumberOptions) (response *NumberResponse, err error) {
numberpool_path := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpool_path, "/")
req, err := service.client.NewRequest("GET", params, "NumberPool/%s/Number/%s/", uriSegments[5], number)
if err != nil {
return
}
response = &NumberResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Add_number(number string) (response *NumberResponse, err error) {
params := PowerpackAddNumberOptions{}
response, err = service.AddNumberWithOptions(number, params)
return
}
func (service *PowerpackService) AddNumberWithOptions(number string, params PowerpackAddNumberOptions) (response *NumberResponse, err error) {
numberpool_path := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpool_path, "/")
req, err := service.client.NewRequest("POST", params, "NumberPool/%s/Number/%s", uriSegments[5], number)
if err != nil {
return
}
response = &NumberResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Add_tollfree(tollfree string) (response *NumberResponse, err error) {
numberpoolUUID := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpoolUUID, "/")
req, err := service.client.NewRequest("POST", nil, "NumberPool/%s/Tollfree/%s", uriSegments[5], tollfree)
if err != nil {
return
}
response = &NumberResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Remove_number(number string, param NumberRemoveParams) (response *NumberDeleteResponse, err error) {
numberpoolUUID := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpoolUUID, "/")
req, err := service.client.NewRequest("DELETE", param, "NumberPool/%s/Number/%s", uriSegments[5], number)
if err != nil {
return
}
response = &NumberDeleteResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Remove_tollfree(tollfree string, param NumberRemoveParams) (response *TollfreeDeleteResponse, err error) {
numberpoolUUID := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpoolUUID, "/")
req, err := service.client.NewRequest("DELETE", param, "NumberPool/%s/Tollfree/%s", uriSegments[5], tollfree)
if err != nil {
return
}
response = &TollfreeDeleteResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Remove_shortcode(shortcode string) (response *ShortcodeDeleteResponse, err error) {
numberpoolUUID := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpoolUUID, "/")
req, err := service.client.NewRequest("DELETE", nil, "NumberPool/%s/Shortcode/%s", uriSegments[5], shortcode)
if err != nil {
return
}
response = &ShortcodeDeleteResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) List_shortcodes() (response *ShortCodeResponse, err error) {
numberpool_path := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpool_path, "/")
req, err := service.client.NewRequest("GET", nil, "NumberPool/%s/Shortcode", uriSegments[5])
if err != nil {
return
}
response = &ShortCodeResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) List_tollfree() (response *TollfreeResponse, err error) {
numberpoolUUID := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpoolUUID, "/")
req, err := service.client.NewRequest("GET", nil, "NumberPool/%s/Tollfree", uriSegments[5])
if err != nil {
return
}
response = &TollfreeResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Find_shortcode(shortcode string) (response *FindShortCodeResponse, err error) {
numberpool_path := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpool_path, "/")
req, err := service.client.NewRequest("GET", nil, "NumberPool/%s/Shortcode/%s/", uriSegments[5], shortcode)
if err != nil {
return
}
response = &FindShortCodeResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Find_tollfree(tollfree string) (response *FindTollfreeResponse, err error) {
numberpoolUUID := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpoolUUID, "/")
req, err := service.client.NewRequest("GET", nil, "NumberPool/%s/Tollfree/%s/", uriSegments[5], tollfree)
if err != nil {
return
}
response = &FindTollfreeResponse{}
err = service.client.ExecuteRequest(req, response)
return
}
func (service *PowerpackService) Buy_add_number(phoneParam BuyPhoneNumberParam) (response *NumberResponse, err error) {
numberpool_path := service.Powerpack.NumberPoolUUID
uriSegments := strings.Split(numberpool_path, "/")
payload := RentNumber{
Rent: "true",
Service: phoneParam.Service,
}
number := phoneParam.Number
if number != "" {
req, reqErr := service.client.NewRequest("POST", payload, "NumberPool/%s/Number/%s", uriSegments[5], number)
if reqErr != nil {
return nil, reqErr
}
response = &NumberResponse{}
err = service.client.ExecuteRequest(req, response)
} else {
Type := phoneParam.Type
region := phoneParam.Region
pattern := phoneParam.Pattern
countryiso := phoneParam.Country_iso2
serviceType := phoneParam.Service
params := PhoneNumberListParams{
Type: Type,
Region: region,
Pattern: pattern,
CountryISO: countryiso,
Services: serviceType,
}
responsephoneNo, er := service.client.PhoneNumbers.List(params)
if er != nil {
return nil, er
}
if len(responsephoneNo.Objects) < 1 {
response = &NumberResponse{}
return response, nil
}
req, reqErr := service.client.NewRequest("POST", payload, "NumberPool/%s/Number/%s", uriSegments[5], responsephoneNo.Objects[0].Number)
if reqErr != nil {
return nil, reqErr
}
response = &NumberResponse{}
err = service.client.ExecuteRequest(req, response)
}
return
}