-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (63 loc) · 1.82 KB
/
main.go
File metadata and controls
76 lines (63 loc) · 1.82 KB
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
package tempmaildetector
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
const (
apiURL = "https://api.tempmaildetector.com/check"
contentType = "application/json"
)
type Client struct {
APIKey string
}
type DomainCheckRequest struct {
Domain string `json:"domain"`
}
type DomainCheckResponse struct {
Domain string `json:"domain"`
Score int `json:"score"`
Meta struct {
BlockList bool `json:"block_list"`
DomainAge int `json:"domain_age"`
WebsiteResolves bool `json:"website_resolves"`
AcceptsAllAddresses bool `json:"accepts_all_addresses"`
ValidEmailSecurity bool `json:"valid_email_security"`
} `json:"meta"`
}
func NewClient(apiKey string) *Client {
return &Client{APIKey: apiKey}
}
func (c *Client) CheckDomain(domain string) (*DomainCheckResponse, error) {
requestBody, err := json.Marshal(DomainCheckRequest{Domain: domain})
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(requestBody))
if err != nil {
return nil, fmt.Errorf("failed to create new request: %w", err)
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", c.APIKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to make HTTP request: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New("received non-200 response: " + string(body))
}
var response DomainCheckResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
return &response, nil
}