Initial release
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
package radarr
|
||||
|
||||
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() ([]Movie, error) {
|
||||
data, err := c.do("GET", "/api/v3/movie", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var movies []Movie
|
||||
if err := json.Unmarshal(data, &movies); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
return movies, nil
|
||||
}
|
||||
|
||||
func (c *Client) Lookup(term string) ([]Movie, error) {
|
||||
data, err := c.do("GET", "/api/v3/movie/lookup?term="+url.QueryEscape(term), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var movies []Movie
|
||||
if err := json.Unmarshal(data, &movies); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
return movies, nil
|
||||
}
|
||||
|
||||
func (c *Client) LookupIMDB(imdbID string) (*Movie, error) {
|
||||
data, err := c.do("GET", "/api/v3/movie/lookup/imdb?imdbId="+url.QueryEscape(imdbID), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var movie Movie
|
||||
if err := json.Unmarshal(data, &movie); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
return &movie, nil
|
||||
}
|
||||
|
||||
func (c *Client) Get(id int) (*Movie, error) {
|
||||
data, err := c.do("GET", fmt.Sprintf("/api/v3/movie/%d", id), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var m Movie
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
return &m, 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(m Movie) (*Movie, error) {
|
||||
data, err := c.do("POST", "/api/v3/movie", m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result Movie
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package radarr
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testClient(t *testing.T) *Client {
|
||||
t.Helper()
|
||||
url := os.Getenv("RADARR_URL")
|
||||
key := os.Getenv("RADARR_API_KEY")
|
||||
if url == "" || key == "" {
|
||||
t.Skip("RADARR_URL and RADARR_API_KEY not set")
|
||||
}
|
||||
return NewClient(url, key, true)
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
c := testClient(t)
|
||||
movies, err := c.List()
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if len(movies) == 0 {
|
||||
t.Fatal("List returned no movies")
|
||||
}
|
||||
m := movies[0]
|
||||
if m.Title == "" {
|
||||
t.Error("first movie has no title")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookup(t *testing.T) {
|
||||
c := testClient(t)
|
||||
results, err := c.Lookup("Dune")
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package radarr
|
||||
|
||||
type Movie 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"`
|
||||
Year int `json:"year,omitempty"`
|
||||
Studio string `json:"studio,omitempty"`
|
||||
Runtime int `json:"runtime,omitempty"`
|
||||
ImdbID string `json:"imdbId,omitempty"`
|
||||
TmdbID int `json:"tmdbId,omitempty"`
|
||||
TitleSlug string `json:"titleSlug,omitempty"`
|
||||
Images []Image `json:"images,omitempty"`
|
||||
HasFile bool `json:"hasFile,omitempty"`
|
||||
QualityProfileID int `json:"qualityProfileId,omitempty"`
|
||||
RootFolderPath string `json:"rootFolderPath,omitempty"`
|
||||
Monitored bool `json:"monitored"`
|
||||
MinimumAvailability string `json:"minimumAvailability,omitempty"`
|
||||
AddOptions *AddOptions `json:"addOptions,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
MovieFile *MovieFile `json:"movieFile,omitempty"`
|
||||
Added string `json:"added,omitempty"`
|
||||
}
|
||||
|
||||
type Image struct {
|
||||
CoverType string `json:"coverType"`
|
||||
URL string `json:"url"`
|
||||
RemoteURL string `json:"remoteUrl"`
|
||||
}
|
||||
|
||||
type AddOptions struct {
|
||||
SearchForMovie bool `json:"searchForMovie"`
|
||||
}
|
||||
|
||||
type MovieFile struct {
|
||||
RelativePath string `json:"relativePath"`
|
||||
Size int64 `json:"size"`
|
||||
Quality *struct {
|
||||
Quality struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"quality"`
|
||||
} `json:"quality,omitempty"`
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
Reference in New Issue
Block a user