From 7e12fef1bb4fd80bde549ce872cb810c3e9971ec Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Wed, 18 Dec 2024 20:08:33 +0200 Subject: [PATCH] DE-1394 Remove deprecated v3 Validation (#371) --- email_validation.go | 76 ++++++++-------------------------------- email_validation_test.go | 20 ----------- examples/examples.go | 13 ------- examples_test.go | 24 ------------- 4 files changed, 14 insertions(+), 119 deletions(-) diff --git a/email_validation.go b/email_validation.go index b1ff5c46..c294fc81 100644 --- a/email_validation.go +++ b/email_validation.go @@ -84,48 +84,32 @@ type EmailValidator interface { } type EmailValidatorImpl struct { - client *http.Client - isPublicKey bool - apiBase string - apiKey string + client *http.Client + apiBase string + apiKey string } // NewEmailValidator creates a new validation instance. -// -// * For ValidateEmail use private key -// -// * For ParseAddresses use public key +// Use private key. func NewEmailValidator(apiKey string) *EmailValidatorImpl { - isPublicKey := false - - // Did the user pass in a public key? - if strings.HasPrefix(apiKey, "pubkey-") { - isPublicKey = true - } - return &EmailValidatorImpl{ // TODO(vtopc): Don’t use http.DefaultClient - https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779 - client: http.DefaultClient, - isPublicKey: isPublicKey, - apiBase: "https://api.mailgun.net/v4", - apiKey: apiKey, + client: http.DefaultClient, + apiBase: "https://api.mailgun.net/v4", + apiKey: apiKey, } } // NewEmailValidatorFromEnv returns a new EmailValidator using environment variables // -// * For ValidateEmail set MG_API_KEY -// -// * For ParseAddresses set MG_PUBLIC_API_KEY +// Set MG_API_KEY func NewEmailValidatorFromEnv() (*EmailValidatorImpl, error) { - apiKey := os.Getenv("MG_PUBLIC_API_KEY") + apiKey := os.Getenv("MG_API_KEY") if apiKey == "" { - apiKey = os.Getenv("MG_API_KEY") - if apiKey == "" { - return nil, errors.New( - "environment variable MG_PUBLIC_API_KEY or MG_API_KEY required for email validation") - } + return nil, errors.New( + "environment variable MG_API_KEY required for email validation") } + v := NewEmailValidator(apiKey) url := os.Getenv("MG_URL") if url != "" { @@ -159,20 +143,11 @@ func (m *EmailValidatorImpl) APIKey() string { return m.apiKey } -func (m *EmailValidatorImpl) getAddressURL(endpoint string) string { - if m.isPublicKey { - return fmt.Sprintf("%s/address/%s", m.APIBase(), endpoint) - } - return fmt.Sprintf("%s/address/private/%s", m.APIBase(), endpoint) -} - // ValidateEmail performs various checks on the email address provided to ensure it's correctly formatted. // It may also be used to break an email address into its sub-components. If user has set the +// TODO(DE-1384): move to *MailgunImpl? func (m *EmailValidatorImpl) ValidateEmail(ctx context.Context, email string, mailBoxVerify bool) (EmailVerification, error) { - if m.isPublicKey { - return EmailVerification{}, errors.New("ValidateEmail: public key is not supported anymore, use private key") - } - + // TODO(DE-1383): remove check: if strings.HasSuffix(m.APIBase(), "/v4") { return m.validateV4(ctx, email, mailBoxVerify) } @@ -208,26 +183,3 @@ func (m *EmailValidatorImpl) validateV4(ctx context.Context, email string, mailB Engagement: res.Engagement, }, nil } - -// ParseAddresses takes a list of addresses and sorts them into valid and invalid address categories. -// NOTE: Use of this function requires a proper public API key. The private API key will not work. -// -// NOTE: Only for v3. To use the /v3 version of validations define MG_URL in the environment variable -// as `https://api.mailgun.net/v3` or set `v.SetAPIBase("https://api.mailgun.net/v3")` -// -// Deprecated: /v3/address/parse is deprecated use ValidateEmail instead. -// TODO(v5): remove -func (m *EmailValidatorImpl) ParseAddresses(ctx context.Context, addresses ...string) (parsed, unparseable []string, err error) { - r := newHTTPRequest(m.getAddressURL("parse")) - r.setClient(m.Client()) - r.addParameter("addresses", strings.Join(addresses, ",")) - r.setBasicAuth(basicAuthUser, m.APIKey()) - - var response addressParseResult - err = getResponseFromJSON(ctx, r, &response) - if err != nil { - return nil, nil, err - } - - return response.Parsed, response.Unparseable, nil -} diff --git a/email_validation_test.go b/email_validation_test.go index 790e520b..53d4c0c0 100644 --- a/email_validation_test.go +++ b/email_validation_test.go @@ -36,26 +36,6 @@ func TestEmailValidationV4(t *testing.T) { assert.False(t, ev.Engagement.IsBot) } -func TestParseAddresses(t *testing.T) { - v := mailgun.NewEmailValidator(testKey) - v.SetAPIBase(server.URL()) - ctx := context.Background() - - addressesThatParsed, unparsableAddresses, err := v.ParseAddresses(ctx, - "Alice ", - "bob@example.com", - "example.com") - require.NoError(t, err) - hittest := map[string]bool{ - "Alice ": true, - "bob@example.com": true, - } - for _, a := range addressesThatParsed { - require.True(t, hittest[a]) - } - require.Len(t, unparsableAddresses, 1) -} - func TestUnmarshallResponse(t *testing.T) { payload := []byte(`{ "address": "some_email@aol.com", diff --git a/examples/examples.go b/examples/examples.go index da15c7e6..86b82953 100644 --- a/examples/examples.go +++ b/examples/examples.go @@ -545,19 +545,6 @@ func ListMailingLists(domain, apiKey string) ([]mailgun.MailingList, error) { return result, nil } -func ParseAddress(apiKey string) ([]string, []string, error) { - mv := mailgun.NewEmailValidator(apiKey) - - ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) - defer cancel() - - return mv.ParseAddresses(ctx, - "Alice ", - "bob@example.com", - // ... - ) -} - func GetRoute(domain, apiKey string) (mailgun.Route, error) { mg := mailgun.NewMailgun(domain, apiKey) diff --git a/examples_test.go b/examples_test.go index 7018765c..aefd03c7 100644 --- a/examples_test.go +++ b/examples_test.go @@ -35,30 +35,6 @@ func ExampleEmailValidatorImpl_ValidateEmail() { } } -func ExampleEmailValidatorImpl_ParseAddresses() { - v := mailgun.NewEmailValidator("my_public_validation_api_key") - - ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) - defer cancel() - - addressesThatParsed, unparsableAddresses, err := v.ParseAddresses(ctx, "Alice ", "bob@example.com", "example.com") - if err != nil { - log.Fatal(err) - } - hittest := map[string]bool{ - "Alice ": true, - "bob@example.com": true, - } - for _, a := range addressesThatParsed { - if !hittest[a] { - log.Fatalf("Expected %s to be parsable", a) - } - } - if len(unparsableAddresses) != 1 { - log.Fatalf("Expected 1 address to be unparsable; got %d", len(unparsableAddresses)) - } -} - func ExampleMailgunImpl_UpdateMailingList() { mg := mailgun.NewMailgun("example.com", "my_api_key")