Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ func (r *Request) Execute(method string, v interface{}) (err error) {
return
}

response := Response{
Result: v,
}
response := Response{Result: v}

if err = json.Unmarshal(body, &response); err != nil {
return
Expand Down
20 changes: 20 additions & 0 deletions api/response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"encoding/json"
"errors"
)

Expand All @@ -11,6 +12,25 @@ type Response struct {
Code string `json:"code,omitempty"`
}

type jsonResponse struct {
Result json.RawMessage `json:"result,omitempty"`
Error string `json:"error,omitempty"`
Code string `json:"code,omitempty"`
}

func (r *Response) UnmarshalJSON(data []byte) error {
var j jsonResponse
if err := json.Unmarshal(data, &j); err != nil {
return err
}
r.Error = j.Error
r.Code = j.Code
if !r.IsError() {
return json.Unmarshal(j.Result, &r.Result)
}
return nil
}

// IsError returns true if response has error.
func (r Response) IsError() bool {
return r.Error != "" || r.Code != ""
Expand Down
2 changes: 1 addition & 1 deletion contacts/is_contact_in_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *IsContactInListRequest) ConditionAnd() *IsContactInListRequest {

// Execute sends request to UniSender API and returns result.
func (r *IsContactInListRequest) Execute() (res bool, err error) {
err = r.request.Execute("isContactInList", &res)
err = r.request.Execute("isContactInLists", &res)
return
}

Expand Down
33 changes: 32 additions & 1 deletion contacts/is_contact_in_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/alexeyco/unisender/api"
"github.com/alexeyco/unisender/contacts"
"github.com/alexeyco/unisender/test"
"github.com/stretchr/testify/assert"
)

func TestIsContactInListRequest_ConditionOr(t *testing.T) {
Expand Down Expand Up @@ -96,7 +97,6 @@ func TestIsContactInListRequest_Execute(t *testing.T) {
var givenCondition string

expectedResult := true
var givenResult bool

req := test.NewRequest(func(req *http.Request) (res *http.Response, err error) {
givenEmail = req.FormValue("email")
Expand Down Expand Up @@ -144,3 +144,34 @@ func TestIsContactInListRequest_Execute(t *testing.T) {
t.Fatal("Results should be equal")
}
}

// Testing HTTP 200 respose with error body:
// {
// "error": "YE131008-12 [Invalid address contact \"invalid_email_string\"]",
// "code": "unspecified",
// "result": ""
// }
func TestIsContactInListRequest_Execute_Error(t *testing.T) {
const errorMessge = "YE131008-12 [Invalid address contact \"invalid_email_string\"]"
req := test.NewRequest(func(req *http.Request) (res *http.Response, err error) {
result := api.Response{
Error: errorMessge,
Code: "unspecified",
Result: "",
}
response, err := json.Marshal(&result)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer(response)),
}, nil
})
res, err := contacts.IsContactInList(req, "invalid_email_string", 1).
ConditionAnd().
Execute()
if assert.EqualError(t, err, errorMessge) {
assert.Equal(t, false, res)
}
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ module github.com/alexeyco/unisender

go 1.13

require github.com/hhatto/gocloc v0.3.1 // indirect
require (
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.4.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
)
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4 changes: 2 additions & 2 deletions unisender.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ func (u *UniSender) IsContactInList(email string, listIDs ...int64) *contacts.Is
// or several lists, and also allows you to add/change the values ​​of additional fields and labels.
//
// See https://www.unisender.com/en/support/api/contacts/subscribe/
func (u *UniSender) Subscribe() *contacts.SubscribeRequest {
return contacts.Subscribe(u.request())
func (u *UniSender) Subscribe(listIDs ...int64) *contacts.SubscribeRequest {
return contacts.Subscribe(u.request(), listIDs...)
}

// Unsubscribe returns request that unsubscribes the contact email or phone number from one or several lists.
Expand Down
2 changes: 1 addition & 1 deletion unisender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestUnisender(t *testing.T) {
"importContacts": func(usndr *unisender.UniSender) {
_, _ = usndr.ImportContacts(contacts.NewImportContactsCollection()).Execute()
},
"isContactInList": func(usndr *unisender.UniSender) {
"isContactInLists": func(usndr *unisender.UniSender) {
_, _ = usndr.IsContactInList("").Execute()
},
"subscribe": func(usndr *unisender.UniSender) {
Expand Down