feat: add sonarr support
This commit is contained in:
parent
249db98ac7
commit
456c7b2018
|
@ -6,10 +6,13 @@ type ErrorCode uint8
|
|||
|
||||
const (
|
||||
ErrorCodeUnknown ErrorCode = iota
|
||||
ErrorCodeValidation
|
||||
)
|
||||
|
||||
func (e ErrorCode) String() string {
|
||||
switch e {
|
||||
case ErrorCodeValidation:
|
||||
return "validation-error"
|
||||
case ErrorCodeUnknown:
|
||||
fallthrough
|
||||
default:
|
||||
|
|
|
@ -13,6 +13,7 @@ func TestErrorCode_String(t *testing.T) {
|
|||
t.Parallel()
|
||||
|
||||
assert.Equal(t, domain.ErrorCodeUnknown.String(), "internal-server-error")
|
||||
assert.Equal(t, domain.ErrorCodeValidation.String(), "validation-error")
|
||||
}
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
|
@ -25,7 +26,7 @@ func TestError(t *testing.T) {
|
|||
expectedMsg string
|
||||
}{
|
||||
{
|
||||
name: "OK: WithCode, WithMessage",
|
||||
name: "OK: WithCode(domain.ErrorCodeUnknown), WithMessage",
|
||||
options: []domain.ErrorOption{
|
||||
domain.WithCode(domain.ErrorCodeUnknown),
|
||||
domain.WithMessage("err err"),
|
||||
|
@ -35,7 +36,17 @@ func TestError(t *testing.T) {
|
|||
expectedMsg: "err err",
|
||||
},
|
||||
{
|
||||
name: "OK: WithCode, WithMessagef, WithErr",
|
||||
name: "OK: WithCode(domain.ErrorCodeValidation), WithMessage",
|
||||
options: []domain.ErrorOption{
|
||||
domain.WithCode(domain.ErrorCodeValidation),
|
||||
domain.WithMessage("err err"),
|
||||
},
|
||||
expectedCode: domain.ErrorCodeValidation,
|
||||
expectedErr: nil,
|
||||
expectedMsg: "err err",
|
||||
},
|
||||
{
|
||||
name: "OK: WithCode(domain.ErrorCodeUnknown), WithMessagef, WithErr",
|
||||
options: []domain.ErrorOption{
|
||||
domain.WithCode(domain.ErrorCodeUnknown),
|
||||
domain.WithMessagef("xxx %d", 25),
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package domain
|
||||
|
||||
type SonarrEventType string
|
||||
|
||||
const (
|
||||
SonarrEventTypeDownload SonarrEventType = "Download"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnsupportedSonarrEventType = NewError(WithCode(ErrorCodeValidation), WithMessage("unsupported event type"))
|
||||
)
|
||||
|
||||
func NewSonarrEventType(s string) (SonarrEventType, error) {
|
||||
if s != SonarrEventTypeDownload.String() {
|
||||
return "", ErrUnsupportedSonarrEventType
|
||||
}
|
||||
|
||||
return SonarrEventTypeDownload, nil
|
||||
}
|
||||
|
||||
func (s SonarrEventType) String() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
type SonarrSeries struct {
|
||||
ID int64
|
||||
Title string
|
||||
}
|
||||
|
||||
type SonarrEpisode struct {
|
||||
ID int64
|
||||
EpisodeNumber int16
|
||||
SeasonNumber int16
|
||||
Title string
|
||||
}
|
||||
|
||||
type SonarrWebhookPayload struct {
|
||||
EventType SonarrEventType
|
||||
Series SonarrSeries
|
||||
Episodes []SonarrEpisode
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package domain_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"gitea.dwysokinski.me/Kichiyaki/notificationarr/internal/domain"
|
||||
)
|
||||
|
||||
func TestNewSonarrEventType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
events := []domain.SonarrEventType{domain.SonarrEventTypeDownload}
|
||||
|
||||
for _, ev := range events {
|
||||
res, err := domain.NewSonarrEventType(ev.String())
|
||||
assert.Equal(t, ev, res)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ERR: invalid event type", func(t *testing.T) {
|
||||
events := []string{"test1", "test2", "aaaa", "bbb"}
|
||||
|
||||
for _, ev := range events {
|
||||
res, err := domain.NewSonarrEventType(ev)
|
||||
assert.Zero(t, res)
|
||||
assert.ErrorIs(t, err, domain.ErrUnsupportedSonarrEventType)
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue