-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
109 lines (88 loc) · 2.68 KB
/
api.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package gozerobounce
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
)
// URI URL for the requests
const URI = `https://api.zerobounce.net/v2/`
// APIKey Key to be used in requests
var APIKey string
// APIResponse basis for api responses
type APIResponse interface{}
// CreditsResponse response of the credits balance
type CreditsResponse struct {
Credits string `json:"Credits"`
}
// ValidateResponse Response from API
type ValidateResponse struct {
Address string `json:"address"`
Status string `json:"status"`
SubStatus string `json:"sub_status"`
FreeEmail bool `json:"free_email"`
DidYouMean interface{} `json:"did_you_mean"`
Account string `json:"account"`
Domain string `json:"domain"`
DomainAgeDays string `json:"domain_age_days"`
SMTPProvider string `json:"smtp_provider"`
MxRecord string `json:"mx_record"`
MxFound string `json:"mx_found"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Gender string `json:"gender"`
Country string `json:"country"`
Region string `json:"region"`
City string `json:"city"`
Zipcode string `json:"zipcode"`
ProcessedAt string `json:"processed_at"`
}
// IsValid checks if an email is valid
func (v *ValidateResponse) IsValid() bool {
if v.Status == "valid" {
return true
}
return false
}
// PrepareURL prepares the URL
func PrepareURL(endpoint string, params url.Values) string {
// Set API KEY
params.Set("api_key", APIKey)
// Create a return the final URL
return fmt.Sprintf("%s/%s?%s", URI, endpoint, params.Encode())
}
// DoRequest does the request to the API
func DoRequest(url string, object APIResponse) error {
// Do the request
response, err := http.Get(url)
if err != nil {
return err
}
// Check if server response is not HTTP 200
if response.StatusCode != 200 {
return errors.New("Server error")
}
// Close the request
defer response.Body.Close()
// Decode JSON Request
err = json.NewDecoder(response.Body).Decode(&object)
return err
}
// Validate validates a single email
func Validate(email string, IPAddress string) (*ValidateResponse, error) {
// Prepare the parameters
params := url.Values{}
params.Set("email", email)
params.Set("ip_address", IPAddress)
response := &ValidateResponse{}
// Do the request
err := DoRequest(PrepareURL("validate", params), response)
return response, err
}
// GetCredits gets credits balance
func GetCredits() (*CreditsResponse, error) {
response := &CreditsResponse{}
err := DoRequest(PrepareURL("getcredits", url.Values{}), response)
return response, err
}