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
}