-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_prices.go
86 lines (72 loc) · 1.76 KB
/
get_prices.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
package sms_activate_go
import (
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/google/go-querystring/query"
)
type (
Info struct {
// cost per number of service
Cost float64 `json:"cost"`
// count numbers available for this service
Count int `json:"count"`
}
)
const pricesAction = "getPrices"
// GetPrices returns actual prices by country and service. Service may be empty if you need to get all services. Country may be -1 if you need to get all countries.
// You can get tables of services and countries at https://sms-activate.org/api2
//
// Example
//
// data, err := client.GetPrices("", -1)
// if err != nil {
// log.Fatal(err)
// }
// for k, v := range data {
// fmt.Println("Country:", k)
// for k, v := range v {
// fmt.Printf("Service: %s. Cost: %f, Count: %d\n", k, v.Cost, v.Count)
// }
// }
func (act *SMSActivate) GetPrices(service string, country int) (map[string]map[string]Info, error) {
if country < 0 || country > maxAvailableCountries {
return nil, BadCountryNum
}
req, _ := http.NewRequest(http.MethodGet, act.BaseURL.String(), nil)
pricesReq := baseRequest{
APIKey: act.APIKey,
Action: pricesAction,
Service: service,
}
val, err := query.Values(pricesReq)
if err != nil {
return nil, 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 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]map[string]Info
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
return data, nil
}