Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Jul 7, 2024
2 parents eecfdce + 84a92f6 commit 5e7b679
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 119 deletions.
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
}
28 changes: 0 additions & 28 deletions pointers.go

This file was deleted.

5 changes: 0 additions & 5 deletions radarr/movieeditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
94 changes: 8 additions & 86 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 @@ -303,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

0 comments on commit 5e7b679

Please sign in to comment.