forked from njern/gonexmo
/
number.go
211 lines (168 loc) · 5.65 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
package nexmo
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
)
// Numbers represents the number management API functions
type Numbers struct {
client *Client
}
// Type NumberSearchOptions defines options for filtering when searching for available numbers to purchase
type NumberSearchOptions struct {
Pattern string
SearchPattern string
}
// Type NumberSearchResponse represents a set of phone number available for purchase, and their count
type NumberSearchResponse struct {
Count int64
Numbers []AvailableNumber
}
// Type AvailableNumber represents a phone number available for purchase
type AvailableNumber struct {
Country string
MSISDN string
Type string
Features []string
Cost float64 `json:",string"`
}
/*
GET /number/search?api_key={api_key}&api_secret={api_secret}&country={country}&pattern={pattern}&search_pattern={search_pattern}&features={features}&index={index}&size={size}
{"count":count,"numbers":[{"country":"country-code","msisdn":"phone number","type":"type of number","features":["feature"],"cost":"number cost"}]}
*/
// Search for available phone numbers in a given country
func (c *Numbers) SearchAvailable(countryCode string) (response NumberSearchResponse, err error) {
return c.SearchAvailableWithOptions(countryCode, NumberSearchOptions{})
}
// Search for available phone numbers in a given country, filtering by a pattern
func (c *Numbers) SearchAvailableWithOptions(countryCode string, opts NumberSearchOptions) (response NumberSearchResponse, err error) {
if len(countryCode) <= 0 {
err = errors.New("Invalid country code field specified")
return
}
client := &http.Client{}
requestUrl := apiRoot + "/number/search/" + c.client.apiKey + "/" + c.client.apiSecret + "/" + countryCode
if opts.Pattern != "" && opts.SearchPattern != "" {
requestUrl += "?pattern=" + url.QueryEscape(opts.Pattern)
if opts.SearchPattern != "" {
requestUrl += "&search_pattern=" + url.QueryEscape(opts.SearchPattern)
}
}
r, _ := http.NewRequest("GET", requestUrl, nil)
r.Header.Add("Accept", "application/json")
resp, err := client.Do(r)
defer resp.Body.Close()
if err != nil {
return
}
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &response)
return
}
/*
POST /number/buy/{api_key}/{api_secret}/{country}/{msisdn}
POST /number/buy?api_key={api_key}&api_secret={api_secret}&country={country}&msisdn={msisdn}
*/
// Buy a phone number
func (c *Numbers) BuyPhoneNumber(countryCode, number string) (bool, error) {
if len(countryCode) <= 0 {
return false, errors.New("Invalid country code field specified")
}
if len(number) <= 0 {
return false, errors.New("Invalid number field specified")
}
client := &http.Client{}
requestUrl := apiRoot + "/number/buy/" + c.client.apiKey + "/" +
c.client.apiSecret + "/" + countryCode + "/" + number
r, _ := http.NewRequest("POST", requestUrl, nil)
r.Header.Add("Accept", "application/json")
resp, err := client.Do(r)
defer resp.Body.Close()
if err != nil {
return false, err
}
switch resp.StatusCode {
case 200:
return true, nil
case 401:
return false, errors.New("Wrong credentials")
case 420:
return false, errors.New("Bad parameters")
default:
return false, errors.New("Other error")
}
}
/*
POST /number/cancel/{api_key}/{api_secret}/{country}/{msisdn}
POST /number/cancel?api_key={api_key}&api_secret={api_secret}&country={country}&msisdn={msisdn}
*/
// Cancel a phone number
func (c *Numbers) CancelPhoneNumber(countryCode, number string) (bool, error) {
if len(countryCode) <= 0 {
return false, errors.New("Invalid country code field specified")
}
if len(number) <= 0 {
return false, errors.New("Invalid number field specified")
}
client := &http.Client{}
requestUrl := apiRoot + "/number/cancel/" + c.client.apiKey + "/" +
c.client.apiSecret + "/" + countryCode + "/" + number
r, _ := http.NewRequest("POST", requestUrl, nil)
r.Header.Add("Accept", "application/json")
resp, err := client.Do(r)
defer resp.Body.Close()
if err != nil {
return false, err
}
switch resp.StatusCode {
case 200:
return true, nil
case 401:
return false, errors.New("Wrong credentials")
case 420:
return false, errors.New("Bad parameters")
default:
return false, errors.New("Other error")
}
}
/*
POST /number/update/{api_key}/{api_secret}/{country}/{msisdn}?moHttpUrl={url}&moSmppSysType={sysType}&voiceCallbackType={type}&voiceCallbackValue={value}&voiceStatusCallback={status}
POST /number/update?api_key={api_key}&api_secret={api_secret}&country={country}&msisdn={msisdn}&moHttpUrl={url}&moSmppSysType={sysType}&voiceCallbackType={type}&voiceCallbackValue={value}&voiceStatusCallback={status}
*/
type UpdateNumberOpts struct {
MoHttpUrl string
}
// Update a phone number with webhook URLs
func (c *Numbers) UpdateNumber(countryCode, number string, opts UpdateNumberOpts) (bool, error) {
if len(countryCode) <= 0 {
return false, errors.New("Invalid country code field specified")
}
if len(number) <= 0 {
return false, errors.New("Invalid number field specified")
}
client := &http.Client{}
requestUrl := apiRoot + "/number/update/" + c.client.apiKey + "/" +
c.client.apiSecret + "/" + countryCode + "/" + number + "?"
if opts.MoHttpUrl != "" {
requestUrl += "&moHttpUrl=" + url.QueryEscape(opts.MoHttpUrl)
}
r, _ := http.NewRequest("POST", requestUrl, nil)
r.Header.Add("Accept", "application/json")
resp, err := client.Do(r)
defer resp.Body.Close()
if err != nil {
return false, err
}
switch resp.StatusCode {
case 200:
return true, nil
case 401:
return false, errors.New("Wrong credentials")
case 420:
return false, errors.New("Bad parameters")
default:
return false, errors.New("Other error")
}
}