-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemail_verifier.go
69 lines (65 loc) · 2.03 KB
/
email_verifier.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
package hunter
import (
"bytes"
"context"
"encoding/json"
"net/http"
)
// EmailVerifierResult is returned by the VerifyEmail function.
type EmailVerifierResult struct {
Data struct {
Result string `json:"result"`
Score int `json:"score"`
Email string `json:"email"`
Regexp bool `json:"regexp"`
Gibberish bool `json:"gibberish"`
Disposable bool `json:"disposable"`
Webmail bool `json:"webmail"`
MxRecords bool `json:"mx_records"`
SMTPServer bool `json:"smtp_server"`
SMTPCheck bool `json:"smtp_check"`
AcceptAll bool `json:"accept_all"`
Block bool `json:"block"`
Sources []struct {
Domain string `json:"domain"`
URI string `json:"uri"`
ExtractedOn string `json:"extracted_on"`
LastSeenOn string `json:"last_seen_on"`
StillOnPage bool `json:"still_on_page"`
} `json:"sources"`
} `json:"data"`
Meta struct {
Params struct {
Email string `json:"email"`
} `json:"params"`
} `json:"meta"`
}
// VerifyEmail allows you to verify the deliverability of an email address.
func (c *Client) VerifyEmail(params Params) (*EmailVerifierResult, error) {
body, err := c.request(context.Background(), http.MethodGet, "https://api.hunter.io/v2/email-verifier", params)
if err != nil {
return nil, err
}
result := new(EmailVerifierResult)
err = json.NewDecoder(bytes.NewReader(body)).Decode(result)
if err != nil {
return nil, err
}
return result, nil
}
// VerifyEmailWithContext allows you to verify the deliverability of an email address.
//
// This context-based version of the function would be more suitable for long-running
// applications like servers.
func (c *Client) VerifyEmailWithContext(ctx context.Context, params Params) (*EmailVerifierResult, error) {
body, err := c.request(ctx, http.MethodGet, "https://api.hunter.io/v2/email-verifier", params)
if err != nil {
return nil, err
}
result := new(EmailVerifierResult)
err = json.NewDecoder(bytes.NewReader(body)).Decode(result)
if err != nil {
return nil, err
}
return result, nil
}