-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_balance.go
61 lines (51 loc) · 1.53 KB
/
user_balance.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
package ephemeralproxies
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
)
type Balance struct {
ConsumedMegabytes int `json:"consumed_megabytes"`
LimitMegabytes int `json:"limit_megabytes"`
}
type userBalanceApiResponse struct {
Success bool `json:"success"`
Balance Balance `json:"balance"`
Message string `json:"message"`
}
func (balance *Balance) String() string {
r, _ := json.MarshalIndent(balance, "", " ")
return string(r)
}
// GetUserBalance returns the monthly balance left for the user
func GetUserBalance(apiKey string, proxyType ProxyType) (*Balance, error) {
if proxyType != Residential {
return nil, errors.New("Proxy type " + proxyType.String() + " does not support retrieving user's balance")
}
url := "https://ephemeral-proxies.p.rapidapi.com/v2/" + proxyType.String() + "/balance"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("X-RapidAPI-Host", "ephemeral-proxies.p.rapidapi.com")
req.Header.Add("X-RapidAPI-Key", apiKey)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, errors.New("http response: " + strconv.Itoa(res.StatusCode))
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var balanceRespone userBalanceApiResponse
if err := json.Unmarshal(body, &balanceRespone); err != nil {
return nil, err
}
if !balanceRespone.Success {
return nil, errors.New("api failure: " + balanceRespone.Message)
}
return &balanceRespone.Balance, nil
}