Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sonarr): add media management tab resources #51

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions sonarr/mediamanagement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package sonarr

import (
"bytes"
"context"
"encoding/json"
"fmt"
)

type MediaManagement struct {
AutoUnmonitorPreviouslyDownloadedEpisodes bool `json:"autoUnmonitorPreviouslyDownloadedEpisodes,omitempty"`
CopyUsingHardlinks bool `json:"copyUsingHardlinks,omitempty"`
CreateEmptySeriesFolders bool `json:"createEmptySeriesFolders,omitempty"`
DeleteEmptyFolders bool `json:"deleteEmptyFolders,omitempty"`
EnableMediaInfo bool `json:"enableMediaInfo,omitempty"`
ImportExtraFiles bool `json:"importExtraFiles,omitempty"`
SetPermissionsLinux bool `json:"setPermissionsLinux,omitempty"`
SkipFreeSpaceCheckWhenImporting bool `json:"skipFreeSpaceCheckWhenImporting,omitempty"`
ID int64 `json:"id,omitempty"`
MinimumFreeSpaceWhenImporting int64 `json:"minimumFreeSpaceWhenImporting,omitempty"`
RecycleBinCleanupDays int64 `json:"recycleBinCleanupDays,omitempty"`
ChmodFolder string `json:"chmodFolder,omitempty"`
ChownGroup string `json:"chownGroup,omitempty"`
DownloadPropersAndRepacks string `json:"downloadPropersAndRepacks,omitempty"`
EpisodeTitleRequired string `json:"episodeTitleRequired,omitempty"`
ExtraFileExtensions string `json:"extraFileExtensions,omitempty"`
FileDate string `json:"fileDate,omitempty"`
RecycleBin string `json:"recycleBin,omitempty"`
RescanAfterRefresh string `json:"rescanAfterRefresh,omitempty"`
}

// Define Base Path for MediaManagement calls.
const bpMediaManagement = APIver + "/config/mediaManagement"

// GetMediaManagement returns the mediaManagement.
func (s *Sonarr) GetMediaManagement() (*MediaManagement, error) {
return s.GetMediaManagementContext(context.Background())
}

func (s *Sonarr) GetMediaManagementContext(ctx context.Context) (*MediaManagement, error) {
var output *MediaManagement

if _, err := s.GetInto(ctx, bpMediaManagement, nil, &output); err != nil {
return nil, fmt.Errorf("api.Get(mediaManagement): %w", err)
}

return output, nil
}

// UpdateMediaManagement updates the mediaManagement.
func (s *Sonarr) UpdateMediaManagement(mMgt *MediaManagement) (*MediaManagement, error) {
return s.UpdateMediaManagementContext(context.Background(), mMgt)
}

func (s *Sonarr) UpdateMediaManagementContext(ctx context.Context, mMgt *MediaManagement) (*MediaManagement, error) {
var output MediaManagement

var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(mMgt); err != nil {
return nil, fmt.Errorf("json.Marshal(mediaManagement): %w", err)
}

if _, err := s.PutInto(ctx, bpMediaManagement, nil, &body, &output); err != nil {
return nil, fmt.Errorf("api.Put(mediaManagement): %w", err)
}

return &output, nil
}
154 changes: 154 additions & 0 deletions sonarr/mediamanagement_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package sonarr_test

import (
"path"
"testing"

"github.com/stretchr/testify/assert"
"golift.io/starr"
"golift.io/starr/sonarr"
)

const mediaManagementBody = `{
"autoUnmonitorPreviouslyDownloadedEpisodes": false,
"recycleBin": "",
"recycleBinCleanupDays": 7,
"downloadPropersAndRepacks": "preferAndUpgrade",
"createEmptySeriesFolders": false,
"deleteEmptyFolders": false,
"fileDate": "none",
"rescanAfterRefresh": "always",
"setPermissionsLinux": false,
"chmodFolder": "755",
"chownGroup": "",
"episodeTitleRequired": "always",
"skipFreeSpaceCheckWhenImporting": false,
"minimumFreeSpaceWhenImporting": 100,
"copyUsingHardlinks": true,
"importExtraFiles": false,
"extraFileExtensions": "srt",
"enableMediaInfo": true,
"id": 1
}`

func TestGetMediaManagement(t *testing.T) {
t.Parallel()

tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "config/mediaManagement"),
ExpectedMethod: "GET",
ResponseStatus: 200,
ResponseBody: mediaManagementBody,
WithResponse: &sonarr.MediaManagement{
ID: 1,
AutoUnmonitorPreviouslyDownloadedEpisodes: false,
RecycleBin: "",
RecycleBinCleanupDays: 7,
DownloadPropersAndRepacks: "preferAndUpgrade",
CreateEmptySeriesFolders: false,
DeleteEmptyFolders: false,
FileDate: "none",
RescanAfterRefresh: "always",
SetPermissionsLinux: false,
ChmodFolder: "755",
ChownGroup: "",
EpisodeTitleRequired: "always",
SkipFreeSpaceCheckWhenImporting: false,
MinimumFreeSpaceWhenImporting: 100,
CopyUsingHardlinks: true,
ImportExtraFiles: false,
ExtraFileExtensions: "srt",
EnableMediaInfo: true,
},
WithError: nil,
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "config/mediaManagement"),
ExpectedMethod: "GET",
ResponseStatus: 404,
ResponseBody: `{"message": "NotFound"}`,
WithError: starr.ErrInvalidStatusCode,
WithResponse: (*sonarr.MediaManagement)(nil),
},
}

for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := test.GetMockServer(t)
client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
output, err := client.GetMediaManagement()
assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
})
}
}

func TestUpdateMediaManagement(t *testing.T) {
t.Parallel()

tests := []*starr.TestMockData{
{
Name: "202",
ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "config/mediaManagement"),
ExpectedMethod: "PUT",
ResponseStatus: 202,
WithRequest: &sonarr.MediaManagement{
EnableMediaInfo: true,
},
ExpectedRequest: `{"enableMediaInfo":true}` + "\n",
ResponseBody: mediaManagementBody,
WithResponse: &sonarr.MediaManagement{
ID: 1,
AutoUnmonitorPreviouslyDownloadedEpisodes: false,
RecycleBin: "",
RecycleBinCleanupDays: 7,
DownloadPropersAndRepacks: "preferAndUpgrade",
CreateEmptySeriesFolders: false,
DeleteEmptyFolders: false,
FileDate: "none",
RescanAfterRefresh: "always",
SetPermissionsLinux: false,
ChmodFolder: "755",
ChownGroup: "",
EpisodeTitleRequired: "always",
SkipFreeSpaceCheckWhenImporting: false,
MinimumFreeSpaceWhenImporting: 100,
CopyUsingHardlinks: true,
ImportExtraFiles: false,
ExtraFileExtensions: "srt",
EnableMediaInfo: true,
},
WithError: nil,
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, sonarr.APIver, "config/mediaManagement"),
ExpectedMethod: "PUT",
WithRequest: &sonarr.MediaManagement{
EnableMediaInfo: true,
},
ExpectedRequest: `{"enableMediaInfo":true}` + "\n",
ResponseStatus: 404,
ResponseBody: `{"message": "NotFound"}`,
WithError: starr.ErrInvalidStatusCode,
WithResponse: (*sonarr.MediaManagement)(nil),
},
}

for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := test.GetMockServer(t)
client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
output, err := client.UpdateMediaManagement(test.WithRequest.(*sonarr.MediaManagement))
assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
})
}
}
65 changes: 65 additions & 0 deletions sonarr/naming.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package sonarr

import (
"bytes"
"context"
"encoding/json"
"fmt"
)

type Naming struct {
RenameEpisodes bool `json:"renameEpisodes,omitempty"`
ReplaceIllegalCharacters bool `json:"replaceIllegalCharacters,omitempty"`
IncludeQuality bool `json:"includeQuality,omitempty"`
IncludeSeriesTitle bool `json:"includeSeriesTitle,omitempty"`
IncludeEpisodeTitle bool `json:"includeEpisodeTitle,omitempty"`
ReplaceSpaces bool `json:"replaceSpaces,omitempty"`
ID int64 `json:"id,omitempty"`
MultiEpisodeStyle int64 `json:"multiEpisodeStyle,omitempty"`
Separator string `json:"separator,omitempty"`
NumberStyle string `json:"numberStyle,omitempty"`
DailyEpisodeFormat string `json:"dailyEpisodeFormat,omitempty"`
AnimeEpisodeFormat string `json:"animeEpisodeFormat,omitempty"`
SeriesFolderFormat string `json:"seriesFolderFormat,omitempty"`
SeasonFolderFormat string `json:"seasonFolderFormat,omitempty"`
SpecialsFolderFormat string `json:"specialsFolderFormat,omitempty"`
StandardEpisodeFormat string `json:"standardEpisodeFormat,omitempty"`
}

// Define Base Path for Naming calls.
const bpNaming = APIver + "/config/naming"

// GetNaming returns the naming.
func (s *Sonarr) GetNaming() (*Naming, error) {
return s.GetNamingContext(context.Background())
}

func (s *Sonarr) GetNamingContext(ctx context.Context) (*Naming, error) {
var output *Naming

if _, err := s.GetInto(ctx, bpNaming, nil, &output); err != nil {
return nil, fmt.Errorf("api.Get(naming): %w", err)
}

return output, nil
}

// UpdateNaming updates the naming.
func (s *Sonarr) UpdateNaming(naming *Naming) (*Naming, error) {
return s.UpdateNamingContext(context.Background(), naming)
}

func (s *Sonarr) UpdateNamingContext(ctx context.Context, naming *Naming) (*Naming, error) {
var output Naming

var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(naming); err != nil {
return nil, fmt.Errorf("json.Marshal(naming): %w", err)
}

if _, err := s.PutInto(ctx, bpNaming, nil, &body, &output); err != nil {
return nil, fmt.Errorf("api.Put(naming): %w", err)
}

return &output, nil
}
Loading