-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (156 loc) · 5.47 KB
/
main.go
File metadata and controls
171 lines (156 loc) · 5.47 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
// Package main demonstrates basic usage of the BillionVerify Go SDK.
// This example shows single email verification, batch verification,
// getting credits, and health check.
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"time"
billionverify "github.com/BillionVerify/billionverify-go"
)
func main() {
// Get API key from environment variable
apiKey := os.Getenv("EMAILVERIFY_API_KEY")
if apiKey == "" {
log.Fatal("EMAILVERIFY_API_KEY environment variable is required")
}
// Create a new client
client, err := billionverify.NewClient(billionverify.Config{
APIKey: apiKey,
Timeout: 30 * time.Second,
Retries: 3,
})
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// 1. Health Check (no authentication required)
fmt.Println("=== Health Check ===")
health, err := client.Health(ctx)
if err != nil {
log.Printf("Health check failed: %v", err)
} else {
fmt.Printf("API Status: %s\n", health.Status)
fmt.Printf("Server Time: %d\n", health.Time)
}
fmt.Println()
// 2. Get Credits
fmt.Println("=== Credits ===")
credits, err := client.GetCredits(ctx)
if err != nil {
handleError(err)
} else {
fmt.Printf("Account ID: %s\n", credits.AccountID)
fmt.Printf("API Key Name: %s\n", credits.APIKeyName)
fmt.Printf("Credits Balance: %d\n", credits.CreditsBalance)
fmt.Printf("Credits Consumed: %d\n", credits.CreditsConsumed)
fmt.Printf("Last Updated: %s\n", credits.LastUpdated)
}
fmt.Println()
// 3. Single Email Verification
fmt.Println("=== Single Email Verification ===")
result, err := client.Verify(ctx, "test@example.com", &billionverify.VerifyOptions{
CheckSMTP: true, // Perform SMTP verification
})
if err != nil {
handleError(err)
} else {
fmt.Printf("Email: %s\n", result.Email)
fmt.Printf("Status: %s\n", result.Status)
fmt.Printf("Score: %.2f\n", result.Score)
fmt.Printf("Is Deliverable: %t\n", result.IsDeliverable)
fmt.Printf("Is Disposable: %t\n", result.IsDisposable)
fmt.Printf("Is Catchall: %t\n", result.IsCatchall)
fmt.Printf("Is Role: %t\n", result.IsRole)
fmt.Printf("Is Free: %t\n", result.IsFree)
fmt.Printf("Domain: %s\n", result.Domain)
fmt.Printf("SMTP Check: %t\n", result.SMTPCheck)
fmt.Printf("Credits Used: %d\n", result.CreditsUsed)
if result.Reason != "" {
fmt.Printf("Reason: %s\n", result.Reason)
}
if result.Suggestion != "" {
fmt.Printf("Suggestion: %s\n", result.Suggestion)
}
if result.DomainReputation != nil {
fmt.Printf("Domain Reputation - Listed: %t\n", result.DomainReputation.IsListed)
}
}
fmt.Println()
// 4. Batch Verification (synchronous, max 50 emails)
fmt.Println("=== Batch Verification ===")
emails := []string{
"user1@example.com",
"user2@gmail.com",
"admin@company.org",
"info@business.net",
"test@disposable.email",
}
batchResult, err := client.VerifyBatch(ctx, emails, &billionverify.BatchVerifyOptions{
CheckSMTP: true,
})
if err != nil {
handleError(err)
} else {
fmt.Printf("Total Emails: %d\n", batchResult.TotalEmails)
fmt.Printf("Valid Emails: %d\n", batchResult.ValidEmails)
fmt.Printf("Invalid Emails: %d\n", batchResult.InvalidEmails)
fmt.Printf("Credits Used: %d\n", batchResult.CreditsUsed)
fmt.Printf("Process Time: %d ms\n", batchResult.ProcessTime)
fmt.Println()
fmt.Println("Individual Results:")
for _, item := range batchResult.Results {
fmt.Printf(" %s: status=%s, deliverable=%t, disposable=%t, catchall=%t\n",
item.Email, item.Status, item.IsDeliverable, item.IsDisposable, item.IsCatchall)
}
}
fmt.Println()
// 5. Verify with context timeout
fmt.Println("=== Verification with Context Timeout ===")
ctxWithTimeout, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
result2, err := client.Verify(ctxWithTimeout, "another@example.com", nil)
if err != nil {
handleError(err)
} else {
fmt.Printf("Email: %s, Status: %s\n", result2.Email, result2.Status)
}
fmt.Println()
// 6. Demonstrate status checking
fmt.Println("=== Status Constants ===")
fmt.Printf("Valid status: %s\n", billionverify.StatusValid)
fmt.Printf("Invalid status: %s\n", billionverify.StatusInvalid)
fmt.Printf("Unknown status: %s\n", billionverify.StatusUnknown)
fmt.Printf("Risky status: %s\n", billionverify.StatusRisky)
fmt.Printf("Disposable status: %s\n", billionverify.StatusDisposable)
fmt.Printf("Catchall status: %s\n", billionverify.StatusCatchall)
fmt.Printf("Role status: %s\n", billionverify.StatusRole)
}
// handleError demonstrates proper error handling for the SDK
func handleError(err error) {
var authErr *billionverify.AuthenticationError
var rateLimitErr *billionverify.RateLimitError
var validationErr *billionverify.ValidationError
var creditsErr *billionverify.InsufficientCreditsError
var notFoundErr *billionverify.NotFoundError
var timeoutErr *billionverify.TimeoutError
switch {
case errors.As(err, &authErr):
log.Printf("Authentication error: Invalid API key")
case errors.As(err, &rateLimitErr):
log.Printf("Rate limit exceeded. Retry after %d seconds", rateLimitErr.RetryAfter)
case errors.As(err, &validationErr):
log.Printf("Validation error: %s (details: %s)", validationErr.Message, validationErr.Details)
case errors.As(err, &creditsErr):
log.Printf("Insufficient credits: %s", creditsErr.Message)
case errors.As(err, ¬FoundErr):
log.Printf("Resource not found: %s", notFoundErr.Message)
case errors.As(err, &timeoutErr):
log.Printf("Request timed out: %s", timeoutErr.Message)
default:
log.Printf("Unexpected error: %v", err)
}
}