Skip to content

Commit

Permalink
Upgrade to v2 of the API
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonhancock committed Jun 26, 2019
1 parent afd7274 commit 04c18bc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 60 deletions.
88 changes: 35 additions & 53 deletions zerobounce/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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 -
Expand All @@ -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
}
15 changes: 8 additions & 7 deletions zerobounce/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 04c18bc

Please sign in to comment.