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

split shared, remove Ptr() methods #167

Merged
merged 2 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
113 changes: 113 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 0 additions & 23 deletions pointers.go

This file was deleted.

25 changes: 10 additions & 15 deletions radarr/movieeditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions radarr/movieeditor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
99 changes: 8 additions & 91 deletions shared.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -235,19 +184,21 @@ 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 {
Original string
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 {
Expand Down Expand Up @@ -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

Expand All @@ -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"`
Expand All @@ -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

Expand Down
Loading