mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat(clients): add Readarr support (#490)
* Add initial Readarr support * Readarr working with MaM * feat(clients): readarr add tests
This commit is contained in:
parent
b7d2161fdb
commit
71d0424b61
16 changed files with 626 additions and 6 deletions
136
pkg/readarr/client.go
Normal file
136
pkg/readarr/client.go
Normal file
|
@ -0,0 +1,136 @@
|
|||
package readarr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *client) get(endpoint string) (int, []byte, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
req.SetBasicAuth(c.config.Username, c.config.Password)
|
||||
}
|
||||
|
||||
c.setHeaders(req)
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "readarr.http.Do(req): %+v", req)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err = io.Copy(&buf, resp.Body); err != nil {
|
||||
return resp.StatusCode, nil, errors.Wrap(err, "readarr.io.Copy")
|
||||
}
|
||||
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) post(endpoint string, data interface{}) (*http.Response, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
req.SetBasicAuth(c.config.Username, c.config.Password)
|
||||
}
|
||||
|
||||
req.Header.Add("X-Api-Key", c.config.APIKey)
|
||||
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("readarr: bad request")
|
||||
}
|
||||
|
||||
// return raw response and let the caller handle json unmarshal of body
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *client) postBody(endpoint string, data interface{}) (int, []byte, error) {
|
||||
u, err := url.Parse(c.config.Hostname)
|
||||
u.Path = path.Join(u.Path, "/api/v1/", endpoint)
|
||||
reqUrl := u.String()
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not marshal data: %+v", data)
|
||||
}
|
||||
|
||||
c.Log.Printf("readarr push JSON: %s\n", string(jsonData))
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "could not build request")
|
||||
}
|
||||
|
||||
if c.config.BasicAuth {
|
||||
req.SetBasicAuth(c.config.Username, c.config.Password)
|
||||
}
|
||||
|
||||
c.setHeaders(req)
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return 0, nil, errors.Wrap(err, "readarr.http.Do(req): %+v", req)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err = io.Copy(&buf, resp.Body); err != nil {
|
||||
return resp.StatusCode, nil, errors.Wrap(err, "readarr.io.Copy")
|
||||
}
|
||||
|
||||
if resp.StatusCode == http.StatusBadRequest {
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
} else if resp.StatusCode < 200 || resp.StatusCode > 401 {
|
||||
return resp.StatusCode, buf.Bytes(), errors.New("readarr: bad request: %v (status: %s): %s", resp.Request.RequestURI, resp.Status, buf.String())
|
||||
}
|
||||
|
||||
return resp.StatusCode, buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *client) setHeaders(req *http.Request) {
|
||||
if req.Body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
req.Header.Set("X-Api-Key", c.config.APIKey)
|
||||
}
|
149
pkg/readarr/readarr.go
Normal file
149
pkg/readarr/readarr.go
Normal file
|
@ -0,0 +1,149 @@
|
|||
package readarr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"log"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Hostname string
|
||||
APIKey string
|
||||
|
||||
// basic auth username and password
|
||||
BasicAuth bool
|
||||
Username string
|
||||
Password string
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
Test() (*SystemStatusResponse, error)
|
||||
Push(release Release) ([]string, error)
|
||||
}
|
||||
|
||||
type client struct {
|
||||
config Config
|
||||
http *http.Client
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
// New create new readarr client
|
||||
func New(config Config) Client {
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
}
|
||||
|
||||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
Log: config.Log,
|
||||
}
|
||||
|
||||
if config.Log == nil {
|
||||
// if no provided logger then use io.Discard
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
type Release struct {
|
||||
Title string `json:"title"`
|
||||
DownloadUrl string `json:"downloadUrl"`
|
||||
Size int64 `json:"size"`
|
||||
Indexer string `json:"indexer"`
|
||||
DownloadProtocol string `json:"downloadProtocol"`
|
||||
Protocol string `json:"protocol"`
|
||||
PublishDate string `json:"publishDate"`
|
||||
}
|
||||
|
||||
type PushResponse struct {
|
||||
Approved bool `json:"approved"`
|
||||
Rejected bool `json:"rejected"`
|
||||
TempRejected bool `json:"temporarilyRejected"`
|
||||
Rejections []string `json:"rejections"`
|
||||
}
|
||||
|
||||
type BadRequestResponse struct {
|
||||
PropertyName string `json:"propertyName"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
AttemptedValue string `json:"attemptedValue"`
|
||||
Severity string `json:"severity"`
|
||||
}
|
||||
|
||||
type SystemStatusResponse struct {
|
||||
AppName string `json:"appName"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func (c *client) Test() (*SystemStatusResponse, error) {
|
||||
status, res, err := c.get("system/status")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make Test")
|
||||
}
|
||||
|
||||
if status == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
}
|
||||
|
||||
c.Log.Printf("readarr system/status status: (%v) response: %v\n", status, string(res))
|
||||
|
||||
response := SystemStatusResponse{}
|
||||
err = json.Unmarshal(res, &response)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not unmarshal data")
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *client) Push(release Release) ([]string, error) {
|
||||
status, res, err := c.postBody("release/push", release)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not push release to readarr")
|
||||
}
|
||||
|
||||
c.Log.Printf("readarr release/push status: (%v) response: %v\n", status, string(res))
|
||||
|
||||
if status == http.StatusBadRequest {
|
||||
badreqResponse := make([]*BadRequestResponse, 0)
|
||||
err = json.Unmarshal(res, &badreqResponse)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not unmarshal data")
|
||||
}
|
||||
|
||||
if badreqResponse[0] != nil && badreqResponse[0].PropertyName == "Title" && badreqResponse[0].ErrorMessage == "Unable to parse" {
|
||||
rejections := []string{fmt.Sprintf("unable to parse: %v", badreqResponse[0].AttemptedValue)}
|
||||
return rejections, err
|
||||
}
|
||||
}
|
||||
|
||||
// pushResponse := make([]PushResponse, 0)
|
||||
var pushResponse PushResponse
|
||||
err = json.Unmarshal(res, &pushResponse)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not unmarshal data")
|
||||
}
|
||||
|
||||
// log and return if rejected
|
||||
if pushResponse.Rejected {
|
||||
rejections := strings.Join(pushResponse.Rejections, ", ")
|
||||
|
||||
c.Log.Printf("readarr release/push rejected %v reasons: %q\n", release.Title, rejections)
|
||||
return pushResponse.Rejections, nil
|
||||
}
|
||||
|
||||
// successful push
|
||||
return nil, nil
|
||||
}
|
158
pkg/readarr/readarr_test.go
Normal file
158
pkg/readarr/readarr_test.go
Normal file
|
@ -0,0 +1,158 @@
|
|||
package readarr
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_client_Push(t *testing.T) {
|
||||
// disable logger
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
ts := httptest.NewServer(mux)
|
||||
defer ts.Close()
|
||||
|
||||
key := "mock-key"
|
||||
|
||||
mux.HandleFunc("/api/v1/release/push", func(w http.ResponseWriter, r *http.Request) {
|
||||
// request validation logic
|
||||
apiKey := r.Header.Get("X-Api-Key")
|
||||
if apiKey != "" {
|
||||
if apiKey != key {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write(nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// read json response
|
||||
jsonPayload, _ := os.ReadFile("testdata/release_push_ok_response.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
})
|
||||
|
||||
type fields struct {
|
||||
config Config
|
||||
}
|
||||
type args struct {
|
||||
release Release
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
err error
|
||||
rejections []string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "push",
|
||||
fields: fields{
|
||||
config: Config{
|
||||
Hostname: ts.URL,
|
||||
APIKey: "",
|
||||
BasicAuth: false,
|
||||
Username: "",
|
||||
Password: "",
|
||||
},
|
||||
},
|
||||
args: args{release: Release{
|
||||
Title: "The Best Book by Famous Author [English / epub, mobi]",
|
||||
DownloadUrl: "https://www.mock-indexer.test/tor/download.php?tid=000000",
|
||||
Size: 1048576,
|
||||
Indexer: "mock-indexer",
|
||||
DownloadProtocol: "torrent",
|
||||
Protocol: "torrent",
|
||||
PublishDate: "2022-10-14T17:36:15Z",
|
||||
}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.fields.config)
|
||||
|
||||
rejections, err := c.Push(tt.args.release)
|
||||
assert.Equal(t, tt.rejections, rejections)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.err, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_client_Test(t *testing.T) {
|
||||
// disable logger
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
|
||||
key := "mock-key"
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
apiKey := r.Header.Get("X-Api-Key")
|
||||
if apiKey != "" {
|
||||
if apiKey != key {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write(nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
jsonPayload, _ := os.ReadFile("testdata/system_status_response_ok.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg Config
|
||||
want *SystemStatusResponse
|
||||
expectedErr string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "fetch",
|
||||
cfg: Config{
|
||||
Hostname: srv.URL,
|
||||
APIKey: key,
|
||||
BasicAuth: false,
|
||||
Username: "",
|
||||
Password: "",
|
||||
},
|
||||
want: &SystemStatusResponse{AppName: "Readarr", Version: "0.1.1.1320"},
|
||||
expectedErr: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "fetch_unauthorized",
|
||||
cfg: Config{
|
||||
Hostname: srv.URL,
|
||||
APIKey: "bad-mock-key",
|
||||
BasicAuth: false,
|
||||
Username: "",
|
||||
Password: "",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
expectedErr: "unauthorized: bad credentials",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := New(tt.cfg)
|
||||
|
||||
got, err := c.Test()
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.EqualErrorf(t, err, tt.expectedErr, "Error should be: %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
37
pkg/readarr/testdata/release_push_ok_response.json
vendored
Normal file
37
pkg/readarr/testdata/release_push_ok_response.json
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
{
|
||||
"guid": "PUSH-https://www.mock-indexer.test/tor/download.php?tid=000000",
|
||||
"quality": {
|
||||
"quality": {
|
||||
"id": 3,
|
||||
"name": "EPUB"
|
||||
},
|
||||
"revision": {
|
||||
"version": 1,
|
||||
"real": 0,
|
||||
"isRepack": false
|
||||
}
|
||||
},
|
||||
"qualityWeight": 301,
|
||||
"age": 0,
|
||||
"ageHours": 0.00138141025,
|
||||
"ageMinutes": 0.08288463833333333,
|
||||
"size": 1048576,
|
||||
"indexerId": 0,
|
||||
"indexer": "mock-indexer",
|
||||
"releaseHash": "",
|
||||
"title": "The Best Book by Famous Author [English / epub, mobi]",
|
||||
"discography": false,
|
||||
"sceneSource": false,
|
||||
"authorName": "Famous Author",
|
||||
"bookTitle": "The best book",
|
||||
"approved": true,
|
||||
"temporarilyRejected": false,
|
||||
"rejected": false,
|
||||
"rejections": [],
|
||||
"publishDate": "2022-10-14T17:36:15Z",
|
||||
"downloadUrl": "https://www.mock-indexer.test/tor/download.php?tid=000000",
|
||||
"downloadAllowed": true,
|
||||
"releaseWeight": 0,
|
||||
"preferredWordScore": 0,
|
||||
"protocol": "torrent"
|
||||
}
|
31
pkg/readarr/testdata/system_status_response_ok.json
vendored
Normal file
31
pkg/readarr/testdata/system_status_response_ok.json
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
{
|
||||
"appName": "Readarr",
|
||||
"version": "0.1.1.1320",
|
||||
"buildTime": "2022-05-09T22:53:15Z",
|
||||
"isDebug": false,
|
||||
"isProduction": true,
|
||||
"isAdmin": false,
|
||||
"isUserInteractive": true,
|
||||
"startupPath": "/app/bin",
|
||||
"appData": "/config",
|
||||
"osName": "alpine",
|
||||
"osVersion": "3.16.2",
|
||||
"isNetCore": true,
|
||||
"isMono": false,
|
||||
"isLinux": true,
|
||||
"isOsx": false,
|
||||
"isWindows": false,
|
||||
"isDocker": false,
|
||||
"mode": "console",
|
||||
"branch": "develop",
|
||||
"authentication": "none",
|
||||
"sqliteVersion": "3.38.5",
|
||||
"migrationVersion": 21,
|
||||
"urlBase": "",
|
||||
"runtimeVersion": "6.0.3",
|
||||
"runtimeName": "netCore",
|
||||
"startTime": "2022-10-14T17:26:21Z",
|
||||
"packageVersion": "testing-e06fe51",
|
||||
"packageAuthor": "[hotio](https://github.com/hotio)",
|
||||
"packageUpdateMechanism": "docker"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue