This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
number.go
372 lines (296 loc) · 10.3 KB
/
number.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
package vonage
import (
"context"
"encoding/json"
"github.com/antihax/optional"
"github.com/vonage/vonage-go-sdk/internal/number"
)
// NumbersClient for working with the Numbers API
type NumbersClient struct {
Config *number.Configuration
apiKey string
apiSecret string
}
// NewNumbersClient Creates a new Numbers Client, supplying an Auth to work with
func NewNumbersClient(Auth Auth) *NumbersClient {
client := new(NumbersClient)
creds := Auth.GetCreds()
client.apiKey = creds[0]
client.apiSecret = creds[1]
// Use a default set of config but make it accessible
client.Config = number.NewConfiguration()
client.Config.UserAgent = GetUserAgent()
transport := &APITransport{APISecret: client.apiSecret}
client.Config.HTTPClient = transport.Client()
return client
}
// NumbersResponse is the response format for the Numbers API
type NumbersResponse struct {
ErrorCode string `json:"error-code,omitempty"`
ErrorCodeLabel string `json:"error-code-label,omitempty"`
}
// NumbersErrorResponse is the error format for the Numbers API
type NumbersErrorResponse struct {
ErrorCode string `json:"error-code,omitempty"`
ErrorCodeLabel string `json:"error-code-label,omitempty"`
}
// NumbersOpts sets the options to use in finding the numbers already in the user's account
type NumbersOpts struct {
ApplicationID string
HasApplication string // string because it's tri-state, not boolean
Country string
Pattern string
SearchPattern int32
Size int32
Index int32
}
type NumberDetail struct {
Country string
Msisdn string
MoHttpUrl string
Type string
Features []string
MessagesCallbackType string
MessagesCallbackValue string
VoiceCallbackType string
VoiceCallbackValue string
}
type NumberCollection struct {
Count int32
Numbers []NumberDetail
}
// List shows the numbers you already own, filters and pagination are available
func (client *NumbersClient) List(opts NumbersOpts) (NumberCollection, NumbersErrorResponse, error) {
numbersClient := number.NewAPIClient(client.Config)
// set up the options and parse them
numbersOpts := number.GetOwnedNumbersOpts{}
if opts.ApplicationID != "" {
numbersOpts.ApplicationId = optional.NewString(opts.ApplicationID)
}
if opts.HasApplication != "" {
// if it's set at all, use the value and set it
if opts.HasApplication == "true" {
numbersOpts.HasApplication = optional.NewBool(true)
} else if opts.HasApplication == "false" {
numbersOpts.HasApplication = optional.NewBool(false)
}
}
if opts.Country != "" {
numbersOpts.Country = optional.NewString(opts.Country)
}
if opts.Pattern != "" {
numbersOpts.Pattern = optional.NewString(opts.Pattern)
}
if opts.SearchPattern != 0 {
numbersOpts.SearchPattern = optional.NewInt32(opts.SearchPattern)
}
if opts.Size != 0 {
numbersOpts.Size = optional.NewInt32(opts.Size)
}
if opts.Index != 0 {
numbersOpts.Index = optional.NewInt32(opts.Index)
}
// we need context for the API key
ctx := context.WithValue(context.Background(), number.ContextAPIKey, number.APIKey{
Key: client.apiKey,
})
result, _, err := numbersClient.DefaultApi.GetOwnedNumbers(ctx, &numbersOpts)
if err != nil {
e := err.(number.GenericOpenAPIError)
data := e.Body()
var errResp NumbersErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return NumberCollection{}, errResp, err
}
return NumberCollection{}, NumbersErrorResponse{}, err
}
// deep-convert the numbers collection
var collection NumberCollection
var numbers []NumberDetail
for _, num := range result.Numbers {
numbers = append(numbers, NumberDetail(num))
}
collection.Numbers = numbers
collection.Count = result.Count
return collection, NumbersErrorResponse{}, nil
}
// NumberSearchOpts sets the optional values in the Search method
type NumberSearchOpts struct {
Type string
Features string
Pattern string
SearchPattern int32
Size int32
Index int32
}
type NumberSearch struct {
Count int32
Numbers []NumberAvailable
}
type NumberAvailable struct {
Country string
Msisdn string
Type string
Cost string
Features []string
}
// Search lets you find a great phone number to use in your application
func (client *NumbersClient) Search(country string, opts NumberSearchOpts) (NumberSearch, NumbersErrorResponse, error) {
numbersClient := number.NewAPIClient(client.Config)
// we need context for the API key
ctx := context.WithValue(context.Background(), number.ContextAPIKey, number.APIKey{
Key: client.apiKey,
})
numbersSearchOpts := number.GetAvailableNumbersOpts{}
if opts.Type != "" {
numbersSearchOpts.Type_ = optional.NewString(opts.Type)
}
if opts.Features != "" {
numbersSearchOpts.Features = optional.NewString(opts.Features)
}
if opts.Pattern != "" {
numbersSearchOpts.Pattern = optional.NewString(opts.Pattern)
}
if opts.SearchPattern != 0 {
numbersSearchOpts.SearchPattern = optional.NewInt32(opts.SearchPattern)
}
if opts.Size != 0 {
numbersSearchOpts.Size = optional.NewInt32(opts.Size)
}
if opts.Index != 0 {
numbersSearchOpts.Index = optional.NewInt32(opts.Index)
}
result, _, err := numbersClient.DefaultApi.GetAvailableNumbers(ctx, country, &numbersSearchOpts)
if err != nil {
return NumberSearch{}, NumbersErrorResponse{}, err
}
// deep-convert the numbers collection
var collection NumberSearch
var numbers []NumberAvailable
for _, num := range result.Numbers {
numbers = append(numbers, NumberAvailable(num))
}
collection.Numbers = numbers
collection.Count = result.Count
return collection, NumbersErrorResponse{}, nil
}
// NumberBuyOpts enables users to set the Target API Key (and any future params)
type NumberBuyOpts struct {
TargetAPIKey string
}
// Buy the best phone number to use in your app
func (client *NumbersClient) Buy(country string, msisdn string, opts NumberBuyOpts) (NumbersResponse, NumbersErrorResponse, error) {
numbersClient := number.NewAPIClient(client.Config)
// we need context for the API key
ctx := context.WithValue(context.Background(), number.ContextAPIKey, number.APIKey{
Key: client.apiKey,
})
numbersBuyOpts := number.BuyANumberOpts{}
if opts.TargetAPIKey != "" {
numbersBuyOpts.TargetApiKey = optional.NewString(opts.TargetAPIKey)
}
result, resp, err := numbersClient.DefaultApi.BuyANumber(ctx, country, msisdn, &numbersBuyOpts)
// check for non-200 status codes first, err will be set but we handle these specifically
if resp.StatusCode != 200 {
// handle a 4xx error
e := err.(number.GenericOpenAPIError)
data := e.Body()
var errResp NumbersErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
if errResp.ErrorCode == "420" && errResp.ErrorCodeLabel == "method failed" {
errResp.ErrorCodeLabel = "method failed. This can also indicate that you already own this number"
}
return NumbersResponse(result), errResp, nil
}
}
if err != nil {
return NumbersResponse{}, NumbersErrorResponse{}, err
}
return NumbersResponse(result), NumbersErrorResponse{}, nil
}
// NumberCancelOpts enables users to set the Target API Key (and any future params)
type NumberCancelOpts struct {
TargetAPIKey string
}
// Cancel a number already in your account
func (client *NumbersClient) Cancel(country string, msisdn string, opts NumberCancelOpts) (NumbersResponse, NumbersErrorResponse, error) {
numbersClient := number.NewAPIClient(client.Config)
// we need context for the API key
ctx := context.WithValue(context.Background(), number.ContextAPIKey, number.APIKey{
Key: client.apiKey,
})
numbersCancelOpts := number.CancelANumberOpts{}
if opts.TargetAPIKey != "" {
numbersCancelOpts.TargetApiKey = optional.NewString(opts.TargetAPIKey)
}
result, resp, err := numbersClient.DefaultApi.CancelANumber(ctx, country, msisdn, &numbersCancelOpts)
if resp.StatusCode != 200 {
// handle a 4xx error
e := err.(number.GenericOpenAPIError)
data := e.Body()
var errResp NumbersErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
if errResp.ErrorCode == "420" && errResp.ErrorCodeLabel == "method failed" {
// expand on this error code, it's commonly because you don't own the number
errResp.ErrorCodeLabel = "method failed. This can also indicate that the number is not associated with this key"
}
return NumbersResponse(result), errResp, nil
}
}
if err != nil {
return NumbersResponse{}, NumbersErrorResponse{}, err
}
return NumbersResponse(result), NumbersErrorResponse{}, nil
}
// NumberUpdateOpts sets all the various fields for the number config
type NumberUpdateOpts struct {
AppID string
MoHTTPURL string
VoiceCallbackType string
VoiceCallbackValue string
VoiceStatusCallback string
MessagesCallbackType string
MessagesCallbackValue string
}
// Update the configuration for your number
func (client *NumbersClient) Update(country string, msisdn string, opts NumberUpdateOpts) (NumbersResponse, NumbersErrorResponse, error) {
numbersClient := number.NewAPIClient(client.Config)
// we need context for the API key
ctx := context.WithValue(context.Background(), number.ContextAPIKey, number.APIKey{
Key: client.apiKey,
})
numbersUpdateOpts := number.UpdateANumberOpts{}
if opts.AppID != "" {
numbersUpdateOpts.AppId = optional.NewString(opts.AppID)
}
if opts.MoHTTPURL != "" {
numbersUpdateOpts.MoHttpUrl = optional.NewString(opts.MoHTTPURL)
}
if opts.VoiceCallbackType != "" {
numbersUpdateOpts.VoiceCallbackType = optional.NewString(opts.VoiceCallbackType)
}
if opts.VoiceCallbackValue != "" {
numbersUpdateOpts.VoiceCallbackValue = optional.NewString(opts.VoiceCallbackValue)
}
if opts.VoiceStatusCallback != "" {
numbersUpdateOpts.VoiceStatusCallback = optional.NewString(opts.VoiceStatusCallback)
}
result, resp, err := numbersClient.DefaultApi.UpdateANumber(ctx, country, msisdn, &numbersUpdateOpts)
if err != nil {
return NumbersResponse{}, NumbersErrorResponse{}, err
}
if resp.StatusCode != 200 {
// handle a 4xx error
e := err.(number.GenericOpenAPIError)
data := e.Body()
var errResp NumbersErrorResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return NumbersResponse(result), errResp, nil
}
}
return NumbersResponse(result), NumbersErrorResponse{}, nil
}