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:
@@ -11,16 +11,26 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Client talks to a SABnzbd instance.
|
||||
type Client struct {
|
||||
mu sync.RWMutex // guards baseURL and apiKey, which Reconfigure can change
|
||||
baseURL string
|
||||
apiKey string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
// Reconfigure points the client at another instance or key.
|
||||
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.
|
||||
func NewClient(baseURL, apiKey string) *Client {
|
||||
return &Client{
|
||||
@@ -159,9 +169,12 @@ func boolParam(b bool) string {
|
||||
// {"status": false, "error": "..."}, which is turned into an error here.
|
||||
func (c *Client) call(ctx context.Context, params url.Values, out any) error {
|
||||
params.Set("output", "json")
|
||||
params.Set("apikey", c.apiKey)
|
||||
c.mu.RLock()
|
||||
baseURL, apiKey := c.baseURL, c.apiKey
|
||||
c.mu.RUnlock()
|
||||
params.Set("apikey", apiKey)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/api?"+params.Encode(), nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api?"+params.Encode(), nil)
|
||||
if err != nil {
|
||||
return stripURL(err)
|
||||
}
|
||||
|
||||
@@ -122,3 +122,22 @@ func TestTransportErrorHidesAPIKey(t *testing.T) {
|
||||
t.Errorf("error leaks API key: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconfigure(t *testing.T) {
|
||||
var gotKey string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotKey = r.URL.Query().Get("apikey")
|
||||
w.Write([]byte(`{"version":"5.1"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient("http://127.0.0.1:1", "old") // unreachable until reconfigured
|
||||
if _, err := c.Version(context.Background()); err == nil {
|
||||
t.Fatal("expected a connection error")
|
||||
}
|
||||
c.Reconfigure(srv.URL+"/", "new")
|
||||
v, err := c.Version(context.Background())
|
||||
if err != nil || v != "5.1" || gotKey != "new" {
|
||||
t.Errorf("version=%q key=%q err=%v", v, gotKey, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user