TransportResponse types (#70)
This commit is contained in:
parent
14af54f7a1
commit
ebded3077b
41
v1/types.go
41
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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user