From 04c18bc3a7ddb42e93d076295fea145185bb1292 Mon Sep 17 00:00:00 2001 From: Jason Hancock Date: Wed, 26 Jun 2019 13:47:27 -0700 Subject: [PATCH] Upgrade to v2 of the API --- zerobounce/api.go | 88 +++++++++++++++++------------------------- zerobounce/api_test.go | 15 +++---- 2 files changed, 43 insertions(+), 60 deletions(-) diff --git a/zerobounce/api.go b/zerobounce/api.go index 2c9e048..f8ddfac 100644 --- a/zerobounce/api.go +++ b/zerobounce/api.go @@ -6,10 +6,11 @@ import ( "fmt" "net/http" "net/url" + "strconv" ) // URI - -const URI = "https://api.zerobounce.net/v1" +const URI = "https://api.zerobounce.net/v2" // BuildURL - func (z *ZeroBounce) BuildURL(endpoint string, params ...url.Values) string { @@ -19,23 +20,22 @@ func (z *ZeroBounce) BuildURL(endpoint string, params ...url.Values) string { value = params[0] } - value.Set("apikey", z.Apikey) + value.Set("api_key", z.Apikey) return fmt.Sprintf("%s/%s?%s", URI, endpoint, value.Encode()) } // Request - func Request(url string, object ResponseBase) error { response, err := http.Get(url) - if err != nil { return err } + defer response.Body.Close() if response.StatusCode != 200 { - return errors.New("server error") + return errors.New(fmt.Sprintf("server error %d", response.StatusCode)) } - defer response.Body.Close() return json.NewDecoder(response.Body).Decode(&object) } @@ -50,40 +50,25 @@ type ResponseBase interface { // ValidateResponse - type ValidateResponse struct { - Address string `json:"address"` - Status string `json:"status"` - SubStatus string `json:"sub_status"` - Account string `json:"account"` - Domain string `json:"domain"` - Disposable bool `json:"disposable"` - Toxic bool `json:"toxic"` - Firstname string `json:"firstname"` - Lastname string `json:"lastname"` - Gender string `json:"gender"` - Location interface{} `json:"location"` - Creationdate interface{} `json:"creationdate"` - Processedat string `json:"processedat"` -} - -// ValidateWithipResponse - -type ValidateWithipResponse struct { - Address string `json:"address"` - Status string `json:"status"` - SubStatus string `json:"sub_status"` - Account string `json:"account"` - Domain string `json:"domain"` - Disposable bool `json:"disposable"` - Toxic bool `json:"toxic"` - Firstname string `json:"firstname"` - Lastname string `json:"lastname"` - Gender string `json:"gender"` - Location interface{} `json:"location"` - Country string `json:"country"` - Region string `json:"region"` - City string `json:"city"` - Zipcode string `json:"zipcode"` - Creationdate interface{} `json:"creationdate"` - Processedat string `json:"processedat"` + Address string `json:"address"` + Status string `json:"status"` + SubStatus string `json:"sub_status"` + Account string `json:"account"` + Domain string `json:"domain"` + DidYouMean string `json:"did_you_mean"` + DomainAgeDays string `json:"domain_age_days"` + FreeEmail bool `json:"free_email"` + MXFound string `json:"mx_found"` + MXRecord string `json:"mx_record"` + SMTPPRovider string `json:"smtp_provider"` + Firstname string `json:"firstname"` + Lastname string `json:"lastname"` + Gender string `json:"gender"` + City string `json:"city"` + Region string `json:"region"` + Zipcode string `json:"zipcode"` + Country string `json:"country"` + ProcessedAt string `json:"processed_at"` } // GetCreditsResponse - @@ -93,37 +78,34 @@ type GetCreditsResponse struct { // Validate - func (z *ZeroBounce) Validate(email string) (*ValidateResponse, error) { - params := url.Values{} - params.Set("email", email) - - validate := &ValidateResponse{} - - err := Request(z.BuildURL("validate", params), validate) - return validate, err + return z.ValidateWithIP(email) } // GetCredits - -func (z *ZeroBounce) GetCredits() (string, error) { +func (z *ZeroBounce) GetCredits() (int, error) { credit := &GetCreditsResponse{} err := Request(z.BuildURL("getcredits"), credit) + if err != nil { + return 0, err + } - return credit.Credits, err + return strconv.Atoi(credit.Credits) } // ValidateWithip - -func (z *ZeroBounce) ValidateWithip(email string, ipaddress ...string) (*ValidateWithipResponse, error) { - validate := &ValidateWithipResponse{} +func (z *ZeroBounce) ValidateWithIP(email string, ipaddress ...string) (*ValidateResponse, error) { + validate := &ValidateResponse{} params := url.Values{} - addr := "99.123.12.122" + addr := "" if len(ipaddress) > 0 { addr = ipaddress[0] } - params.Set("ipaddress", addr) + params.Set("ip_address", addr) params.Set("email", email) - err := Request(z.BuildURL("validatewithip", params), validate) + err := Request(z.BuildURL("validate", params), validate) return validate, err } diff --git a/zerobounce/api_test.go b/zerobounce/api_test.go index 8b64182..5949016 100644 --- a/zerobounce/api_test.go +++ b/zerobounce/api_test.go @@ -17,9 +17,10 @@ func Test_should_get_95_credits(t *testing.T) { JSON(map[string]string{"Credits": "95"}) zero := ZeroBounce{Apikey: "xxxxxxx"} - result, _ := zero.GetCredits() + result, err := zero.GetCredits() - assert.Equal(t, result, "95") + assert.NoError(t, err) + assert.Equal(t, 95, result) } func Test_when_the_status_is_500_method_should_get_error(t *testing.T) { @@ -53,29 +54,29 @@ func Test_should_build_url_with_email(t *testing.T) { params.Set("email", "jonas@example.com") zero := ZeroBounce{Apikey: "xxxxxxx"} - assert.Equal(t, zero.BuildURL("score", params), "https://api.zerobounce.net/v1/score?apikey=xxxxxxx&email=jonas%40example.com") + assert.Equal(t, zero.BuildURL("score", params), "https://api.zerobounce.net/v2/score?api_key=xxxxxxx&email=jonas%40example.com") } func Test_should_get_url_without_query_string_params(t *testing.T) { zero := ZeroBounce{Apikey: "xxxxxxx"} - assert.Equal(t, zero.BuildURL("score"), "https://api.zerobounce.net/v1/score?apikey=xxxxxxx") + assert.Equal(t, zero.BuildURL("score"), "https://api.zerobounce.net/v2/score?api_key=xxxxxxx") } func Test_status_should_be_valid(t *testing.T) { defer gock.Off() - response := ValidateWithipResponse{ + response := ValidateResponse{ Address: "flowerjill@aol.com", Status: "Valid", } gock.New(URI). - Get("/validatewithip"). + Get("/validate"). Reply(200). JSON(response) zero := ZeroBounce{Apikey: "xxxxxxx"} - result, _ := zero.ValidateWithip("contato@heriquelopes.com.br") + result, _ := zero.ValidateWithIP("contato@heriquelopes.com.br", "99.123.12.122") assert.Equal(t, result.Address, response.Address) assert.Equal(t, result.Status, response.Status)