-
Notifications
You must be signed in to change notification settings - Fork 2
/
binchecker.go
80 lines (68 loc) · 1.61 KB
/
binchecker.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
package binchecker
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"time"
)
// Result represents incoming JSON data from Prompt API - BIN Checker
type Result struct {
BankName string `json:"bank_name"`
Country string `json:"country"`
URL string `json:"url"`
Type string `json:"type"`
Scheme string `json:"scheme"`
Bin string `json:"bin"`
}
// ErrorResponse represents response errors from Prompt API - BIN Checker
type ErrorResponse struct {
Message string `json:"message"`
}
var promptAPIEndpoint = "https://api.promptapi.com/bincheck/"
// BinChecker collects bin information from Prompt API - BIN Checker
func BinChecker(binNumber string, result *Result) error {
apiKey, ok := os.LookupEnv("PROMPTAPI_TOKEN")
if !ok {
return errors.New("You need to set PROMPTAPI_TOKEN environment variable")
}
client := &http.Client{
Timeout: 5 * time.Second,
}
requestURL := promptAPIEndpoint + binNumber
testURL, ok := os.LookupEnv("PROMPTAPI_TEST_ENDPOINT")
if ok {
requestURL = testURL
}
req, err := http.NewRequest("GET", requestURL, nil)
req.Header.Set("apikey", apiKey)
if err != nil {
return err
}
res, err := client.Do(req)
if err != nil {
return err
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
msg := new(ErrorResponse)
err := json.Unmarshal(body, msg)
if err != nil {
return err
}
return errors.New(msg.Message)
}
err = json.Unmarshal(body, result)
if err != nil {
return err
}
return nil
}