1
0
mirror of synced 2024-11-25 06:26:03 +03:00

TransportResponse types (#70)

This commit is contained in:
DanielWeiser 2021-01-15 14:51:24 +03:00 committed by GitHub
parent 14af54f7a1
commit ebded3077b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package v1
import ( import (
"errors" "errors"
"fmt"
"net/http" "net/http"
"time" "time"
) )
@ -518,3 +519,43 @@ func (o Originator) MarshalText() ([]byte, error) {
return nil, ErrInvalidOriginator 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,
},
}
}

View File

@ -99,3 +99,43 @@ func TestOriginator(t *testing.T) {
t.Run("MarshalJSON_Invalid", OriginatorMarshalJSONInvalid) 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)
}
})
}