diff --git a/api/request.go b/api/request.go index 9f82127..5ffe02a 100644 --- a/api/request.go +++ b/api/request.go @@ -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 diff --git a/api/response.go b/api/response.go index 3ca6649..2e770f7 100644 --- a/api/response.go +++ b/api/response.go @@ -1,6 +1,7 @@ package api import ( + "encoding/json" "errors" ) @@ -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 != "" diff --git a/contacts/is_contact_in_list.go b/contacts/is_contact_in_list.go index 072c998..203a099 100644 --- a/contacts/is_contact_in_list.go +++ b/contacts/is_contact_in_list.go @@ -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 } diff --git a/contacts/is_contact_in_list_test.go b/contacts/is_contact_in_list_test.go index 3465d6e..b579301 100644 --- a/contacts/is_contact_in_list_test.go +++ b/contacts/is_contact_in_list_test.go @@ -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) { @@ -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") @@ -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) + } +} diff --git a/go.mod b/go.mod index c82c1f3..cf3deac 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5b89cfc --- /dev/null +++ b/go.sum @@ -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= diff --git a/unisender.go b/unisender.go index c95d0eb..17566a7 100644 --- a/unisender.go +++ b/unisender.go @@ -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. diff --git a/unisender_test.go b/unisender_test.go index 6ac5ac3..4b8aecb 100644 --- a/unisender_test.go +++ b/unisender_test.go @@ -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) {