-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_operators.go
76 lines (64 loc) · 1.7 KB
/
get_operators.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
package sms_activate_go
import (
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/google/go-querystring/query"
)
type CountryOperators struct {
Operators map[string][]string `json:"countryOperators"`
}
const operatorsAction = "getOperators"
// GetOperators returns all operators available for the transferred country, if the country is -1 it will return all operators available for each country.
//
// Example
//
// operators, err := client.GetOperators(-1)
// if err != nil {
// log.Fatal(err)
// }
// for country, operators := range operators.Operators {
// fmt.Printf("Country: %s. Operators: %s\n", country, operators)
// }
func (act *SMSActivate) GetOperators(country int) (CountryOperators, error) {
if country < allCountries || country > maxAvailableCountries {
return CountryOperators{}, BadCountryNum
}
req, _ := http.NewRequest(http.MethodGet, act.BaseURL.String(), nil)
operatorsReq := baseRequest{
APIKey: act.APIKey,
Action: operatorsAction,
}
val, err := query.Values(operatorsReq)
if err != nil {
return CountryOperators{}, err
}
if country > allCountries {
val.Add(countryQuery, strconv.Itoa(country))
}
req.URL.RawQuery = val.Encode()
resp, err := act.httpClient.Do(req)
if err != nil {
return CountryOperators{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return CountryOperators{}, err
}
switch string(body) {
case badKeyMsg:
return CountryOperators{}, ErrBadKey
case errorSQLMsg:
return CountryOperators{}, ErrSQL
case noOperatorsMsg:
return CountryOperators{}, ErrNoOperators
}
var data CountryOperators
err = json.Unmarshal(body, &data)
if err != nil {
return CountryOperators{}, err
}
return data, nil
}