-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_number.go
88 lines (78 loc) · 2.07 KB
/
get_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
package sms_activate_go
import (
"encoding/json"
"io"
"net/http"
"github.com/google/go-querystring/query"
)
const (
getNumberAction = "getNumberV2"
)
type (
GetNumberRequest struct {
Service string `url:"service"`
Country string `url:"country"`
Forward bool `url:"forward,omitempty"`
FreePrice bool `url:"freePrice,omitempty"`
MaxPrice float64 `url:"maxPrice,omitempty"`
PhoneException []string `url:"phoneException,omitempty" del:","`
Operator []string `url:"operator,omitempty" del:","`
Verification bool `url:"verification,omitempty"`
}
Number struct {
ActivationID string `json:"activationId"`
PhoneNumber string `json:"phoneNumber"`
ActivationCost string `json:"activationCost"`
CountryCode string `json:"countryCode"`
CanGetAnotherSMS bool `json:"canGetAnotherSms"`
Time string `json:"activationTime"`
Operator string `json:"activationOperator"`
}
)
// GetNumber rents a phone number and returns info about it.
//
// Example
//
// num, err := client.GetNumber(SMSActivate.GetNumberRequest{
// Service: "ig",
// Country: "6",
// })
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(num.PhoneNumber)
func (act *SMSActivate) GetNumber(request GetNumberRequest) (Number, error) {
req, _ := http.NewRequest(http.MethodGet, act.BaseURL.String(), nil)
val, err := query.Values(request)
if err != nil {
return Number{}, err
}
val.Add(apiKeyQuery, act.APIKey)
val.Add(actionQuery, getNumberAction)
req.URL.RawQuery = val.Encode()
resp, err := act.httpClient.Do(req)
if err != nil {
return Number{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Number{}, err
}
switch string(body) {
case noBalanceMsg:
return Number{}, ErrNoBalance
case badKeyMsg:
return Number{}, ErrBadKey
case errorSQLMsg:
return Number{}, ErrSQL
case noNumbersMsg:
return Number{}, ErrNoNumbers
}
var num Number
err = json.Unmarshal(body, &num)
if err != nil {
return Number{}, err
}
return num, nil
}