-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipbase.go
62 lines (47 loc) · 1.12 KB
/
ipbase.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
package emailvalidation
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
)
var apikey = ""
var client = httpClient()
const BaseUrl = "https://api.emailvalidation.io/v1/"
func Init(key string) {
apikey = key
}
func httpClient() *http.Client {
client := &http.Client{Timeout: 10 * time.Second}
return client
}
func apiCall(endpoint string, params ...map[string]string) []byte {
if len(apikey) == 0 {
log.Fatalf("No API key provided!")
}
jsonParams, err := json.Marshal(params)
req, err := http.NewRequest("GET", BaseUrl+endpoint, bytes.NewBuffer(jsonParams))
if err != nil {
log.Fatalf("Error Occurred. %+v", err)
}
req.Header.Set("apikey", apikey)
response, err := client.Do(req)
if err != nil {
log.Fatalf("Error sending request to API endpoint. %+v", err)
}
// Close the connection to reuse it
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("Couldn't parse response body. %+v", err)
}
return body
}
func Status() []byte {
return apiCall("status")
}
func Info(params map[string]string) []byte {
return apiCall("info", params)
}