-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
616 lines (524 loc) · 17.4 KB
/
client.go
File metadata and controls
616 lines (524 loc) · 17.4 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package billionverify
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
defaultBaseURL = "https://api.billionverify.com"
defaultTimeout = 30 * time.Second
defaultRetries = 3
userAgent = "billionverify-go/1.0.0"
)
// Config contains client configuration options.
type Config struct {
APIKey string
BaseURL string
Timeout time.Duration
Retries int
}
// Client is the BillionVerify API client.
type Client struct {
apiKey string
baseURL string
timeout time.Duration
retries int
httpClient *http.Client
}
// NewClient creates a new BillionVerify client.
func NewClient(config Config) (*Client, error) {
if config.APIKey == "" {
return nil, NewAuthenticationError("API key is required")
}
baseURL := config.BaseURL
if baseURL == "" {
baseURL = defaultBaseURL
}
baseURL = strings.TrimSuffix(baseURL, "/")
timeout := config.Timeout
if timeout == 0 {
timeout = defaultTimeout
}
retries := config.Retries
if retries == 0 {
retries = defaultRetries
}
return &Client{
apiKey: config.APIKey,
baseURL: baseURL,
timeout: timeout,
retries: retries,
httpClient: &http.Client{
Timeout: timeout,
},
}, nil
}
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) error {
return c.requestWithRetry(ctx, method, path, body, result, 1)
}
func (c *Client) requestWithRetry(ctx context.Context, method, path string, body interface{}, result interface{}, attempt int) error {
reqURL := c.baseURL + path
var bodyReader io.Reader
if body != nil {
jsonBody, err := json.Marshal(body)
if err != nil {
return &BillionVerifyError{Code: "MARSHAL_ERROR", Message: err.Error()}
}
bodyReader = bytes.NewReader(jsonBody)
}
req, err := http.NewRequestWithContext(ctx, method, reqURL, bodyReader)
if err != nil {
return &BillionVerifyError{Code: "REQUEST_ERROR", Message: err.Error()}
}
req.Header.Set("EV-API-KEY", c.apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return NewTimeoutError(fmt.Sprintf("Request timed out after %v", c.timeout))
}
return &BillionVerifyError{Code: "NETWORK_ERROR", Message: err.Error()}
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return &BillionVerifyError{Code: "READ_ERROR", Message: err.Error()}
}
if resp.StatusCode == http.StatusNoContent {
return nil
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
if result != nil && len(respBody) > 0 {
if err := json.Unmarshal(respBody, result); err != nil {
return &BillionVerifyError{Code: "UNMARSHAL_ERROR", Message: err.Error()}
}
}
return nil
}
return c.handleErrorResponse(ctx, resp, respBody, method, path, body, result, attempt)
}
// requestNoAuth makes a request without authentication (for health check).
func (c *Client) requestNoAuth(ctx context.Context, method, path string, result interface{}) error {
reqURL := c.baseURL + path
req, err := http.NewRequestWithContext(ctx, method, reqURL, nil)
if err != nil {
return &BillionVerifyError{Code: "REQUEST_ERROR", Message: err.Error()}
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return NewTimeoutError(fmt.Sprintf("Request timed out after %v", c.timeout))
}
return &BillionVerifyError{Code: "NETWORK_ERROR", Message: err.Error()}
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return &BillionVerifyError{Code: "READ_ERROR", Message: err.Error()}
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
if result != nil && len(respBody) > 0 {
if err := json.Unmarshal(respBody, result); err != nil {
return &BillionVerifyError{Code: "UNMARSHAL_ERROR", Message: err.Error()}
}
}
return nil
}
return &BillionVerifyError{Code: "REQUEST_FAILED", Message: resp.Status, StatusCode: resp.StatusCode}
}
func (c *Client) handleErrorResponse(ctx context.Context, resp *http.Response, body []byte, method, path string, reqBody interface{}, result interface{}, attempt int) error {
var errResp struct {
Error APIError `json:"error"`
}
json.Unmarshal(body, &errResp)
message := errResp.Error.Message
if message == "" {
message = resp.Status
}
code := errResp.Error.Code
if code == "" {
code = "UNKNOWN_ERROR"
}
switch resp.StatusCode {
case http.StatusUnauthorized:
return NewAuthenticationError(message)
case http.StatusPaymentRequired:
return NewInsufficientCreditsError(message)
case http.StatusNotFound:
return NewNotFoundError(message)
case http.StatusTooManyRequests:
retryAfter, _ := strconv.Atoi(resp.Header.Get("Retry-After"))
if attempt < c.retries {
waitTime := retryAfter
if waitTime == 0 {
waitTime = 1 << attempt
}
time.Sleep(time.Duration(waitTime) * time.Second)
return c.requestWithRetry(ctx, method, path, reqBody, result, attempt+1)
}
return NewRateLimitError(message, retryAfter)
case http.StatusBadRequest:
return NewValidationError(message, errResp.Error.Details)
case http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable:
if attempt < c.retries {
time.Sleep(time.Duration(1<<attempt) * time.Second)
return c.requestWithRetry(ctx, method, path, reqBody, result, attempt+1)
}
return &BillionVerifyError{Code: code, Message: message, StatusCode: resp.StatusCode}
default:
return &BillionVerifyError{Code: code, Message: message, StatusCode: resp.StatusCode}
}
}
// Health checks the API health status (no authentication required).
func (c *Client) Health(ctx context.Context) (*HealthResponse, error) {
var result HealthResponse
if err := c.requestNoAuth(ctx, http.MethodGet, "/health", &result); err != nil {
return nil, err
}
return &result, nil
}
// Verify verifies a single email address.
func (c *Client) Verify(ctx context.Context, email string, opts *VerifyOptions) (*VerifyResponse, error) {
payload := map[string]interface{}{
"email": email,
"check_smtp": true,
}
if opts != nil {
payload["check_smtp"] = opts.CheckSMTP
}
var apiResp struct {
Success bool `json:"success"`
Code string `json:"code"`
Message string `json:"message"`
Data VerifyResponse `json:"data"`
}
if err := c.request(ctx, http.MethodPost, "/v1/verify/single", payload, &apiResp); err != nil {
return nil, err
}
return &apiResp.Data, nil
}
// VerifyBatch verifies multiple email addresses synchronously (max 50 emails).
func (c *Client) VerifyBatch(ctx context.Context, emails []string, opts *BatchVerifyOptions) (*BatchVerifyResponse, error) {
if len(emails) > 50 {
return nil, NewValidationError("Maximum 50 emails per batch request", "")
}
payload := map[string]interface{}{
"emails": emails,
"check_smtp": true,
}
if opts != nil {
payload["check_smtp"] = opts.CheckSMTP
}
var apiResp struct {
Success bool `json:"success"`
Code string `json:"code"`
Message string `json:"message"`
Data BatchVerifyResponse `json:"data"`
}
if err := c.request(ctx, http.MethodPost, "/v1/verify/bulk", payload, &apiResp); err != nil {
return nil, err
}
return &apiResp.Data, nil
}
// UploadFile uploads a file for asynchronous verification.
func (c *Client) UploadFile(ctx context.Context, filePath string, opts *FileUploadOptions) (*FileUploadResponse, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, &BillionVerifyError{Code: "FILE_ERROR", Message: err.Error()}
}
defer file.Close()
return c.UploadFileReader(ctx, file, filepath.Base(filePath), opts)
}
// UploadFileReader uploads a file from an io.Reader for asynchronous verification.
func (c *Client) UploadFileReader(ctx context.Context, reader io.Reader, filename string, opts *FileUploadOptions) (*FileUploadResponse, error) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("file", filename)
if err != nil {
return nil, &BillionVerifyError{Code: "MULTIPART_ERROR", Message: err.Error()}
}
if _, err := io.Copy(part, reader); err != nil {
return nil, &BillionVerifyError{Code: "COPY_ERROR", Message: err.Error()}
}
// Add optional form fields
checkSMTP := true
preserveOriginal := true
emailColumn := ""
if opts != nil {
checkSMTP = opts.CheckSMTP
preserveOriginal = opts.PreserveOriginal
emailColumn = opts.EmailColumn
}
writer.WriteField("check_smtp", strconv.FormatBool(checkSMTP))
writer.WriteField("preserve_original", strconv.FormatBool(preserveOriginal))
if emailColumn != "" {
writer.WriteField("email_column", emailColumn)
}
if err := writer.Close(); err != nil {
return nil, &BillionVerifyError{Code: "MULTIPART_ERROR", Message: err.Error()}
}
reqURL := c.baseURL + "/v1/verify/file"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, &body)
if err != nil {
return nil, &BillionVerifyError{Code: "REQUEST_ERROR", Message: err.Error()}
}
req.Header.Set("EV-API-KEY", c.apiKey)
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("User-Agent", userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return nil, NewTimeoutError(fmt.Sprintf("Request timed out after %v", c.timeout))
}
return nil, &BillionVerifyError{Code: "NETWORK_ERROR", Message: err.Error()}
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, &BillionVerifyError{Code: "READ_ERROR", Message: err.Error()}
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var apiResp struct {
Success bool `json:"success"`
Code string `json:"code"`
Message string `json:"message"`
Data FileUploadResponse `json:"data"`
}
if err := json.Unmarshal(respBody, &apiResp); err != nil {
return nil, &BillionVerifyError{Code: "UNMARSHAL_ERROR", Message: err.Error()}
}
return &apiResp.Data, nil
}
// Handle error response
var errResp struct {
Error APIError `json:"error"`
}
json.Unmarshal(respBody, &errResp)
message := errResp.Error.Message
if message == "" {
message = resp.Status
}
switch resp.StatusCode {
case http.StatusUnauthorized:
return nil, NewAuthenticationError(message)
case http.StatusPaymentRequired:
return nil, NewInsufficientCreditsError(message)
case http.StatusBadRequest:
return nil, NewValidationError(message, errResp.Error.Details)
default:
return nil, &BillionVerifyError{Code: errResp.Error.Code, Message: message, StatusCode: resp.StatusCode}
}
}
// GetFileJobStatus gets the status of a file verification job.
func (c *Client) GetFileJobStatus(ctx context.Context, jobID string, opts *FileJobStatusOptions) (*FileJobResponse, error) {
path := "/v1/verify/file/" + jobID
if opts != nil && opts.Timeout > 0 {
timeout := opts.Timeout
if timeout > 300 {
timeout = 300
}
path += "?timeout=" + strconv.Itoa(timeout)
}
var apiResp struct {
Success bool `json:"success"`
Code string `json:"code"`
Message string `json:"message"`
Data FileJobResponse `json:"data"`
}
if err := c.request(ctx, http.MethodGet, path, nil, &apiResp); err != nil {
return nil, err
}
return &apiResp.Data, nil
}
// GetFileJobResults downloads the results of a completed file verification job.
// Returns the raw CSV data as bytes. Use FileResultsOptions to filter results.
func (c *Client) GetFileJobResults(ctx context.Context, jobID string, opts *FileResultsOptions) ([]byte, error) {
path := "/v1/verify/file/" + jobID + "/results"
if opts != nil {
params := url.Values{}
if opts.Valid != nil {
params.Set("valid", strconv.FormatBool(*opts.Valid))
}
if opts.Invalid != nil {
params.Set("invalid", strconv.FormatBool(*opts.Invalid))
}
if opts.Catchall != nil {
params.Set("catchall", strconv.FormatBool(*opts.Catchall))
}
if opts.Role != nil {
params.Set("role", strconv.FormatBool(*opts.Role))
}
if opts.Unknown != nil {
params.Set("unknown", strconv.FormatBool(*opts.Unknown))
}
if opts.Disposable != nil {
params.Set("disposable", strconv.FormatBool(*opts.Disposable))
}
if opts.Risky != nil {
params.Set("risky", strconv.FormatBool(*opts.Risky))
}
if len(params) > 0 {
path += "?" + params.Encode()
}
}
reqURL := c.baseURL + path
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return nil, &BillionVerifyError{Code: "REQUEST_ERROR", Message: err.Error()}
}
req.Header.Set("EV-API-KEY", c.apiKey)
req.Header.Set("User-Agent", userAgent)
// Create a client that doesn't follow redirects automatically
client := &http.Client{
Timeout: c.timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return nil, NewTimeoutError(fmt.Sprintf("Request timed out after %v", c.timeout))
}
return nil, &BillionVerifyError{Code: "NETWORK_ERROR", Message: err.Error()}
}
defer resp.Body.Close()
// Handle redirect (307) - follow it to get the actual file
if resp.StatusCode == http.StatusTemporaryRedirect {
location := resp.Header.Get("Location")
if location != "" {
redirectReq, err := http.NewRequestWithContext(ctx, http.MethodGet, location, nil)
if err != nil {
return nil, &BillionVerifyError{Code: "REQUEST_ERROR", Message: err.Error()}
}
redirectResp, err := c.httpClient.Do(redirectReq)
if err != nil {
return nil, &BillionVerifyError{Code: "NETWORK_ERROR", Message: err.Error()}
}
defer redirectResp.Body.Close()
return io.ReadAll(redirectResp.Body)
}
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return io.ReadAll(resp.Body)
}
// Handle error
respBody, _ := io.ReadAll(resp.Body)
var errResp struct {
Error APIError `json:"error"`
}
json.Unmarshal(respBody, &errResp)
message := errResp.Error.Message
if message == "" {
message = resp.Status
}
switch resp.StatusCode {
case http.StatusUnauthorized:
return nil, NewAuthenticationError(message)
case http.StatusNotFound:
return nil, NewNotFoundError(message)
default:
return nil, &BillionVerifyError{Code: errResp.Error.Code, Message: message, StatusCode: resp.StatusCode}
}
}
// WaitForFileJobCompletion polls for file job completion with optional long-polling.
func (c *Client) WaitForFileJobCompletion(ctx context.Context, jobID string, pollInterval, maxWait time.Duration) (*FileJobResponse, error) {
if pollInterval == 0 {
pollInterval = 5 * time.Second
}
if maxWait == 0 {
maxWait = 10 * time.Minute
}
deadline := time.Now().Add(maxWait)
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
for {
status, err := c.GetFileJobStatus(ctx, jobID, nil)
if err != nil {
return nil, err
}
if status.Status == JobStatusCompleted || status.Status == JobStatusFailed {
return status, nil
}
if time.Now().After(deadline) {
return nil, NewTimeoutError(fmt.Sprintf("File job %s did not complete within %v", jobID, maxWait))
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-ticker.C:
continue
}
}
}
// GetCredits gets current credit balance.
func (c *Client) GetCredits(ctx context.Context) (*CreditsResponse, error) {
var apiResp struct {
Success bool `json:"success"`
Code string `json:"code"`
Message string `json:"message"`
Data CreditsResponse `json:"data"`
}
if err := c.request(ctx, http.MethodGet, "/v1/credits", nil, &apiResp); err != nil {
return nil, err
}
return &apiResp.Data, nil
}
// CreateWebhook creates a new webhook.
func (c *Client) CreateWebhook(ctx context.Context, config WebhookConfig) (*Webhook, error) {
var apiResp struct {
Success bool `json:"success"`
Code string `json:"code"`
Message string `json:"message"`
Data Webhook `json:"data"`
}
if err := c.request(ctx, http.MethodPost, "/v1/webhooks", config, &apiResp); err != nil {
return nil, err
}
return &apiResp.Data, nil
}
// ListWebhooks lists all webhooks.
func (c *Client) ListWebhooks(ctx context.Context) (*WebhooksListResponse, error) {
var apiResp struct {
Success bool `json:"success"`
Code string `json:"code"`
Message string `json:"message"`
Data WebhooksListResponse `json:"data"`
}
if err := c.request(ctx, http.MethodGet, "/v1/webhooks", nil, &apiResp); err != nil {
return nil, err
}
return &apiResp.Data, nil
}
// DeleteWebhook deletes a webhook.
func (c *Client) DeleteWebhook(ctx context.Context, webhookID string) error {
return c.request(ctx, http.MethodDelete, "/v1/webhooks/"+webhookID, nil, nil)
}
// VerifyWebhookSignature verifies a webhook signature.
func VerifyWebhookSignature(payload, signature, secret string) bool {
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(payload))
expectedSig := "sha256=" + hex.EncodeToString(mac.Sum(nil))
return hmac.Equal([]byte(signature), []byte(expectedSig))
}
// Deprecated: VerifyBulk is deprecated. Use VerifyBatch for synchronous batch verification
// or UploadFile for asynchronous file-based verification.
func (c *Client) VerifyBulk(ctx context.Context, emails []string, opts *BatchVerifyOptions) (*BatchVerifyResponse, error) {
return c.VerifyBatch(ctx, emails, opts)
}