Initial release

This commit is contained in:
2026-03-12 22:13:57 +00:00
commit 6c067287d7
21 changed files with 2458 additions and 0 deletions
+141
View File
@@ -0,0 +1,141 @@
package sonarr
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
type Client struct {
baseURL string
apiKey string
httpClient *http.Client
}
func NewClient(baseURL, apiKey string, tlsSkipVerify bool) *Client {
client := &http.Client{}
if strings.HasPrefix(baseURL, "https") && tlsSkipVerify {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
return &Client{
baseURL: strings.TrimRight(baseURL, "/"),
apiKey: apiKey,
httpClient: client,
}
}
func (c *Client) do(method, path string, body interface{}) ([]byte, error) {
var reqBody io.Reader
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal request: %w", err)
}
reqBody = bytes.NewReader(data)
}
req, err := http.NewRequest(method, c.baseURL+path, reqBody)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("X-Api-Key", c.apiKey)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response: %w", err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("API error %d: %s", resp.StatusCode, string(respBody))
}
return respBody, nil
}
func (c *Client) List() ([]Series, error) {
data, err := c.do("GET", "/api/v3/series", nil)
if err != nil {
return nil, err
}
var series []Series
if err := json.Unmarshal(data, &series); err != nil {
return nil, fmt.Errorf("parse response: %w", err)
}
return series, nil
}
func (c *Client) Lookup(term string) ([]Series, error) {
data, err := c.do("GET", "/api/v3/series/lookup?term="+url.QueryEscape(term), nil)
if err != nil {
return nil, err
}
var series []Series
if err := json.Unmarshal(data, &series); err != nil {
return nil, fmt.Errorf("parse response: %w", err)
}
return series, nil
}
func (c *Client) Get(id int) (*Series, error) {
data, err := c.do("GET", fmt.Sprintf("/api/v3/series/%d", id), nil)
if err != nil {
return nil, err
}
var s Series
if err := json.Unmarshal(data, &s); err != nil {
return nil, fmt.Errorf("parse response: %w", err)
}
return &s, nil
}
func (c *Client) QualityProfiles() ([]QualityProfile, error) {
data, err := c.do("GET", "/api/v3/qualityprofile", nil)
if err != nil {
return nil, err
}
var profiles []QualityProfile
if err := json.Unmarshal(data, &profiles); err != nil {
return nil, fmt.Errorf("parse response: %w", err)
}
return profiles, nil
}
func (c *Client) RootFolders() ([]RootFolder, error) {
data, err := c.do("GET", "/api/v3/rootfolder", nil)
if err != nil {
return nil, err
}
var folders []RootFolder
if err := json.Unmarshal(data, &folders); err != nil {
return nil, fmt.Errorf("parse response: %w", err)
}
return folders, nil
}
func (c *Client) Add(s Series) (*Series, error) {
data, err := c.do("POST", "/api/v3/series", s)
if err != nil {
return nil, err
}
var result Series
if err := json.Unmarshal(data, &result); err != nil {
return nil, fmt.Errorf("parse response: %w", err)
}
return &result, nil
}
+76
View File
@@ -0,0 +1,76 @@
package sonarr
import (
"os"
"testing"
)
func testClient(t *testing.T) *Client {
t.Helper()
url := os.Getenv("SONARR_URL")
key := os.Getenv("SONARR_API_KEY")
if url == "" || key == "" {
t.Skip("SONARR_URL and SONARR_API_KEY not set")
}
return NewClient(url, key, true)
}
func TestList(t *testing.T) {
c := testClient(t)
series, err := c.List()
if err != nil {
t.Fatalf("List: %v", err)
}
if len(series) == 0 {
t.Fatal("List returned no series")
}
s := series[0]
if s.Title == "" {
t.Error("first series has no title")
}
if s.TvdbID == 0 {
t.Error("first series has no TVDB ID")
}
}
func TestLookup(t *testing.T) {
c := testClient(t)
results, err := c.Lookup("Breaking Bad")
if err != nil {
t.Fatalf("Lookup: %v", err)
}
if len(results) == 0 {
t.Fatal("Lookup returned no results")
}
if results[0].Title == "" {
t.Error("first result has no title")
}
}
func TestQualityProfiles(t *testing.T) {
c := testClient(t)
profiles, err := c.QualityProfiles()
if err != nil {
t.Fatalf("QualityProfiles: %v", err)
}
if len(profiles) == 0 {
t.Fatal("no quality profiles returned")
}
if profiles[0].Name == "" {
t.Error("first profile has no name")
}
}
func TestRootFolders(t *testing.T) {
c := testClient(t)
folders, err := c.RootFolders()
if err != nil {
t.Fatalf("RootFolders: %v", err)
}
if len(folders) == 0 {
t.Fatal("no root folders returned")
}
if folders[0].Path == "" {
t.Error("first folder has no path")
}
}
+63
View File
@@ -0,0 +1,63 @@
package sonarr
type Series struct {
ID int `json:"id,omitempty"`
Title string `json:"title"`
SortTitle string `json:"sortTitle,omitempty"`
Status string `json:"status,omitempty"`
Overview string `json:"overview,omitempty"`
Network string `json:"network,omitempty"`
Year int `json:"year,omitempty"`
SeasonCount int `json:"seasonCount,omitempty"`
TvdbID int `json:"tvdbId,omitempty"`
ImdbID string `json:"imdbId,omitempty"`
TitleSlug string `json:"titleSlug,omitempty"`
Seasons []Season `json:"seasons,omitempty"`
Images []Image `json:"images,omitempty"`
Statistics *Statistics `json:"statistics,omitempty"`
QualityProfileID int `json:"qualityProfileId,omitempty"`
RootFolderPath string `json:"rootFolderPath,omitempty"`
Monitored bool `json:"monitored"`
AddOptions *AddOptions `json:"addOptions,omitempty"`
NextAiring string `json:"nextAiring,omitempty"`
PreviousAiring string `json:"previousAiring,omitempty"`
SeriesType string `json:"seriesType,omitempty"`
LanguageProfileID int `json:"languageProfileId,omitempty"`
Path string `json:"path,omitempty"`
Added string `json:"added,omitempty"`
}
type Season struct {
SeasonNumber int `json:"seasonNumber"`
Monitored bool `json:"monitored"`
Statistics *Statistics `json:"statistics,omitempty"`
}
type Statistics struct {
EpisodeFileCount int `json:"episodeFileCount"`
EpisodeCount int `json:"episodeCount"`
TotalEpisodeCount int `json:"totalEpisodeCount"`
SizeOnDisk int64 `json:"sizeOnDisk"`
PercentOfEpisodes float64 `json:"percentOfEpisodes"`
}
type Image struct {
CoverType string `json:"coverType"`
URL string `json:"url"`
RemoteURL string `json:"remoteUrl"`
}
type AddOptions struct {
SearchForMissingEpisodes bool `json:"searchForMissingEpisodes"`
}
type QualityProfile struct {
ID int `json:"id"`
Name string `json:"name"`
}
type RootFolder struct {
ID int `json:"id"`
Path string `json:"path"`
FreeSpace int64 `json:"freeSpace"`
}