diff --git a/lidarr/queue.go b/lidarr/queue.go index 698f778..2825b0a 100644 --- a/lidarr/queue.go +++ b/lidarr/queue.go @@ -3,6 +3,7 @@ package lidarr import ( "context" "fmt" + "path" "time" "golift.io/starr" @@ -105,3 +106,18 @@ func (l *Lidarr) GetQueuePageContext(ctx context.Context, params *starr.PageReq) return &output, nil } + +// DeleteQueue deletes an item from the Activity Queue. +func (l *Lidarr) DeleteQueue(queueID int64, opts *starr.QueueDeleteOpts) error { + return l.DeleteQueueContext(context.Background(), queueID, opts) +} + +// DeleteQueueContext deletes an item from the Activity Queue. +func (l *Lidarr) DeleteQueueContext(ctx context.Context, queueID int64, opts *starr.QueueDeleteOpts) error { + req := starr.Request{URI: path.Join(bpQueue, fmt.Sprint(queueID)), Query: opts.Values()} + if err := l.DeleteAny(ctx, req); err != nil { + return fmt.Errorf("api.Delete(%s): %w", &req, err) + } + + return nil +} diff --git a/radarr/queue.go b/radarr/queue.go index 0e38bc3..6ebe91e 100644 --- a/radarr/queue.go +++ b/radarr/queue.go @@ -3,6 +3,7 @@ package radarr import ( "context" "fmt" + "path" "time" "golift.io/starr" @@ -106,3 +107,18 @@ func (r *Radarr) GetQueuePageContext(ctx context.Context, params *starr.PageReq) return &output, nil } + +// DeleteQueue deletes an item from the Activity Queue. +func (r *Radarr) DeleteQueue(queueID int64, opts *starr.QueueDeleteOpts) error { + return r.DeleteQueueContext(context.Background(), queueID, opts) +} + +// DeleteQueueContext deletes an item from the Activity Queue. +func (r *Radarr) DeleteQueueContext(ctx context.Context, queueID int64, opts *starr.QueueDeleteOpts) error { + req := starr.Request{URI: path.Join(bpQueue, fmt.Sprint(queueID)), Query: opts.Values()} + if err := r.DeleteAny(ctx, req); err != nil { + return fmt.Errorf("api.Delete(%s): %w", &req, err) + } + + return nil +} diff --git a/readarr/queue.go b/readarr/queue.go index 37c7a5d..3ab5aae 100644 --- a/readarr/queue.go +++ b/readarr/queue.go @@ -3,6 +3,7 @@ package readarr import ( "context" "fmt" + "path" "time" "golift.io/starr" @@ -107,3 +108,18 @@ func (r *Readarr) GetQueuePageContext(ctx context.Context, params *starr.PageReq return &output, nil } + +// DeleteQueue deletes an item from the Activity Queue. +func (r *Readarr) DeleteQueue(queueID int64, opts *starr.QueueDeleteOpts) error { + return r.DeleteQueueContext(context.Background(), queueID, opts) +} + +// DeleteQueueContext deletes an item from the Activity Queue. +func (r *Readarr) DeleteQueueContext(ctx context.Context, queueID int64, opts *starr.QueueDeleteOpts) error { + req := starr.Request{URI: path.Join(bpQueue, fmt.Sprint(queueID)), Query: opts.Values()} + if err := r.DeleteAny(ctx, req); err != nil { + return fmt.Errorf("api.Delete(%s): %w", &req, err) + } + + return nil +} diff --git a/shared.go b/shared.go index 1a83578..a2f51df 100644 --- a/shared.go +++ b/shared.go @@ -3,7 +3,9 @@ package starr import ( "crypto/tls" "encoding/json" + "fmt" "net/http" + "net/url" "strconv" "strings" "time" @@ -200,6 +202,36 @@ type BackupFile struct { Size int64 `json:"size"` } +// QueueDeleteOpts are the extra inputs when deleting an item from the Activity Queue. +// Set these appropriately for your expectations. All inputs are the same in all apps. +type QueueDeleteOpts struct { + // Default True, use starr.False() to change it. + RemoveFromClient *bool + // Default False + BlockList bool + // Default False + SkipRedownload bool +} + +// Values turns delete options into http get query parameters. +func (o *QueueDeleteOpts) Values() url.Values { + params := make(url.Values) + params.Set("removeFromClient", "true") + + if o == nil { + return params + } + + params.Set("blocklist", fmt.Sprint(o.BlockList)) + params.Set("skipRedownload", fmt.Sprint(o.SkipRedownload)) + + if o.RemoveFromClient != nil { + params.Set("removeFromClient", fmt.Sprint(*o.RemoveFromClient)) + } + + return params +} + // PlayTime is used in at least Sonarr, maybe other places. // Holds a string duration converted from hh:mm:ss. type PlayTime struct { //nolint:musttag diff --git a/shared_test.go b/shared_test.go new file mode 100644 index 0000000..2fcc8ab --- /dev/null +++ b/shared_test.go @@ -0,0 +1,30 @@ +package starr_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "golift.io/starr" +) + +func TestQueueDeleteOpts_Values(t *testing.T) { + t.Parallel() + assert := assert.New(t) + + var opts *starr.QueueDeleteOpts + + params := opts.Values() // it's nil. + assert.Equal(params.Encode(), "removeFromClient=true", + "default queue delete parameters encoded incorrectly") + + opts = &starr.QueueDeleteOpts{ + BlockList: true, + RemoveFromClient: starr.False(), + SkipRedownload: true, + } + params = opts.Values() + + assert.Equal("false", params.Get("removeFromClient"), "delete parameters encoded incorrectly") + assert.Equal("true", params.Get("blocklist"), "delete parameters encoded incorrectly") + assert.Equal("true", params.Get("skipRedownload"), "delete parameters encoded incorrectly") +} diff --git a/sonarr/queue.go b/sonarr/queue.go index b2405dc..f8ef9e9 100644 --- a/sonarr/queue.go +++ b/sonarr/queue.go @@ -3,6 +3,7 @@ package sonarr import ( "context" "fmt" + "path" "time" "golift.io/starr" @@ -107,3 +108,18 @@ func (s *Sonarr) GetQueuePageContext(ctx context.Context, params *starr.PageReq) return &output, nil } + +// DeleteQueue deletes an item from the Activity Queue. +func (s *Sonarr) DeleteQueue(queueID int64, opts *starr.QueueDeleteOpts) error { + return s.DeleteQueueContext(context.Background(), queueID, opts) +} + +// DeleteQueueContext deletes an item from the Activity Queue. +func (s *Sonarr) DeleteQueueContext(ctx context.Context, queueID int64, opts *starr.QueueDeleteOpts) error { + req := starr.Request{URI: path.Join(bpQueue, fmt.Sprint(queueID)), Query: opts.Values()} + if err := s.DeleteAny(ctx, req); err != nil { + return fmt.Errorf("api.Delete(%s): %w", &req, err) + } + + return nil +}