From 84a92f64ddd8de6230368fc8cd6e2b89b643400f Mon Sep 17 00:00:00 2001 From: David Newhall Date: Sat, 6 Jul 2024 19:57:29 -0700 Subject: [PATCH] split shared, remove Ptr() methods (#167) * split shared, remove Ptr() methods * add Ptr method --- helpers.go | 113 +++++++++++++++++++++++++++++++++++++ pointers.go | 23 -------- radarr/movieeditor.go | 25 ++++---- radarr/movieeditor_test.go | 4 +- shared.go | 99 +++----------------------------- 5 files changed, 133 insertions(+), 131 deletions(-) create mode 100644 helpers.go delete mode 100644 pointers.go diff --git a/helpers.go b/helpers.go new file mode 100644 index 00000000..3e5a7a1d --- /dev/null +++ b/helpers.go @@ -0,0 +1,113 @@ +package starr + +import ( + "crypto/tls" + "fmt" + "net/http" + "strconv" + "strings" + "time" + + "golift.io/starr/debuglog" +) + +// App can be used to satisfy a context value key. +// It is not used in this library; provided for convenience. +type App string + +// These constants are just here for convenience. +const ( + Emby App = "Emby" + Lidarr App = "Lidarr" + Plex App = "Plex" + Prowlarr App = "Prowlarr" + Radarr App = "Radarr" + Readarr App = "Readarr" + Sonarr App = "Sonarr" + Whisparr App = "Whisparr" +) + +// String turns an App name into a string. +func (a App) String() string { + return string(a) +} + +// Lower turns an App name into a lowercase string. +func (a App) Lower() string { + return strings.ToLower(string(a)) +} + +// Client returns the default client, and is used if one is not passed in. +func Client(timeout time.Duration, verifySSL bool) *http.Client { + return &http.Client{ + Timeout: timeout, + CheckRedirect: func(_ *http.Request, _ []*http.Request) error { + return http.ErrUseLastResponse + }, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifySSL}, //nolint:gosec + }, + } +} + +// ClientWithDebug returns an http client with a debug logger enabled. +func ClientWithDebug(timeout time.Duration, verifySSL bool, logConfig debuglog.Config) *http.Client { + client := Client(timeout, verifySSL) + client.Transport = debuglog.NewLoggingRoundTripper(logConfig, client.Transport) + + return client +} + +// Itoa converts an int64 to a string. +// Deprecated: Use starr.Str() instead. +func Itoa(v int64) string { + return Str(v) +} + +// Str converts numbers and booleans to a string. +func Str[I int | int64 | float64 | bool](val I) string { + const ( + base10 = 10 + bits64 = 64 + ) + + switch val := any(val).(type) { + case int: + return strconv.Itoa(val) + case bool: + return strconv.FormatBool(val) + case int64: + return strconv.FormatInt(val, base10) + case float64: + return strconv.FormatFloat(val, 'f', -1, bits64) + default: + return fmt.Sprint(val) + } +} + +// Ptr returns a pointer to the provided "whatever". +func Ptr[P any](p P) *P { + return &p +} + +// True returns a pointer to a true boolean. +func True() *bool { + return Ptr(true) +} + +// False returns a pointer to a false boolean. +func False() *bool { + return Ptr(false) +} + +// String returns a pointer to a string. +// Deprecated: Use Ptr() function instead. +func String(s string) *string { + return &s +} + +// Int64 returns a pointer to the provided integer. +// Deprecated: Use Ptr() function instead. +func Int64(s int64) *int64 { + return &s +} diff --git a/pointers.go b/pointers.go deleted file mode 100644 index 383d48f4..00000000 --- a/pointers.go +++ /dev/null @@ -1,23 +0,0 @@ -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 -} diff --git a/radarr/movieeditor.go b/radarr/movieeditor.go index f3a3d442..92faab7e 100644 --- a/radarr/movieeditor.go +++ b/radarr/movieeditor.go @@ -15,16 +15,16 @@ const bpMovieEditor = bpMovie + "/editor" // 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 + 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. @@ -40,11 +40,6 @@ const ( AvailabilityDeleted Availability = "deleted" ) -// 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) diff --git a/radarr/movieeditor_test.go b/radarr/movieeditor_test.go index 4424c619..8aed5e04 100644 --- a/radarr/movieeditor_test.go +++ b/radarr/movieeditor_test.go @@ -41,8 +41,8 @@ func TestEditMovies(t *testing.T) { WithRequest: &radarr.BulkEdit{ MovieIDs: []int64{17, 13}, Tags: []int{44, 55, 66}, - ApplyTags: starr.TagsAdd.Ptr(), - MinimumAvailability: radarr.AvailabilityToBeAnnounced.Ptr(), + ApplyTags: starr.TagsAdd, + MinimumAvailability: radarr.AvailabilityToBeAnnounced, }, ExpectedRequest: `{"movieIds":[17,13],"minimumAvailability":"tba","tags":[44,55,66],"applyTags":"add"}` + "\n", ExpectedMethod: http.MethodPut, diff --git a/shared.go b/shared.go index c6b7ea2c..e0ed97fb 100644 --- a/shared.go +++ b/shared.go @@ -1,66 +1,15 @@ package starr import ( - "crypto/tls" "encoding/json" - "net/http" "net/url" "strconv" "strings" "time" - - "golift.io/starr/debuglog" ) /* This file contains shared structs or constants for all the *arr apps. */ -// App can be used to satisfy a context value key. -// It is not used in this library; provided for convenience. -type App string - -// These constants are just here for convenience. -const ( - Emby App = "Emby" - Lidarr App = "Lidarr" - Plex App = "Plex" - Prowlarr App = "Prowlarr" - Radarr App = "Radarr" - Readarr App = "Readarr" - Sonarr App = "Sonarr" - Whisparr App = "Whisparr" -) - -// String turns an App name into a string. -func (a App) String() string { - return string(a) -} - -// Lower turns an App name into a lowercase string. -func (a App) Lower() string { - return strings.ToLower(string(a)) -} - -// Client returns the default client, and is used if one is not passed in. -func Client(timeout time.Duration, verifySSL bool) *http.Client { - return &http.Client{ - Timeout: timeout, - CheckRedirect: func(_ *http.Request, _ []*http.Request) error { - return http.ErrUseLastResponse - }, - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifySSL}, //nolint:gosec - }, - } -} - -// ClientWithDebug returns an http client with a debug logger enabled. -func ClientWithDebug(timeout time.Duration, verifySSL bool, logConfig debuglog.Config) *http.Client { - client := Client(timeout, verifySSL) - client.Transport = debuglog.NewLoggingRoundTripper(logConfig, client.Transport) - - return client -} - // CalendarTimeFilterFormat is the Go time format the calendar expects the filter to be in. const CalendarTimeFilterFormat = "2006-01-02T03:04:05.000Z" @@ -235,6 +184,13 @@ func (o *QueueDeleteOpts) Values() url.Values { return params } +// FormatItem is part of a quality profile. +type FormatItem struct { + Format int64 `json:"format"` + Name string `json:"name"` + Score int64 `json:"score"` +} + // PlayTime is used in at least Sonarr, maybe other places. // Holds a string duration converted from hh:mm:ss. type PlayTime struct { @@ -242,12 +198,7 @@ type PlayTime struct { time.Duration } -// FormatItem is part of a quality profile. -type FormatItem struct { - Format int64 `json:"format"` - Name string `json:"name"` - Score int64 `json:"score"` -} +var _ json.Unmarshaler = (*PlayTime)(nil) // UnmarshalJSON parses a run time duration in format hh:mm:ss. func (d *PlayTime) UnmarshalJSON(b []byte) error { @@ -275,8 +226,6 @@ func (d *PlayTime) MarshalJSON() ([]byte, error) { return []byte(`"` + d.Original + `"`), nil } -var _ json.Unmarshaler = (*PlayTime)(nil) - // ApplyTags is an enum used as an input for Bulk editors, and perhaps other places. type ApplyTags string @@ -288,11 +237,6 @@ const ( TagsReplace ApplyTags = "replace" ) -// Ptr returns a pointer to an apply tags value. Useful for a BulkEdit struct. -func (a ApplyTags) Ptr() *ApplyTags { - return &a -} - // TimeSpan is part of AudioTags and possibly used other places. type TimeSpan struct { Ticks int64 `json:"ticks"` @@ -308,33 +252,6 @@ type TimeSpan struct { TotalSeconds int64 `json:"totalSeconds"` } -// Itoa converts an int64 to a string. -// Deprecated: Use starr.Str() instead. -func Itoa(v int64) string { - return Str(v) -} - -// Str converts numbers and booleans to a string. -func Str[I int | int64 | float64 | bool](val I) string { - const ( - base10 = 10 - bits64 = 64 - ) - - switch val := interface{}(val).(type) { - case int: - return strconv.Itoa(val) - case bool: - return strconv.FormatBool(val) - case int64: - return strconv.FormatInt(val, base10) - case float64: - return strconv.FormatFloat(val, 'f', -1, bits64) - default: - return "" - } -} - // Protocol used to download media. Comes with enum constants. type Protocol string