-
Notifications
You must be signed in to change notification settings - Fork 19
/
helpers.go
113 lines (96 loc) · 2.47 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
}