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

Add movie editor endpoints #69

Merged
merged 6 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 23 additions & 0 deletions pointers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package starr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These may be useful down the road too.


// String returns a pointer to a string.
func String(s string) *string {
return &s
}

// True returns a pointer to a true boolean.
func True() *bool {
s := true
return &s
}

// False returns a pointer to a false boolean.
func False() *bool {
s := false
return &s
}

// Int64 returns a pointer to the provided integer.
func Int64(s int64) *int64 {
return &s
}
18 changes: 18 additions & 0 deletions radarr/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,21 @@ func (r *Radarr) LookupContext(ctx context.Context, term string) ([]*Movie, erro

return output, nil
}

// DeleteMovie removes a movie from the database. Setting deleteFiles true will delete all content for the movie.
func (r *Radarr) DeleteMovie(movieID int64, deleteFiles, addImportExclusion bool) error {
return r.DeleteMovieContext(context.Background(), movieID, deleteFiles, addImportExclusion)
}

// DeleteMovieContext removes a movie from the database. Setting deleteFiles true will delete all content for the movie.
func (r *Radarr) DeleteMovieContext(ctx context.Context, movieID int64, deleteFiles, addImportExclusion bool) error {
req := starr.Request{URI: path.Join(bpMovie, fmt.Sprint(movieID)), Query: make(url.Values)}
req.Query.Set("deleteFiles", fmt.Sprint(deleteFiles))
req.Query.Set("addImportExclusion", fmt.Sprint(addImportExclusion))

if err := r.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}

return nil
}
69 changes: 69 additions & 0 deletions radarr/movieeditor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package radarr

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

"golift.io/starr"
)

const bpMovieEditor = bpMovie + "/editor"

// BulkEdit is the input for the bulk movie editor endpoint.
// You may use starr.True(), starr.False(), starr.Int64(), and starr.String() to add data to the struct members.
type BulkEdit struct {
MovieIDs []int64 `json:"movieIds"`
Monitored *bool `json:"monitored,omitempty"`
QualityProfileID *int64 `json:"qualityProfileId,omitempty"`
MinimumAvailability *string `json:"minimumAvailability,omitempty"` // tba
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MinimumAvailability could probably be enum constants, but I do not know all the values.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tba, announced, inCinemas, released, deleted

ref https://radarr.video/docs/api/#/MovieEditor/put_api_v3_movie_editor

RootFolderPath *string `json:"rootFolderPath,omitempty"` // path
Tags []int `json:"tags,omitempty"` // [0]
ApplyTags *string `json:"applyTags,omitempty"` // add
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApplyTags could probably be enum constants, but I do not know all the values.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MoveFiles *bool `json:"moveFiles,omitempty"`
DeleteFiles *bool `json:"deleteFiles,omitempty"` // delete only
AddImportExclusion *bool `json:"addImportExclusion,omitempty"` // delete only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are they pointer to let them leverage the omitempty tag and use the default 0 value as well?
when they are not present which is the default behaviour? if it is the same as the 0 value you can use values instead of pointers and remove the omitempty tag.
I haven't tried this, so they're just some thoughts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These members are purposely pointers, because the default value is different than the "missing" value.

This is a bulk-change endpoint. When you pick monitored, for instance, there are three choices. monitored, not monitored and no change. We achieve "no change" by omitting the key entirely. If we set it to true, then all movie IDs provided are monitored. If we set it to false then they are all unmonitored. If we do not want to change the monitored status, but rather, change something else, then we have to omit monitored.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks for the example

}

// EditMovies allows bulk diting many movies at once.
func (r *Radarr) EditMovies(editMovies *BulkEdit) ([]*Movie, error) {
return r.EditMoviesContext(context.Background(), editMovies)
}

// EditMoviesContext allows bulk diting many movies at once.
func (r *Radarr) EditMoviesContext(ctx context.Context, editMovies *BulkEdit) ([]*Movie, error) {
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(editMovies); err != nil {
return nil, fmt.Errorf("json.Marshal(%s): %w", bpMovieEditor, err)
}

var output []*Movie

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

return output, nil
}

// DeleteMovies bulk deletes movies. Can also mark them as excluded, and delete their files.
func (r *Radarr) DeleteMovies(deleteMovies *BulkEdit) error {
return r.DeleteMoviesContext(context.Background(), deleteMovies)
}

// DeleteDeleteMoviesContextMovies bulk deletes movies. Can also mark them as excluded, and delete their files.
func (r *Radarr) DeleteMoviesContext(ctx context.Context, deleteMovies *BulkEdit) error {
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(deleteMovies); err != nil {
return fmt.Errorf("json.Marshal(%s): %w", bpMovieEditor, err)
}

req := starr.Request{URI: bpMovieEditor, Body: &body}
if err := r.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}

return nil
}
45 changes: 45 additions & 0 deletions radarr/movieeditor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package radarr_test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidnewhall this really rock! thanks 🚀


import (
"net/http"
"path"
"testing"

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

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

tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "movie", "editor"),
ResponseStatus: http.StatusOK,
ResponseBody: `[{"id": 7, "monitored": true},{"id": 3, "monitored": true}]`,
WithError: nil,
WithRequest: &radarr.BulkEdit{
MovieIDs: []int64{7, 3},
Monitored: starr.True(),
DeleteFiles: starr.False(),
},
ExpectedRequest: `{"movieIds":[7,3],"monitored":true,"deleteFiles":false}` + "\n",
ExpectedMethod: http.MethodPut,
WithResponse: []*radarr.Movie{{ID: 7, Monitored: true}, {ID: 3, Monitored: true}},
},
}

for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := test.GetMockServer(t)
client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
output, err := client.EditMovies(test.WithRequest.(*radarr.BulkEdit))
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")
})
}
}
2 changes: 1 addition & 1 deletion test_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (test *TestMockData) GetMockServer(t *testing.T) *httptest.Server {
assert.EqualValues(t, test.ExpectedPath, req.URL.String())
writer.WriteHeader(test.ResponseStatus)

assert.EqualValues(t, req.Method, test.ExpectedMethod)
assert.EqualValues(t, test.ExpectedMethod, req.Method)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was backward.


body, err := io.ReadAll(req.Body)
assert.NoError(t, err)
Expand Down