feat(indexers): test API from settings (#829)

* refactor(indexers): test api clients

* feat(indexers): test api connection

* fix(indexers): api client tests

* refactor: indexer api clients

* feat: add Toasts for indexer api tests

* fix: failing red tests
This commit is contained in:
ze0s 2023-04-15 23:34:27 +02:00 committed by GitHub
parent fb9dcc23a0
commit f3cfeed8cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 475 additions and 191 deletions

View file

@ -1,12 +1,14 @@
package btn
import (
"context"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
)
func (c *Client) TestAPI() (bool, error) {
res, err := c.rpcClient.Call("userInfo", [2]string{c.APIKey})
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
res, err := c.rpcClient.CallCtx(ctx, "userInfo", [2]string{c.APIKey})
if err != nil {
return false, errors.Wrap(err, "test api userInfo failed")
}
@ -24,12 +26,12 @@ func (c *Client) TestAPI() (bool, error) {
return false, nil
}
func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("btn client: must have torrentID")
}
res, err := c.rpcClient.Call("getTorrentById", [2]string{c.APIKey, torrentID})
res, err := c.rpcClient.CallCtx(ctx, "getTorrentById", [2]string{c.APIKey, torrentID})
if err != nil {
return nil, errors.Wrap(err, "call getTorrentById failed")
}

View file

@ -1,6 +1,7 @@
package btn
import (
"context"
"io"
"net/http"
"net/http/httptest"
@ -65,7 +66,7 @@ func TestAPI(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIKey)
got, err := c.TestAPI()
got, err := c.TestAPI(context.Background())
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)
}
@ -165,7 +166,7 @@ func TestClient_GetTorrentByID(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIKey)
got, err := c.GetTorrentByID(tt.args.torrentID)
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)
}

View file

@ -4,46 +4,40 @@ import (
"context"
"io"
"log"
"net/http"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/jsonrpc"
"golang.org/x/time/rate"
)
type BTNClient interface {
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
TestAPI() (bool, error)
type ApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
type Client struct {
Timeout int
client *http.Client
rpcClient jsonrpc.Client
Ratelimiter *rate.Limiter
APIKey string
Headers http.Header
Log *log.Logger
}
func NewClient(url string, apiKey string) BTNClient {
func NewClient(url string, apiKey string) ApiClient {
if url == "" {
url = "https://api.broadcasthe.net/"
}
c := &Client{
client: http.DefaultClient,
rpcClient: jsonrpc.NewClientWithOpts(url, &jsonrpc.ClientOpts{
Headers: map[string]string{
"User-Agent": "autobrr",
},
}),
APIKey: apiKey,
Ratelimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
APIKey: apiKey,
}
if c.Log == nil {
@ -52,16 +46,3 @@ func NewClient(url string, apiKey string) BTNClient {
return c
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
ctx := context.Background()
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
if err != nil {
return nil, errors.Wrap(err, "error waiting for ratelimiter")
}
resp, err := c.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "could not make request")
}
return resp, nil
}

View file

@ -16,36 +16,36 @@ import (
"golang.org/x/time/rate"
)
type Client interface {
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
TestAPI() (bool, error)
type ApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
UseURL(url string)
}
type client struct {
type Client struct {
Url string
Timeout int
client *http.Client
Ratelimiter *rate.Limiter
APIKey string
Headers http.Header
}
func NewClient(url string, apiKey string) Client {
// set default url
if url == "" {
url = "https://gazellegames.net/api.php"
}
c := &client{
APIKey: apiKey,
client: http.DefaultClient,
Url: url,
func NewClient(apiKey string) ApiClient {
c := &Client{
Url: "https://gazellegames.net/api.php",
client: &http.Client{
Timeout: time.Second * 30,
},
Ratelimiter: rate.NewLimiter(rate.Every(5*time.Second), 1), // 5 request every 10 seconds
APIKey: apiKey,
}
return c
}
func (c *Client) UseURL(url string) {
c.Url = url
}
type Group struct {
BbWikiBody string `json:"bbWikiBody"`
WikiBody string `json:"wikiBody"`
@ -145,7 +145,7 @@ type Response struct {
Error string `json:"error,omitempty"`
}
func (c *client) Do(req *http.Request) (*http.Response, error) {
func (c *Client) Do(req *http.Request) (*http.Response, error) {
ctx := context.Background()
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
if err != nil {
@ -158,10 +158,10 @@ func (c *client) Do(req *http.Request) (*http.Response, error) {
return resp, nil
}
func (c *client) get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, errors.Wrap(err, "ggn client request error : %v", url)
return nil, errors.Wrap(err, "ggn client request error : %s", url)
}
req.Header.Add("X-API-Key", c.APIKey)
@ -169,7 +169,7 @@ func (c *client) get(url string) (*http.Response, error) {
res, err := c.Do(req)
if err != nil {
return nil, errors.Wrap(err, "ggn client request error : %v", url)
return nil, errors.Wrap(err, "ggn client request error : %s", url)
}
if res.StatusCode == http.StatusUnauthorized {
@ -183,7 +183,7 @@ func (c *client) get(url string) (*http.Response, error) {
return res, nil
}
func (c *client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("ggn client: must have torrentID")
}
@ -194,9 +194,9 @@ func (c *client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
v.Add("id", torrentID)
params := v.Encode()
reqUrl := fmt.Sprintf("%v?%v&%v", c.Url, "request=torrent", params)
reqUrl := fmt.Sprintf("%s?%s&%s", c.Url, "request=torrent", params)
resp, err := c.get(reqUrl)
resp, err := c.get(ctx, reqUrl)
if err != nil {
return nil, errors.Wrap(err, "error getting data")
}
@ -208,13 +208,12 @@ func (c *client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
return nil, errors.Wrap(readErr, "error reading body")
}
err = json.Unmarshal(body, &r)
if err != nil {
if err = json.Unmarshal(body, &r); err != nil {
return nil, errors.Wrap(err, "error unmarshal body")
}
if r.Status != "success" {
return nil, errors.New("bad status: %v", r.Status)
return nil, errors.New("bad status: %s", r.Status)
}
t := &domain.TorrentBasic{
@ -228,8 +227,8 @@ func (c *client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
}
// TestAPI try api access against torrents page
func (c *client) TestAPI() (bool, error) {
resp, err := c.get(c.Url)
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
resp, err := c.get(ctx, c.Url)
if err != nil {
return false, errors.Wrap(err, "error getting data")
}

View file

@ -1,6 +1,7 @@
package ggn
import (
"context"
"net/http"
"net/http/httptest"
"os"
@ -86,9 +87,10 @@ func Test_client_GetTorrentByID(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIKey)
c := NewClient(tt.fields.APIKey)
c.UseURL(tt.fields.Url)
got, err := c.GetTorrentByID(tt.args.torrentID)
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)
}

View file

@ -15,38 +15,38 @@ import (
"golang.org/x/time/rate"
)
type PTPClient interface {
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
TestAPI() (bool, error)
type ApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
UseURL(url string)
}
type Client struct {
Url string
Timeout int
client *http.Client
Ratelimiter *rate.Limiter
APIUser string
APIKey string
Headers http.Header
}
func NewClient(url string, apiUser string, apiKey string) PTPClient {
// set default url
if url == "" {
url = "https://passthepopcorn.me/torrents.php"
}
func NewClient(apiUser, apiKey string) ApiClient {
c := &Client{
Url: "https://passthepopcorn.me/torrents.php",
client: &http.Client{
Timeout: time.Second * 30,
},
Ratelimiter: rate.NewLimiter(rate.Every(1*time.Second), 1), // 10 request every 10 seconds
APIUser: apiUser,
APIKey: apiKey,
client: http.DefaultClient,
Url: url,
Ratelimiter: rate.NewLimiter(rate.Every(1*time.Second), 1), // 10 request every 10 seconds
}
return c
}
func (c *Client) UseURL(url string) {
c.Url = url
}
type TorrentResponse struct {
Page string `json:"Page"`
Result string `json:"Result"`
@ -96,8 +96,8 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
return resp, nil
}
func (c *Client) get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, errors.Wrap(err, "ptp client request error : %v", url)
}
@ -122,7 +122,7 @@ func (c *Client) get(url string) (*http.Response, error) {
return res, nil
}
func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("ptp client: must have torrentID")
}
@ -135,7 +135,7 @@ func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
reqUrl := fmt.Sprintf("%v?%v", c.Url, params)
resp, err := c.get(reqUrl)
resp, err := c.get(ctx, reqUrl)
if err != nil {
return nil, errors.Wrap(err, "error requesting data")
}
@ -147,8 +147,7 @@ func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
return nil, errors.Wrap(readErr, "could not read body")
}
err = json.Unmarshal(body, &r)
if err != nil {
if err = json.Unmarshal(body, &r); err != nil {
return nil, errors.Wrap(readErr, "could not unmarshal body")
}
@ -166,17 +165,17 @@ func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
}
// TestAPI try api access against torrents page
func (c *Client) TestAPI() (bool, error) {
resp, err := c.get(c.Url)
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
resp, err := c.get(ctx, c.Url)
if err != nil {
return false, errors.Wrap(err, "error requesting data")
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return true, nil
if resp.StatusCode != http.StatusOK {
return false, nil
}
return false, nil
return true, nil
}

View file

@ -1,6 +1,7 @@
package ptp
import (
"context"
"net/http"
"net/http/httptest"
"os"
@ -87,9 +88,10 @@ func TestPTPClient_GetTorrentByID(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIUser, tt.fields.APIKey)
c := NewClient(tt.fields.APIUser, tt.fields.APIKey)
c.UseURL(tt.fields.Url)
got, err := c.GetTorrentByID(tt.args.torrentID)
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)
}
@ -163,9 +165,10 @@ func Test(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIUser, tt.fields.APIKey)
c := NewClient(tt.fields.APIUser, tt.fields.APIKey)
c.UseURL(tt.fields.Url)
got, err := c.TestAPI()
got, err := c.TestAPI(context.Background())
if tt.wantErr && assert.Error(t, err) {
assert.Equal(t, tt.wantErr, err)

View file

@ -16,34 +16,41 @@ import (
"golang.org/x/time/rate"
)
type REDClient interface {
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
TestAPI() (bool, error)
type ApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
UseURL(url string)
}
type Client struct {
URL string
Timeout int
Url string
client *http.Client
RateLimiter *rate.Limiter
APIKey string
}
func NewClient(url string, apiKey string) REDClient {
if url == "" {
url = "https://redacted.ch/ajax.php"
}
func NewClient(apiKey string) ApiClient {
c := &Client{
APIKey: apiKey,
client: http.DefaultClient,
URL: url,
Url: "https://redacted.ch/ajax.php",
client: &http.Client{
Timeout: time.Second * 30,
},
RateLimiter: rate.NewLimiter(rate.Every(10*time.Second), 10),
APIKey: apiKey,
}
return c
}
func (c *Client) UseURL(url string) {
c.Url = url
}
type ErrorResponse struct {
Status string `json:"status"`
Error string `json:"error,omitempty"`
}
type TorrentDetailsResponse struct {
Status string `json:"status"`
Response struct {
@ -115,8 +122,8 @@ type Torrent struct {
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
ctx := context.Background()
err := c.RateLimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
//ctx := context.Background()
err := c.RateLimiter.Wait(req.Context()) // This is a blocking call. Honors the rate limit
if err != nil {
return nil, err
}
@ -127,8 +134,12 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
return resp, nil
}
func (c *Client) get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
if c.APIKey == "" {
return nil, errors.New("RED client missing API key!")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, errors.Wrap(err, "could not build request")
}
@ -141,20 +152,28 @@ func (c *Client) get(url string) (*http.Response, error) {
return nil, errors.Wrap(err, "could not make request: %+v", req)
}
if res.StatusCode == http.StatusUnauthorized {
return nil, errors.New("unauthorized: bad credentials")
} else if res.StatusCode == http.StatusForbidden {
return nil, nil
} else if res.StatusCode == http.StatusBadRequest {
return nil, errors.New("bad id parameter")
} else if res.StatusCode == http.StatusTooManyRequests {
return nil, errors.New("rate-limited")
// return early if not OK
if res.StatusCode != http.StatusOK {
var r ErrorResponse
body, readErr := io.ReadAll(res.Body)
if readErr != nil {
return nil, errors.Wrap(readErr, "could not read body")
}
if err = json.Unmarshal(body, &r); err != nil {
return nil, errors.Wrap(readErr, "could not unmarshal body")
}
res.Body.Close()
return nil, errors.New("status code: %d status: %s error: %s", res.StatusCode, r.Status, r.Error)
}
return res, nil
}
func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("red client: must have torrentID")
}
@ -165,9 +184,9 @@ func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
v.Add("id", torrentID)
params := v.Encode()
reqUrl := fmt.Sprintf("%v?action=torrent&%v", c.URL, params)
reqUrl := fmt.Sprintf("%s?action=torrent&%s", c.Url, params)
resp, err := c.get(reqUrl)
resp, err := c.get(ctx, reqUrl)
if err != nil {
return nil, errors.Wrap(err, "could not get torrent by id: %v", torrentID)
}
@ -179,8 +198,7 @@ func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
return nil, errors.Wrap(readErr, "could not read body")
}
err = json.Unmarshal(body, &r)
if err != nil {
if err := json.Unmarshal(body, &r); err != nil {
return nil, errors.Wrap(readErr, "could not unmarshal body")
}
@ -193,17 +211,17 @@ func (c *Client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
}
// TestAPI try api access against torrents page
func (c *Client) TestAPI() (bool, error) {
resp, err := c.get(c.URL + "?action=index")
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
resp, err := c.get(ctx, c.Url+"?action=index")
if err != nil {
return false, errors.Wrap(err, "could not run test api")
return false, errors.Wrap(err, "test api error")
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return true, nil
if resp.StatusCode != http.StatusOK {
return false, nil
}
return false, nil
return true, nil
}

View file

@ -1,6 +1,7 @@
package red
import (
"context"
"net/http"
"net/http/httptest"
"os"
@ -79,7 +80,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
},
args: args{torrentID: "100002"},
want: nil,
wantErr: "could not get torrent by id: 100002: bad id parameter",
wantErr: "could not get torrent by id: 100002: status code: 400 status: failure error: bad id parameter",
},
{
name: "get_by_id_3",
@ -89,14 +90,15 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
},
args: args{torrentID: "100002"},
want: nil,
wantErr: "could not get torrent by id: 100002: unauthorized: bad credentials",
wantErr: "could not get torrent by id: 100002: RED client missing API key!",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.Url, tt.fields.APIKey)
c := NewClient(tt.fields.APIKey)
c.UseURL(tt.fields.Url)
got, err := c.GetTorrentByID(tt.args.torrentID)
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
if tt.wantErr != "" && assert.Error(t, err) {
assert.EqualErrorf(t, err, tt.wantErr, "Error should be: %v, got: %v", tt.wantErr, err)
}