-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemail_verification.go
220 lines (176 loc) · 6.25 KB
/
email_verification.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package mailersend
import (
"context"
"fmt"
"net/http"
"time"
)
const emailVerificationBasePath = "/email-verification"
type EmailVerificationService service
// emailVerificationRoot format of verification response
type emailVerificationRoot struct {
Data []emailVerification `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
// singleDomainRoot format of single verification response
type singleEmailVerificationRoot struct {
Data emailVerification `json:"data"`
}
type resultEmailVerificationRoot struct {
Data []result `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type resultSingleEmailVerification struct {
Status string `json:"status"`
}
type emailVerification struct {
Id string `json:"id"`
Name string `json:"name"`
Total int `json:"total"`
VerificationStarted interface{} `json:"verification_started"`
VerificationEnded interface{} `json:"verification_ended"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Status status `json:"status"`
Source string `json:"source"`
Statistics statistics `json:"statistics"`
}
type status struct {
Name string `json:"name"`
Count int `json:"count"`
}
type statistics struct {
Valid int `json:"valid"`
CatchAll int `json:"catch_all"`
MailboxFull int `json:"mailbox_full"`
RoleBased int `json:"role_based"`
Unknown int `json:"unknown"`
SyntaxError int `json:"syntax_error"`
Typo int `json:"typo"`
MailboxNotFound int `json:"mailbox_not_found"`
Disposable int `json:"disposable"`
MailboxBlocked int `json:"mailbox_blocked"`
Failed int `json:"failed"`
}
type result struct {
Address string `json:"address"`
Result string `json:"result"`
}
// ListEmailVerificationOptions - modifies the behavior of EmailVerificationService.List Method
type ListEmailVerificationOptions struct {
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
// CreateEmailVerificationOptions - modifies the behavior of EmailVerificationService.Create Method
type CreateEmailVerificationOptions struct {
Name string `json:"name"`
Emails []string `json:"emails"`
}
// GetEmailVerificationOptions - modifies the behavior of EmailVerificationService.List and EmailVerificationService.GetResult Method
type GetEmailVerificationOptions struct {
EmailVerificationId string `url:"-"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
type SingleEmailVerificationOptions struct {
Email string `json:"email"`
}
func (s *EmailVerificationService) List(ctx context.Context, options *ListEmailVerificationOptions) (*emailVerificationRoot, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, emailVerificationBasePath, options)
if err != nil {
return nil, nil, err
}
root := new(emailVerificationRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *EmailVerificationService) Get(ctx context.Context, emailVerificationId string) (*singleEmailVerificationRoot, *Response, error) {
path := fmt.Sprintf("%s/%s", emailVerificationBasePath, emailVerificationId)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(singleEmailVerificationRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *EmailVerificationService) Update(ctx context.Context, options *DomainSettingOptions) (*singleEmailVerificationRoot, *Response, error) {
path := fmt.Sprintf("%s/%s/settings", emailVerificationBasePath, options.DomainID)
req, err := s.client.newRequest(http.MethodPut, path, options)
if err != nil {
return nil, nil, err
}
root := new(singleEmailVerificationRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *EmailVerificationService) Delete(ctx context.Context, domainID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", emailVerificationBasePath, domainID)
req, err := s.client.newRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.do(ctx, req, nil)
}
func (s *EmailVerificationService) Create(ctx context.Context, options *CreateEmailVerificationOptions) (*singleEmailVerificationRoot, *Response, error) {
req, err := s.client.newRequest(http.MethodPost, emailVerificationBasePath, options)
if err != nil {
return nil, nil, err
}
root := new(singleEmailVerificationRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *EmailVerificationService) Verify(ctx context.Context, emailVerificationId string) (*singleEmailVerificationRoot, *Response, error) {
path := fmt.Sprintf("%s/%s/verify", emailVerificationBasePath, emailVerificationId)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(singleEmailVerificationRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *EmailVerificationService) VerifySingle(ctx context.Context, options *SingleEmailVerificationOptions) (*resultSingleEmailVerification, *Response, error) {
path := fmt.Sprintf("%s/verify", emailVerificationBasePath)
req, err := s.client.newRequest(http.MethodPost, path, options)
if err != nil {
return nil, nil, err
}
verification := new(resultSingleEmailVerification)
res, err := s.client.do(ctx, req, verification)
if err != nil {
return nil, res, err
}
return verification, res, nil
}
func (s *EmailVerificationService) GetResults(ctx context.Context, options *GetEmailVerificationOptions) (*resultEmailVerificationRoot, *Response, error) {
path := fmt.Sprintf("%s/%s/results", emailVerificationBasePath, options.EmailVerificationId)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(resultEmailVerificationRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}