Skip to content

Commit 36597cf

Browse files
authored
DE-1307 Make v4 validation a default one (#326)
1 parent ba82fd9 commit 36597cf

File tree

3 files changed

+24
-55
lines changed

3 files changed

+24
-55
lines changed

README.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,12 @@ import (
165165
"github.com/mailgun/mailgun-go/v4"
166166
)
167167

168-
// If your plan does not include email validations but you have an account,
169-
// use your Public Validation api key. If your plan does include email validations,
170-
// use your Private API key. You can find both the Private and
171-
// Public Validation API Keys in your Account Menu, under "Settings":
172-
// (https://app.mailgun.com/app/account/security)
168+
// Your plan should include email validations.
169+
// Use your Mailgun API key. You can find the Mailgun API keys in your Account Menu, under "Settings":
170+
// (https://app.mailgun.com/settings/api_security)
173171
var apiKey string = "your-api-key"
174172

175173
func main() {
176-
// To use the /v4 version of validations define MG_URL in the environment
177-
// as `https://api.mailgun.net/v4` or set `v.SetAPIBase("https://api.mailgun.net/v4")`
178-
179174
// Create an instance of the Validator
180175
v := mailgun.NewEmailValidator(apiKey)
181176

email_validation.go

+21-26
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ type EmailVerification struct {
5050
Result string `json:"result"`
5151
// Engagement results are a macro-level view that explain an email recipient’s propensity to engage.
5252
// https://documentation.mailgun.com/docs/inboxready/mailgun-validate/validate_engagement/
53-
//
54-
// Only for v4. To use the /v4 version of validations define MG_URL in the environment variable
55-
// as `https://api.mailgun.net/v4` or set `v.SetAPIBase("https://api.mailgun.net/v4")`
5653
Engagement *EngagementData `json:"engagement,omitempty"`
5754
}
5855

@@ -93,9 +90,11 @@ type EmailValidatorImpl struct {
9390
apiKey string
9491
}
9592

96-
// Creates a new validation instance.
97-
// * If a public key is provided, uses the public validation endpoints
98-
// * If a private key is provided, uses the private validation endpoints
93+
// NewEmailValidator creates a new validation instance.
94+
//
95+
// * For ValidateEmail use private key
96+
//
97+
// * For ParseAddresses use public key
9998
func NewEmailValidator(apiKey string) *EmailValidatorImpl {
10099
isPublicKey := false
101100

@@ -105,16 +104,19 @@ func NewEmailValidator(apiKey string) *EmailValidatorImpl {
105104
}
106105

107106
return &EmailValidatorImpl{
107+
// TODO(vtopc): Don’t use http.DefaultClient - https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
108108
client: http.DefaultClient,
109109
isPublicKey: isPublicKey,
110-
apiBase: APIBase,
110+
apiBase: "https://api.mailgun.net/v4",
111111
apiKey: apiKey,
112112
}
113113
}
114114

115115
// NewEmailValidatorFromEnv returns a new EmailValidator using environment variables
116-
// If MG_PUBLIC_API_KEY is set, assume using the free validation subject to daily usage limits
117-
// If only MG_API_KEY is set, assume using the /private validation routes with no daily usage limits
116+
//
117+
// * For ValidateEmail set MG_API_KEY
118+
//
119+
// * For ParseAddresses set MG_PUBLIC_API_KEY
118120
func NewEmailValidatorFromEnv() (*EmailValidatorImpl, error) {
119121
apiKey := os.Getenv("MG_PUBLIC_API_KEY")
120122
if apiKey == "" {
@@ -167,27 +169,15 @@ func (m *EmailValidatorImpl) getAddressURL(endpoint string) string {
167169
// ValidateEmail performs various checks on the email address provided to ensure it's correctly formatted.
168170
// It may also be used to break an email address into its sub-components. If user has set the
169171
func (m *EmailValidatorImpl) ValidateEmail(ctx context.Context, email string, mailBoxVerify bool) (EmailVerification, error) {
170-
if strings.HasSuffix(m.APIBase(), "/v4") {
171-
return m.validateV4(ctx, email, mailBoxVerify)
172+
if m.isPublicKey {
173+
return EmailVerification{}, errors.New("ValidateEmail: public key is not supported anymore, use private key")
172174
}
173-
return m.validateV3(ctx, email, mailBoxVerify)
174-
}
175175

176-
func (m *EmailValidatorImpl) validateV3(ctx context.Context, email string, mailBoxVerify bool) (EmailVerification, error) {
177-
r := newHTTPRequest(m.getAddressURL("validate"))
178-
r.setClient(m.Client())
179-
r.addParameter("address", email)
180-
if mailBoxVerify {
181-
r.addParameter("mailbox_verification", "true")
176+
if strings.HasSuffix(m.APIBase(), "/v4") {
177+
return m.validateV4(ctx, email, mailBoxVerify)
182178
}
183-
r.setBasicAuth(basicAuthUser, m.APIKey())
184179

185-
var res EmailVerification
186-
err := getResponseFromJSON(ctx, r, &res)
187-
if err != nil {
188-
return EmailVerification{}, err
189-
}
190-
return res, nil
180+
return EmailVerification{}, errors.New("ValidateEmail: only v4 is supported")
191181
}
192182

193183
func (m *EmailValidatorImpl) validateV4(ctx context.Context, email string, mailBoxVerify bool) (EmailVerification, error) {
@@ -221,6 +211,11 @@ func (m *EmailValidatorImpl) validateV4(ctx context.Context, email string, mailB
221211

222212
// ParseAddresses takes a list of addresses and sorts them into valid and invalid address categories.
223213
// NOTE: Use of this function requires a proper public API key. The private API key will not work.
214+
//
215+
// NOTE: Only for v3. To use the /v3 version of validations define MG_URL in the environment variable
216+
// as `https://api.mailgun.net/v3` or set `v.SetAPIBase("https://api.mailgun.net/v3")`
217+
//
218+
// Deprecated: /v3/address/parse is deprecated use ValidateEmail instead.
224219
func (m *EmailValidatorImpl) ParseAddresses(ctx context.Context, addresses ...string) ([]string, []string, error) {
225220
r := newHTTPRequest(m.getAddressURL("parse"))
226221
r.setClient(m.Client())

email_validation_test.go

-21
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,6 @@ import (
99
"github.com/mailgun/mailgun-go/v4"
1010
)
1111

12-
func TestEmailValidationV3(t *testing.T) {
13-
v := mailgun.NewEmailValidator(testKey)
14-
// API Base is set to `http://server/v3`
15-
v.SetAPIBase(server.URL())
16-
ctx := context.Background()
17-
18-
ev, err := v.ValidateEmail(ctx, "[email protected]", false)
19-
ensure.Nil(t, err)
20-
21-
ensure.True(t, ev.IsValid)
22-
ensure.DeepEqual(t, ev.MailboxVerification, "")
23-
ensure.False(t, ev.IsDisposableAddress)
24-
ensure.False(t, ev.IsRoleAddress)
25-
ensure.True(t, ev.Parts.DisplayName == "")
26-
ensure.DeepEqual(t, ev.Parts.LocalPart, "foo")
27-
ensure.DeepEqual(t, ev.Parts.Domain, "mailgun.com")
28-
ensure.DeepEqual(t, ev.Reason, "no-reason")
29-
ensure.True(t, len(ev.Reasons) == 0)
30-
ensure.DeepEqual(t, ev.Risk, "unknown")
31-
}
32-
3312
func TestEmailValidationV4(t *testing.T) {
3413
v := mailgun.NewEmailValidator(testKey)
3514
// API Base is set to `http://server/v4`

0 commit comments

Comments
 (0)