golift.io/starr@v1.0.0/starr.go (about)

     1  // Package starr is a library for interacting with the APIs in Radarr, Lidarr, Sonarr
     2  // and Readarr. It consists of the main starr package and one sub package for each
     3  // starr application. In the basic use, you create a starr Config that contains an
     4  // API key and an App URL. Pass this into one of the other packages (like radarr),
     5  // and it's used as an interface to make API calls.
     6  //
     7  // You can either call starr.New() to build an http.Client for you, or create a
     8  // starr.Config that contains one you control. If you pass a starr.Config into
     9  // a sub package without an http Client, it will be created for you. There are
    10  // a lot of option to set this code up from simple and easy to more advanced.
    11  //
    12  // The sub package contain methods and data structures for a number of API endpoints.
    13  // Each app has somewhere between 50 and 100 API endpoints. This library currently
    14  // covers about 10% of those. You can retrieve things like movies, albums, series
    15  // and books. You can retrieve server status, authors, artists and items in queues.
    16  // You can also add new media to each application with this library.
    17  package starr
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"time"
    23  )
    24  
    25  // Defaults for New().
    26  const (
    27  	DefaultTimeout = 30 * time.Second
    28  )
    29  
    30  // Errors you may receive from this package.
    31  var (
    32  	// ErrInvalidStatusCode matches ANY ReqError when using errors.Is.
    33  	// You should instead use errors.As if you need the response data.
    34  	// Find an example of errors.As in the Login() method.
    35  	ErrInvalidStatusCode = &ReqError{Code: -1}
    36  	// ErrNilClient is returned if you attempt a request with a nil http.Client.
    37  	ErrNilClient = fmt.Errorf("http.Client must not be nil")
    38  	// ErrNilInterface is returned by *Into() methods when a nil interface is provided.
    39  	ErrNilInterface = fmt.Errorf("cannot unmarshal data into a nil or empty interface")
    40  	// ErrInvalidAPIKey is returned if we know the API key didn't work.
    41  	ErrInvalidAPIKey = fmt.Errorf("API Key may be incorrect")
    42  	// ErrRequestError is returned when bad input is provided.
    43  	ErrRequestError = fmt.Errorf("request error")
    44  )
    45  
    46  // Config is the data needed to poll Radarr or Sonarr or Lidarr or Readarr.
    47  // At a minimum, provide a URL and API Key.
    48  // HTTPUser and HTTPPass are used for Basic HTTP auth, if enabled (not common).
    49  // Username and Password are for non-API paths with native authentication enabled.
    50  type Config struct {
    51  	APIKey   string       `json:"apiKey" toml:"api_key" xml:"api_key" yaml:"apiKey"`
    52  	URL      string       `json:"url" toml:"url" xml:"url" yaml:"url"`
    53  	HTTPPass string       `json:"httpPass" toml:"http_pass" xml:"http_pass" yaml:"httpPass"`
    54  	HTTPUser string       `json:"httpUser" toml:"http_user" xml:"http_user" yaml:"httpUser"`
    55  	Username string       `json:"username" toml:"username" xml:"username" yaml:"username"`
    56  	Password string       `json:"password" toml:"password" xml:"password" yaml:"password"`
    57  	Client   *http.Client `json:"-" toml:"-" xml:"-" yaml:"-"`
    58  	cookie   bool         // this probably doesn't work right.
    59  }
    60  
    61  // New returns a *starr.Config pointer. This pointer is safe to modify
    62  // further before passing it into one of the starr app New() procedures.
    63  func New(apiKey, appURL string, timeout time.Duration) *Config {
    64  	if timeout == 0 {
    65  		timeout = DefaultTimeout
    66  	}
    67  
    68  	return &Config{
    69  		APIKey: apiKey,
    70  		URL:    appURL,
    71  		Client: Client(timeout, false),
    72  	}
    73  }