From ebded3077b4cdf3e8761b2bc2af2dc5bd22f31a3 Mon Sep 17 00:00:00 2001 From: DanielWeiser <46303277+DanielWeiser@users.noreply.github.com> Date: Fri, 15 Jan 2021 14:51:24 +0300 Subject: [PATCH] TransportResponse types (#70) --- v1/types.go | 41 +++++++++++++++++++++++++++++++++++++++++ v1/types_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/v1/types.go b/v1/types.go index 1c227fc..16710b3 100644 --- a/v1/types.go +++ b/v1/types.go @@ -2,6 +2,7 @@ package v1 import ( "errors" + "fmt" "net/http" "time" ) @@ -518,3 +519,43 @@ func (o Originator) MarshalText() ([]byte, error) { return nil, ErrInvalidOriginator } + +type TransportErrorCode string + +const ( + MessageErrorGeneral TransportErrorCode = "general" + MessageErrorCustomerNotExists TransportErrorCode = "customer_not_exists" + MessageErrorReplyTimedOut TransportErrorCode = "reply_timed_out" + MessageErrorSpamSuspicion TransportErrorCode = "spam_suspicion" + MessageErrorAccessRestricted TransportErrorCode = "access_restricted" +) + +type TransportResponse struct { + ExternalMessageID string `json:"external_message_id,omitempty"` + Error *TransportError `json:"error,omitempty"` +} + +type TransportError struct { + Code TransportErrorCode `json:"code"` + Message string `json:"message,omitempty"` +} + +func (t TransportErrorCode) MarshalJSON() ([]byte, error) { + if t == "" { + return []byte(fmt.Sprintf(`"%s"`, MessageErrorGeneral)), nil + } + return []byte(fmt.Sprintf(`"%s"`, t)), nil +} + +func NewSentMessageResponse(externalMessageID string) TransportResponse { + return TransportResponse{ExternalMessageID: externalMessageID} +} + +func NewTransportErrorResponse(code TransportErrorCode, message string) TransportResponse { + return TransportResponse{ + Error: &TransportError{ + Code: code, + Message: message, + }, + } +} diff --git a/v1/types_test.go b/v1/types_test.go index c69cfbf..64ea9b8 100644 --- a/v1/types_test.go +++ b/v1/types_test.go @@ -99,3 +99,43 @@ func TestOriginator(t *testing.T) { t.Run("MarshalJSON_Invalid", OriginatorMarshalJSONInvalid) } + +func TestTransportErrorResponse(t *testing.T) { + t.Run("NewTransportErrorResponse", func(t *testing.T) { + expected := TransportResponse{ + Error: &TransportError{ + Code: MessageErrorGeneral, + Message: "error", + }, + } + resp := NewTransportErrorResponse(MessageErrorGeneral, "error") + assert.Equal(t, expected, resp) + }) + + t.Run("NewSentMessageResponse", func(t *testing.T) { + expected := TransportResponse{ + ExternalMessageID: "extID", + } + resp := NewSentMessageResponse("extID") + assert.Equal(t, expected, resp) + }) + + t.Run("MarshalJSON", func(t *testing.T) { + cases := []struct { + byte byte + expected string + }{ + {1, "general"}, + {2, "customer_not_exists"}, + {3, "reply_timed_out"}, + {4, "spam_suspicion"}, + {5, "access_restricted"}, + } + for _, c := range cases { + result, err := TransportErrorCode(c.expected).MarshalJSON() + expected := []byte(fmt.Sprintf(`"%s"`, c.expected)) + assert.NoError(t, err) + assert.Equal(t, expected, result) + } + }) +}