Add the Config view: edit and save the settings, applied without a restart
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+21
-2
@@ -11,16 +11,34 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Client is a minimal JSON client for the Radarr/Sonarr v3 API.
|
||||
type Client struct {
|
||||
mu sync.RWMutex // guards baseURL and apiKey, which Reconfigure can change
|
||||
baseURL string
|
||||
apiKey string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
// settings returns the current base URL and API key.
|
||||
func (c *Client) settings() (baseURL, apiKey string) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.baseURL, c.apiKey
|
||||
}
|
||||
|
||||
// Reconfigure points the client at another instance or key. Requests already
|
||||
// in flight are not affected.
|
||||
func (c *Client) Reconfigure(baseURL, apiKey string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.baseURL = strings.TrimRight(baseURL, "/")
|
||||
c.apiKey = apiKey
|
||||
}
|
||||
|
||||
// NewClient returns a client for the instance at baseURL (without /api/v3).
|
||||
func NewClient(baseURL, apiKey string) *Client {
|
||||
return &Client{
|
||||
@@ -46,7 +64,8 @@ func (e *APIError) Error() string {
|
||||
// Do sends a request to /api/v3/<path>. body (if non-nil) is JSON-encoded and
|
||||
// the response is decoded into out (if non-nil).
|
||||
func (c *Client) Do(ctx context.Context, method, path string, query url.Values, body, out any) error {
|
||||
u := c.baseURL + "/api/v3/" + strings.TrimLeft(path, "/")
|
||||
base, apiKey := c.settings()
|
||||
u := base + "/api/v3/" + strings.TrimLeft(path, "/")
|
||||
if len(query) > 0 {
|
||||
u += "?" + query.Encode()
|
||||
}
|
||||
@@ -64,7 +83,7 @@ func (c *Client) Do(ctx context.Context, method, path string, query url.Values,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("X-Api-Key", c.apiKey)
|
||||
req.Header.Set("X-Api-Key", apiKey)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
Reference in New Issue
Block a user