-
Notifications
You must be signed in to change notification settings - Fork 19
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
Changes from 5 commits
0ab5fd5
9735b7c
dedd989
574338e
e14d1f8
cc0cd9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,5 +30,10 @@ linters: | |
- dupl | ||
- nlreturn | ||
|
||
#linters-settings: | ||
# govet: | ||
# enable: | ||
# - fieldalignment | ||
|
||
run: | ||
timeout: 35m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package starr | ||
|
||
// 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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. | ||
// Use Availability.Ptr() to add a value to minimum availability, and starr.ApplyTags.Ptr() for apply tags. | ||
type BulkEdit struct { | ||
MovieIDs []int64 `json:"movieIds"` | ||
Monitored *bool `json:"monitored,omitempty"` | ||
QualityProfileID *int64 `json:"qualityProfileId,omitempty"` | ||
MinimumAvailability *Availability `json:"minimumAvailability,omitempty"` // tba | ||
RootFolderPath *string `json:"rootFolderPath,omitempty"` // path | ||
Tags []int `json:"tags,omitempty"` // [0] | ||
ApplyTags *starr.ApplyTags `json:"applyTags,omitempty"` // add | ||
MoveFiles *bool `json:"moveFiles,omitempty"` | ||
DeleteFiles *bool `json:"deleteFiles,omitempty"` // delete only | ||
AddImportExclusion *bool `json:"addImportExclusion,omitempty"` // delete only | ||
} | ||
|
||
// Availability is an enum used as MinimumAvailability in a few places throughout Radarr. | ||
type Availability string | ||
|
||
// Availability / MinimumAvailability constants. | ||
// https://radarr.video/docs/api/#/MovieEditor/put_api_v3_movie_editor | ||
const ( | ||
AvailabilityToBeAnnounced Availability = "tba" | ||
AvailabilityAnnounced Availability = "announced" | ||
AvailabilityInCinemas Availability = "inCinemas" | ||
AvailabilityReleased Availability = "released" | ||
AvailabilityDeleted Availability = "deleted" | ||
) | ||
Comment on lines
+30
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is nice for a user perspective since it provides users with valid values. However, users can still use any string they want... so I'd say it doesn't make much difference for me, if you like this approach you can continue to use it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's all about users. This, if nothing else, gives them an easy way to see what values are appropriate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a difference in status and availability. tba and deleted will not work as a minimum availability in radarr. "announced", "released" & "inCinemas" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Practical use would be the 3 nit mentioned
neither really has any practical use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All good. I just made them constants. Users don't have to use them. :) |
||
|
||
// Ptr returns a pointer to a minimum availability. Useful for a BulkEdit struct. | ||
func (a Availability) Ptr() *Availability { | ||
return &a | ||
} | ||
|
||
// 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package radarr_test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}}, | ||
}, | ||
{ | ||
Name: "200", | ||
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "movie", "editor"), | ||
ResponseStatus: http.StatusOK, | ||
ResponseBody: `[{"id":17,"minimumAvailability":"tba","tags":[44,55,66]},` + | ||
`{"id":13,"minimumAvailability":"tba","tags":[44,55,66]}]`, | ||
WithError: nil, | ||
WithRequest: &radarr.BulkEdit{ | ||
MovieIDs: []int64{17, 13}, | ||
Tags: []int{44, 55, 66}, | ||
ApplyTags: starr.TagsAdd.Ptr(), | ||
MinimumAvailability: radarr.AvailabilityToBeAnnounced.Ptr(), | ||
}, | ||
ExpectedRequest: `{"movieIds":[17,13],"minimumAvailability":"tba","tags":[44,55,66],"applyTags":"add"}` + "\n", | ||
ExpectedMethod: http.MethodPut, | ||
WithResponse: []*radarr.Movie{ | ||
{ID: 17, MinimumAvailability: radarr.AvailabilityToBeAnnounced, Tags: []int{44, 55, 66}}, | ||
{ID: 13, MinimumAvailability: radarr.AvailabilityToBeAnnounced, Tags: []int{44, 55, 66}}, | ||
}, | ||
}, | ||
} | ||
|
||
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, "the wrong error was returned") | ||
assert.EqualValues(t, test.WithResponse, output, "make sure ResponseBody and WithResponse are a match") | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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.