Skip to content

Commit

Permalink
feat(sonarr): download client (#63)
Browse files Browse the repository at this point in the history
* feat(sonarr): add downloadclientconfig

* feat(sonarr): add downloadclient

* feat(sonarr): add remotepathmapping

* fix: align comments fix typos and path joining

* style: forgot a path join

* fix: int64 type and few more typo

* fix: wrong assertion in test
  • Loading branch information
Fuochi authored Sep 23, 2022
1 parent e035cd7 commit 756b5db
Show file tree
Hide file tree
Showing 21 changed files with 1,283 additions and 80 deletions.
12 changes: 6 additions & 6 deletions lidarr/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestGetTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"),
ExpectedMethod: "GET",
ResponseStatus: 200,
WithRequest: 1,
Expand All @@ -74,7 +74,7 @@ func TestGetTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"),
ExpectedMethod: "GET",
ResponseStatus: 404,
WithRequest: 1,
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestUpdateTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"),
ExpectedMethod: "PUT",
ResponseStatus: 200,
WithRequest: &starr.Tag{
Expand All @@ -168,7 +168,7 @@ func TestUpdateTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"),
ExpectedMethod: "PUT",
ResponseStatus: 404,
WithRequest: &starr.Tag{
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestDeleteTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 200,
Expand All @@ -210,7 +210,7 @@ func TestDeleteTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 404,
Expand Down
12 changes: 6 additions & 6 deletions prowlarr/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestGetTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag", "1"),
ExpectedMethod: "GET",
ResponseStatus: 200,
WithRequest: 1,
Expand All @@ -74,7 +74,7 @@ func TestGetTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag", "1"),
ExpectedMethod: "GET",
ResponseStatus: 404,
WithRequest: 1,
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestUpdateTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag", "1"),
ExpectedMethod: "PUT",
ResponseStatus: 200,
WithRequest: &starr.Tag{
Expand All @@ -168,7 +168,7 @@ func TestUpdateTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag", "1"),
ExpectedMethod: "PUT",
ResponseStatus: 404,
WithRequest: &starr.Tag{
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestDeleteTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag", "1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 200,
Expand All @@ -210,7 +210,7 @@ func TestDeleteTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, prowlarr.APIver, "tag", "1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 404,
Expand Down
12 changes: 6 additions & 6 deletions radarr/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestGetTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag", "1"),
ExpectedMethod: "GET",
ResponseStatus: 200,
WithRequest: 1,
Expand All @@ -74,7 +74,7 @@ func TestGetTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag", "1"),
ExpectedMethod: "GET",
ResponseStatus: 404,
WithRequest: 1,
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestUpdateTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag", "1"),
ExpectedMethod: "PUT",
ResponseStatus: 200,
WithRequest: &starr.Tag{
Expand All @@ -168,7 +168,7 @@ func TestUpdateTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag", "1"),
ExpectedMethod: "PUT",
ResponseStatus: 404,
WithRequest: &starr.Tag{
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestDeleteTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag", "1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 200,
Expand All @@ -210,7 +210,7 @@ func TestDeleteTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "tag", "1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 404,
Expand Down
12 changes: 6 additions & 6 deletions readarr/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestGetTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag", "1"),
ExpectedMethod: "GET",
ResponseStatus: 200,
WithRequest: 1,
Expand All @@ -74,7 +74,7 @@ func TestGetTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag", "1"),
ExpectedMethod: "GET",
ResponseStatus: 404,
WithRequest: 1,
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestUpdateTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag", "1"),
ExpectedMethod: "PUT",
ResponseStatus: 200,
WithRequest: &starr.Tag{
Expand All @@ -168,7 +168,7 @@ func TestUpdateTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag", "1"),
ExpectedMethod: "PUT",
ResponseStatus: 404,
WithRequest: &starr.Tag{
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestDeleteTag(t *testing.T) {
tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag", "1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 200,
Expand All @@ -210,7 +210,7 @@ func TestDeleteTag(t *testing.T) {
},
{
Name: "404",
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag/1"),
ExpectedPath: path.Join("/", starr.API, readarr.APIver, "tag", "1"),
ExpectedMethod: "DELETE",
WithRequest: 1,
ResponseStatus: 404,
Expand Down
8 changes: 4 additions & 4 deletions sonarr/delayprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func (s *Sonarr) GetDelayProfilesContext(ctx context.Context) ([]*DelayProfile,
}

// GetDelayProfile returns a single delay profile.
func (s *Sonarr) GetDelayProfile(profileID int) (*DelayProfile, error) {
func (s *Sonarr) GetDelayProfile(profileID int64) (*DelayProfile, error) {
return s.GetDelayProfileContext(context.Background(), profileID)
}

// GetDelayProfileContext returns a single delay profile.
func (s *Sonarr) GetDelayProfileContext(ctx context.Context, profileID int) (*DelayProfile, error) {
func (s *Sonarr) GetDelayProfileContext(ctx context.Context, profileID int64) (*DelayProfile, error) {
var output DelayProfile

req := starr.Request{URI: path.Join(bpDelayProfile, fmt.Sprint(profileID))}
Expand Down Expand Up @@ -105,12 +105,12 @@ func (s *Sonarr) UpdateDelayProfileContext(ctx context.Context, profile *DelayPr
}

// DeleteDelayProfile removes a single delay profile.
func (s *Sonarr) DeleteDelayProfile(profileID int) error {
func (s *Sonarr) DeleteDelayProfile(profileID int64) error {
return s.DeleteDelayProfileContext(context.Background(), profileID)
}

// DeleteDelayProfileContext removes a single delay profile.
func (s *Sonarr) DeleteDelayProfileContext(ctx context.Context, profileID int) error {
func (s *Sonarr) DeleteDelayProfileContext(ctx context.Context, profileID int64) error {
req := starr.Request{URI: path.Join(bpDelayProfile, fmt.Sprint(profileID))}
if err := s.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
Expand Down
143 changes: 143 additions & 0 deletions sonarr/downloadclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package sonarr

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

"golift.io/starr"
)

// Define Base Path for download client calls.
const bpDownloadClient = APIver + "/downloadClient"

// DownloadClientInput is the input for a new or updated download client.
type DownloadClientInput struct {
Enable bool `json:"enable"`
RemoveCompletedDownloads bool `json:"removeCompletedDownloads"`
RemoveFailedDownloads bool `json:"removeFailedDownloads"`
Priority int `json:"priority"`
ID int64 `json:"id,omitempty"`
ConfigContract string `json:"configContract"`
Implementation string `json:"implementation"`
Name string `json:"name"`
Protocol string `json:"protocol"`
Tags []int `json:"tags"`
Fields []*starr.FieldInput `json:"fields"`
}

// DownloadClientOutput is the output from the download client methods.
type DownloadClientOutput struct {
Enable bool `json:"enable"`
RemoveCompletedDownloads bool `json:"removeCompletedDownloads"`
RemoveFailedDownloads bool `json:"removeFailedDownloads"`
Priority int `json:"priority"`
ID int64 `json:"id,omitempty"`
ConfigContract string `json:"configContract"`
Implementation string `json:"implementation"`
ImplementationName string `json:"implementationName"`
InfoLink string `json:"infoLink"`
Name string `json:"name"`
Protocol string `json:"protocol"`
Tags []int `json:"tags"`
Fields []*starr.FieldOutput `json:"fields"`
}

// GetDownloadClients returns all configured download clients.
func (s *Sonarr) GetDownloadClients() ([]*DownloadClientOutput, error) {
return s.GetDownloadClientsContext(context.Background())
}

// GetDownloadClientsContext returns all configured download clients.
func (s *Sonarr) GetDownloadClientsContext(ctx context.Context) ([]*DownloadClientOutput, error) {
var output []*DownloadClientOutput

req := starr.Request{URI: bpDownloadClient}
if err := s.GetInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
}

return output, nil
}

// GetDownloadClient returns a single download client.
func (s *Sonarr) GetDownloadClient(downloadclientID int64) (*DownloadClientOutput, error) {
return s.GetDownloadClientContext(context.Background(), downloadclientID)
}

// GetDownloadClientContext returns a single download client.
func (s *Sonarr) GetDownloadClientContext(ctx context.Context, downloadclientID int64) (*DownloadClientOutput, error) {
var output DownloadClientOutput

req := starr.Request{URI: path.Join(bpDownloadClient, fmt.Sprint(downloadclientID))}
if err := s.GetInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
}

return &output, nil
}

// AddDownloadClient creates a download client.
func (s *Sonarr) AddDownloadClient(downloadclient *DownloadClientInput) (*DownloadClientOutput, error) {
return s.AddDownloadClientContext(context.Background(), downloadclient)
}

// AddDownloadClientContext creates a download client.
func (s *Sonarr) AddDownloadClientContext(ctx context.Context,
client *DownloadClientInput,
) (*DownloadClientOutput, error) {
var output DownloadClientOutput

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

req := starr.Request{URI: bpDownloadClient, Body: &body}
if err := s.PostInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
}

return &output, nil
}

// UpdateDownloadClient updates the download client.
func (s *Sonarr) UpdateDownloadClient(downloadclient *DownloadClientInput) (*DownloadClientOutput, error) {
return s.UpdateDownloadClientContext(context.Background(), downloadclient)
}

// UpdateDownloadClientContext updates the download client.
func (s *Sonarr) UpdateDownloadClientContext(ctx context.Context,
client *DownloadClientInput,
) (*DownloadClientOutput, error) {
var output DownloadClientOutput

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

req := starr.Request{URI: path.Join(bpDownloadClient, fmt.Sprint(client.ID)), Body: &body}
if err := s.PutInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
}

return &output, nil
}

// DeleteDownloadClient removes a single download client.
func (s *Sonarr) DeleteDownloadClient(downloadclientID int64) error {
return s.DeleteDownloadClientContext(context.Background(), downloadclientID)
}

// DeleteDownloadClientContext removes a single download client.
func (s *Sonarr) DeleteDownloadClientContext(ctx context.Context, downloadclientID int64) error {
req := starr.Request{URI: path.Join(bpDownloadClient, fmt.Sprint(downloadclientID))}
if err := s.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}

return nil
}
Loading

0 comments on commit 756b5db

Please sign in to comment.