-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_available_numbers.go
82 lines (71 loc) · 1.74 KB
/
get_available_numbers.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
package sms_activate_go
import (
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"github.com/google/go-querystring/query"
)
const (
numsStatusAction = "getNumbersStatus"
russiaID = 0
ukraineID = 1
kazakhstanID = 2
)
// GetAvailableNumbers returns available phone numbers to rent by country.
// (It is also possible to add certain mobile operators. But only for countries with ID 0, 1, 2 - Russia, Ukraine, Kazakhstan, respectively)
//
// Example
//
// av, err := client.GetAvailableNumbers(0)
// if err != nil {
// log.Fatal(err)
// }
// for k, v := range av {
// fmt.Printf("Service: %s, Count: %s\n", k, v)
// }
func (act *SMSActivate) GetAvailableNumbers(country int, operator ...string) (map[string]string, error) {
if country < allCountries || country > maxAvailableCountries {
return nil, BadCountryNum
}
req, _ := http.NewRequest(http.MethodGet, act.BaseURL.String(), nil)
numsReq := baseRequest{
APIKey: apiKeyQuery,
Action: numsStatusAction,
}
val, err := query.Values(numsReq)
if err != nil {
return nil, err
}
if country > allCountries {
val.Add(countryQuery, strconv.Itoa(country))
}
if country == russiaID || country == ukraineID || country == kazakhstanID {
var operators string
operators = strings.Join(operator, ",")
val.Add(operatorQuery, operators)
}
req.URL.RawQuery = val.Encode()
resp, err := act.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
switch string(body) {
case badKeyMsg:
return nil, ErrBadKey
case errorSQLMsg:
return nil, ErrSQL
}
var data map[string]string
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
return data, nil
}