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

@ -350,3 +350,10 @@ func (t TorrentBasic) ReleaseSizeBytes() uint64 {
}
return releaseSizeBytes
}
type IndexerTestApiRequest struct {
IndexerId int `json:"id,omitempty"`
Identifier string `json:"identifier,omitempty"`
ApiUser string `json:"api_user,omitempty"`
ApiKey string `json:"api_key"`
}

View file

@ -405,31 +405,36 @@ func (s *service) CheckFilter(ctx context.Context, f domain.Filter, release *dom
// additional size check. Some indexers have api implemented to fetch this data and for the others
// it will download the torrent file to parse and make the size check. This is all to minimize the amount of downloads.
func (s *service) AdditionalSizeCheck(ctx context.Context, f domain.Filter, release *domain.Release) (bool, error) {
var err error
defer func() {
// try recover panic if anything went wrong with API or size checks
errors.RecoverPanic(recover(), &err)
}()
// do additional size check against indexer api or torrent for size
s.log.Debug().Msgf("filter.Service.AdditionalSizeCheck: (%v) additional size check required", f.Name)
s.log.Debug().Msgf("filter.Service.AdditionalSizeCheck: (%s) additional size check required", f.Name)
switch release.Indexer {
case "ptp", "btn", "ggn", "redacted", "mock":
if release.Size == 0 {
s.log.Trace().Msgf("filter.Service.AdditionalSizeCheck: (%v) preparing to check via api", f.Name)
torrentInfo, err := s.apiService.GetTorrentByID(release.Indexer, release.TorrentID)
s.log.Trace().Msgf("filter.Service.AdditionalSizeCheck: (%s) preparing to check via api", f.Name)
torrentInfo, err := s.apiService.GetTorrentByID(ctx, release.Indexer, release.TorrentID)
if err != nil || torrentInfo == nil {
s.log.Error().Stack().Err(err).Msgf("filter.Service.AdditionalSizeCheck: (%v) could not get torrent info from api: '%v' from: %v", f.Name, release.TorrentID, release.Indexer)
s.log.Error().Stack().Err(err).Msgf("filter.Service.AdditionalSizeCheck: (%s) could not get torrent info from api: '%s' from: %s", f.Name, release.TorrentID, release.Indexer)
return false, err
}
s.log.Debug().Msgf("filter.Service.AdditionalSizeCheck: (%v) got torrent info from api: %+v", f.Name, torrentInfo)
s.log.Debug().Msgf("filter.Service.AdditionalSizeCheck: (%s) got torrent info from api: %+v", f.Name, torrentInfo)
release.Size = torrentInfo.ReleaseSizeBytes()
}
default:
s.log.Trace().Msgf("filter.Service.AdditionalSizeCheck: (%v) preparing to download torrent metafile", f.Name)
s.log.Trace().Msgf("filter.Service.AdditionalSizeCheck: (%s) preparing to download torrent metafile", f.Name)
// if indexer doesn't have api, download torrent and add to tmpPath
if err := release.DownloadTorrentFileCtx(ctx); err != nil {
s.log.Error().Stack().Err(err).Msgf("filter.Service.AdditionalSizeCheck: (%v) could not download torrent file with id: '%v' from: %v", f.Name, release.TorrentID, release.Indexer)
s.log.Error().Stack().Err(err).Msgf("filter.Service.AdditionalSizeCheck: (%s) could not download torrent file with id: '%s' from: %s", f.Name, release.TorrentID, release.Indexer)
return false, err
}
}
@ -437,12 +442,12 @@ func (s *service) AdditionalSizeCheck(ctx context.Context, f domain.Filter, rele
// compare size against filter
match, err := checkSizeFilter(f.MinSize, f.MaxSize, release.Size)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("filter.Service.AdditionalSizeCheck: (%v) error checking extra size filter", f.Name)
s.log.Error().Stack().Err(err).Msgf("filter.Service.AdditionalSizeCheck: (%s) error checking extra size filter", f.Name)
return false, err
}
//no match, lets continue to next filter
if !match {
s.log.Debug().Msgf("filter.Service.AdditionalSizeCheck: (%v) filter did not match after additional size check, trying next", f.Name)
s.log.Debug().Msgf("filter.Service.AdditionalSizeCheck: (%s) filter did not match after additional size check, trying next", f.Name)
return false, nil
}

View file

@ -18,6 +18,7 @@ type indexerService interface {
GetAll() ([]*domain.IndexerDefinition, error)
GetTemplates() ([]domain.IndexerDefinition, error)
Delete(ctx context.Context, id int) error
TestApi(ctx context.Context, req domain.IndexerTestApiRequest) error
}
type indexerHandler struct {
@ -41,6 +42,7 @@ func (h indexerHandler) Routes(r chi.Router) {
r.Get("/", h.getAll)
r.Get("/options", h.list)
r.Delete("/{indexerID}", h.delete)
r.Post("/{id}/api/test", h.testApi)
}
func (h indexerHandler) getSchema(w http.ResponseWriter, r *http.Request) {
@ -62,6 +64,7 @@ func (h indexerHandler) store(w http.ResponseWriter, r *http.Request) {
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
@ -81,6 +84,7 @@ func (h indexerHandler) update(w http.ResponseWriter, r *http.Request) {
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
h.encoder.Error(w, err)
return
}
@ -132,3 +136,39 @@ func (h indexerHandler) list(w http.ResponseWriter, r *http.Request) {
h.encoder.StatusResponse(ctx, w, indexers, http.StatusOK)
}
func (h indexerHandler) testApi(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
idParam = chi.URLParam(r, "id")
req domain.IndexerTestApiRequest
)
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
h.encoder.Error(w, err)
return
}
id, err := strconv.Atoi(idParam)
if err != nil {
h.encoder.Error(w, err)
return
}
if req.IndexerId == 0 {
req.IndexerId = id
}
if err := h.service.TestApi(ctx, req); err != nil {
h.encoder.Error(w, err)
return
}
res := struct {
Message string `json:"message"`
}{
Message: "Indexer api test OK",
}
h.encoder.StatusResponse(ctx, w, res, http.StatusOK)
}

View file

@ -1,6 +1,8 @@
package indexer
import (
"context"
"github.com/rs/zerolog"
"github.com/autobrr/autobrr/internal/domain"
@ -14,15 +16,15 @@ import (
)
type APIService interface {
TestConnection(indexer string) (bool, error)
GetTorrentByID(indexer string, torrentID string) (*domain.TorrentBasic, error)
TestConnection(ctx context.Context, req domain.IndexerTestApiRequest) (bool, error)
GetTorrentByID(ctx context.Context, indexer string, torrentID string) (*domain.TorrentBasic, error)
AddClient(indexer string, settings map[string]string) error
RemoveClient(indexer string) error
}
type apiClient interface {
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
TestAPI() (bool, error)
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
type apiService struct {
@ -37,49 +39,43 @@ func NewAPIService(log logger.Logger) APIService {
}
}
func (s *apiService) GetTorrentByID(indexer string, torrentID string) (*domain.TorrentBasic, error) {
v, ok := s.apiClients[indexer]
if !ok {
return nil, nil
func (s *apiService) GetTorrentByID(ctx context.Context, indexer string, torrentID string) (*domain.TorrentBasic, error) {
client, err := s.getApiClient(indexer)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not get api client for: %s", indexer)
return nil, errors.Wrap(err, "could not get torrent via api for indexer: %s", indexer)
}
s.log.Trace().Str("method", "GetTorrentByID").Msgf("'%v' trying to fetch torrent from api", indexer)
s.log.Trace().Str("method", "GetTorrentByID").Msgf("%s fetching torrent from api...", indexer)
t, err := v.GetTorrentByID(torrentID)
torrent, err := client.GetTorrentByID(ctx, torrentID)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not get torrent: '%v' from: %v", torrentID, indexer)
s.log.Error().Stack().Err(err).Msgf("could not get torrent: %s from: %s", torrentID, indexer)
return nil, err
}
s.log.Trace().Str("method", "GetTorrentByID").Msgf("'%v' successfully fetched torrent from api: %+v", indexer, t)
s.log.Trace().Str("method", "GetTorrentByID").Msgf("%s api successfully fetched torrent: %+v", indexer, torrent)
return t, nil
return torrent, nil
}
func (s *apiService) TestConnection(indexer string) (bool, error) {
v, ok := s.apiClients[indexer]
if !ok {
return false, nil
func (s *apiService) TestConnection(ctx context.Context, req domain.IndexerTestApiRequest) (bool, error) {
client, err := s.getClientForTest(req)
if err != nil {
return false, errors.New("could not init api client: %s", req.Identifier)
}
t, err := v.TestAPI()
success, err := client.TestAPI(ctx)
if err != nil {
s.log.Error().Err(err).Msgf("error testing connection for api: %v", indexer)
s.log.Error().Err(err).Msgf("error testing connection for api: %s", req.Identifier)
return false, err
}
return t, nil
return success, nil
}
func (s *apiService) AddClient(indexer string, settings map[string]string) error {
// basic validation
if indexer == "" {
return errors.New("api.Service.AddClient: validation falied: indexer can't be empty")
} else if len(settings) == 0 {
return errors.New("api.Service.AddClient: validation falied: settings can't be empty")
}
s.log.Trace().Msgf("api.Service.AddClient: init api client for '%v'", indexer)
s.log.Trace().Msgf("api.Service.AddClient: init api client for: %s", indexer)
// init client
switch indexer {
@ -100,32 +96,82 @@ func (s *apiService) AddClient(indexer string, settings map[string]string) error
if !ok || key == "" {
return errors.New("api.Service.AddClient: could not initialize ptp client: missing var 'api_key'")
}
s.apiClients[indexer] = ptp.NewClient("", user, key)
s.apiClients[indexer] = ptp.NewClient(user, key)
case "ggn":
key, ok := settings["api_key"]
if !ok || key == "" {
return errors.New("api.Service.AddClient: could not initialize ggn client: missing var 'api_key'")
}
s.apiClients[indexer] = ggn.NewClient("", key)
s.apiClients[indexer] = ggn.NewClient(key)
case "redacted":
key, ok := settings["api_key"]
if !ok || key == "" {
return errors.New("api.Service.AddClient: could not initialize red client: missing var 'api_key'")
}
s.apiClients[indexer] = red.NewClient("", key)
s.apiClients[indexer] = red.NewClient(key)
case "mock":
s.apiClients[indexer] = mock.NewMockClient("", "mock")
default:
return errors.New("api.Service.AddClient: could not initialize client: unsupported indexer '%v'", indexer)
return errors.New("api.Service.AddClient: could not initialize client: unsupported indexer: %s", indexer)
}
return nil
}
func (s *apiService) getApiClient(indexer string) (apiClient, error) {
client, ok := s.apiClients[indexer]
if !ok {
return nil, errors.New("could not find api client for: %s", indexer)
}
return client, nil
}
func (s *apiService) getClientForTest(req domain.IndexerTestApiRequest) (apiClient, error) {
// init client
switch req.Identifier {
case "btn":
if req.ApiKey == "" {
return nil, errors.New("api.Service.AddClient: could not initialize btn client: missing var 'api_key'")
}
return btn.NewClient("", req.ApiKey), nil
case "ptp":
if req.ApiUser == "" {
return nil, errors.New("api.Service.AddClient: could not initialize ptp client: missing var 'api_user'")
}
if req.ApiKey == "" {
return nil, errors.New("api.Service.AddClient: could not initialize ptp client: missing var 'api_key'")
}
return ptp.NewClient(req.ApiUser, req.ApiKey), nil
case "ggn":
if req.ApiKey == "" {
return nil, errors.New("api.Service.AddClient: could not initialize ggn client: missing var 'api_key'")
}
return ggn.NewClient(req.ApiKey), nil
case "redacted":
if req.ApiKey == "" {
return nil, errors.New("api.Service.AddClient: could not initialize red client: missing var 'api_key'")
}
return red.NewClient(req.ApiKey), nil
case "mock":
return mock.NewMockClient("", "mock"), nil
default:
return nil, errors.New("api.Service.AddClient: could not initialize client: unsupported indexer: %s", req.Identifier)
}
}
func (s *apiService) RemoveClient(indexer string) error {
_, ok := s.apiClients[indexer]
if ok {

View file

@ -32,13 +32,14 @@ type Service interface {
GetIndexersByIRCNetwork(server string) []*domain.IndexerDefinition
GetTorznabIndexers() []domain.IndexerDefinition
Start() error
TestApi(ctx context.Context, req domain.IndexerTestApiRequest) error
}
type service struct {
log zerolog.Logger
config *domain.Config
repo domain.IndexerRepo
apiService APIService
ApiService APIService
scheduler scheduler.Service
// contains all raw indexer definitions
@ -60,7 +61,7 @@ func NewService(log logger.Logger, config *domain.Config, repo domain.IndexerRep
log: log.With().Str("module", "indexer").Logger(),
config: config,
repo: repo,
apiService: apiService,
ApiService: apiService,
scheduler: scheduler,
lookupIRCServerDefinition: make(map[string]map[string]*domain.IndexerDefinition),
torznabIndexers: make(map[string]*domain.IndexerDefinition),
@ -106,8 +107,7 @@ func (s *service) Update(ctx context.Context, indexer domain.Indexer) (*domain.I
}
// add to indexerInstances
err = s.updateIndexer(*i)
if err != nil {
if err = s.updateIndexer(*i); err != nil {
s.log.Error().Err(err).Msgf("failed to add indexer: %v", indexer.Name)
return nil, err
}
@ -137,6 +137,10 @@ func (s *service) Delete(ctx context.Context, id int) error {
// remove from lookup tables
s.removeIndexer(*indexer)
if err := s.ApiService.RemoveClient(indexer.Identifier); err != nil {
s.log.Error().Err(err).Msgf("could not delete indexer api client: %s", indexer.Identifier)
}
return nil
}
@ -332,7 +336,7 @@ func (s *service) Start() error {
// check if it has api and add to api service
if indexer.Enabled && indexer.HasApi() {
if err := s.apiService.AddClient(indexer.Identifier, indexer.SettingsMap); err != nil {
if err := s.ApiService.AddClient(indexer.Identifier, indexer.SettingsMap); err != nil {
s.log.Error().Stack().Err(err).Msgf("indexer.start: could not init api client for: '%v'", indexer.Identifier)
}
}
@ -382,8 +386,8 @@ func (s *service) addIndexer(indexer domain.Indexer) error {
s.mapIRCServerDefinitionLookup(indexerDefinition.IRC.Server, indexerDefinition)
// check if it has api and add to api service
if indexerDefinition.Enabled && indexerDefinition.HasApi() {
if err := s.apiService.AddClient(indexerDefinition.Identifier, indexerDefinition.SettingsMap); err != nil {
if indexerDefinition.HasApi() {
if err := s.ApiService.AddClient(indexerDefinition.Identifier, indexerDefinition.SettingsMap); err != nil {
s.log.Error().Stack().Err(err).Msgf("indexer.start: could not init api client for: '%v'", indexer.Identifier)
}
}
@ -418,9 +422,9 @@ func (s *service) updateIndexer(indexer domain.Indexer) error {
s.mapIRCServerDefinitionLookup(indexerDefinition.IRC.Server, indexerDefinition)
// check if it has api and add to api service
if indexerDefinition.Enabled && indexerDefinition.HasApi() {
if err := s.apiService.AddClient(indexerDefinition.Identifier, indexerDefinition.SettingsMap); err != nil {
s.log.Error().Stack().Err(err).Msgf("indexer.start: could not init api client for: '%v'", indexer.Identifier)
if indexerDefinition.HasApi() {
if err := s.ApiService.AddClient(indexerDefinition.Identifier, indexerDefinition.SettingsMap); err != nil {
s.log.Error().Stack().Err(err).Msgf("indexer.start: could not init api client for: '%s'", indexer.Identifier)
}
}
}
@ -616,6 +620,14 @@ func (s *service) getDefinitionByName(name string) *domain.IndexerDefinition {
return nil
}
func (s *service) getMappedDefinitionByName(name string) *domain.IndexerDefinition {
if v, ok := s.mappedDefinitions[name]; ok {
return v
}
return nil
}
func (s *service) stopFeed(indexer string) {
// verify indexer is torznab indexer
_, ok := s.torznabIndexers[indexer]
@ -631,3 +643,30 @@ func (s *service) stopFeed(indexer string) {
return
}
}
func (s *service) TestApi(ctx context.Context, req domain.IndexerTestApiRequest) error {
indexer, err := s.FindByID(ctx, req.IndexerId)
if err != nil {
return err
}
def := s.getMappedDefinitionByName(indexer.Identifier)
if def == nil {
return errors.New("could not find definition: %s", indexer.Identifier)
}
if !def.HasApi() {
return errors.New("indexer (%s) does not support api", indexer.Identifier)
}
req.Identifier = def.Identifier
if _, err = s.ApiService.TestConnection(ctx, req); err != nil {
s.log.Error().Err(err).Msgf("error testing api for: %s", indexer.Identifier)
return err
}
s.log.Info().Msgf("successful api test for: %s", indexer.Identifier)
return nil
}

View file

@ -1,13 +1,15 @@
package mock
import (
"context"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
)
type IndexerApiClient interface {
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
TestAPI() (bool, error)
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
type IndexerClient struct {
@ -24,7 +26,7 @@ func NewMockClient(url string, apiKey string) IndexerApiClient {
return c
}
func (c *IndexerClient) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error) {
func (c *IndexerClient) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("mock client: must have torrentID")
}
@ -40,6 +42,6 @@ func (c *IndexerClient) GetTorrentByID(torrentID string) (*domain.TorrentBasic,
}
// TestAPI try api access against torrents page
func (c *IndexerClient) TestAPI() (bool, error) {
func (c *IndexerClient) TestAPI(ctx context.Context) (bool, error) {
return true, nil
}

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)
}

View file

@ -135,7 +135,8 @@ export const APIClient = {
getSchema: () => appClient.Get<IndexerDefinition[]>("api/indexer/schema"),
create: (indexer: Indexer) => appClient.Post<Indexer>("api/indexer", indexer),
update: (indexer: Indexer) => appClient.Put("api/indexer", indexer),
delete: (id: number) => appClient.Delete(`api/indexer/${id}`)
delete: (id: number) => appClient.Delete(`api/indexer/${id}`),
testApi: (req: IndexerTestApiReq) => appClient.Post<IndexerTestApiReq>(`api/indexer/${req.id}/api/test`, req)
},
irc: {
getNetworks: () => appClient.Get<IrcNetworkWithHealth[]>("api/irc"),

View file

@ -22,6 +22,7 @@ interface SlideOverProps<DataType> {
isTesting?: boolean;
isTestSuccessful?: boolean;
isTestError?: boolean;
extraButtons?: (values: DataType) => React.ReactNode;
}
function SlideOver<DataType>({
@ -37,7 +38,8 @@ function SlideOver<DataType>({
testFn,
isTesting,
isTestSuccessful,
isTestError
isTestError,
extraButtons
}: SlideOverProps<DataType>): React.ReactElement {
const cancelModalButtonRef = useRef<HTMLInputElement | null>(null);
const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false);
@ -125,6 +127,10 @@ function SlideOver<DataType>({
</button>
)}
<div>
{!!values && extraButtons !== undefined && (
extraButtons(values)
)}
{testFn && (
<button
type="button"

View file

@ -1,4 +1,4 @@
import { Fragment, useState } from "react";
import React, { Fragment, useState } from "react";
import { toast } from "react-hot-toast";
import { useMutation, useQuery } from "react-query";
import Select, { components, ControlProps, InputProps, MenuProps, OptionProps } from "react-select";
@ -8,7 +8,7 @@ import { Field, Form, Formik, FormikValues } from "formik";
import { XMarkIcon } from "@heroicons/react/24/solid";
import { Dialog, Transition } from "@headlessui/react";
import { sleep } from "../../utils";
import { classNames, sleep } from "../../utils";
import { queryClient } from "../../App";
import DEBUG from "../../components/debug";
import { APIClient } from "../../api/APIClient";
@ -576,6 +576,129 @@ export function IndexerAddForm({ isOpen, toggle }: AddProps) {
);
}
interface TestApiButtonProps {
values: FormikValues;
}
function TestApiButton({ values }: TestApiButtonProps) {
const [isTesting, setIsTesting] = useState(false);
const [isSuccessfulTest, setIsSuccessfulTest] = useState(false);
const [isErrorTest, setIsErrorTest] = useState(false);
if (!values.settings.api_key) {
return null;
}
const testApiMutation = useMutation(
(req: IndexerTestApiReq) => APIClient.indexers.testApi(req),
{
onMutate: () => {
setIsTesting(true);
setIsErrorTest(false);
setIsSuccessfulTest(false);
},
onSuccess: () => {
toast.custom((t) => <Toast type="success" body="API test successful!" t={t} />);
sleep(1000)
.then(() => {
setIsTesting(false);
setIsSuccessfulTest(true);
})
.then(() => {
sleep(2500).then(() => {
setIsSuccessfulTest(false);
});
});
},
onError: (error: Error) => {
toast.custom((t) => <Toast type="error" body={error.message} t={t} />);
setIsTesting(false);
setIsErrorTest(true);
sleep(2500).then(() => {
setIsErrorTest(false);
});
}
}
);
const testApi = () => {
const req: IndexerTestApiReq = {
id: values.id,
api_key: values.settings.api_key
};
if (values.settings.api_user) {
req.api_user = values.settings.api_user;
}
testApiMutation.mutate(req);
};
return (
<button
type="button"
className={classNames(
isSuccessfulTest
? "text-green-500 border-green-500 bg-green-50"
: isErrorTest
? "text-red-500 border-red-500 bg-red-50"
: "border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 hover:bg-gray-50 focus:border-rose-700 active:bg-rose-700",
isTesting ? "cursor-not-allowed" : "",
"mr-2 float-left items-center px-4 py-2 border font-medium rounded-md shadow-sm text-sm transition ease-in-out duration-150 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 dark:focus:ring-blue-500"
)}
disabled={isTesting}
onClick={testApi}
>
{isTesting ? (
<svg
className="animate-spin h-5 w-5 text-green-500"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
></circle>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
></path>
</svg>
) : isSuccessfulTest ? (
"OK!"
) : isErrorTest ? (
"ERROR"
) : (
"Test API"
)}
</button>
);
}
interface IndexerUpdateInitialValues {
id: number;
name: string;
enabled: boolean;
identifier: string;
implementation: string;
base_url: string;
settings: {
api_key?: string;
api_user?: string;
authkey?: string;
torrent_pass?: string;
}
}
interface UpdateProps {
isOpen: boolean;
toggle: () => void;
@ -635,10 +758,10 @@ export function IndexerUpdateForm({ isOpen, toggle, indexer }: UpdateProps) {
);
};
const initialValues = {
const initialValues: IndexerUpdateInitialValues = {
id: indexer.id,
name: indexer.name,
enabled: indexer.enabled,
enabled: indexer.enabled || false,
identifier: indexer.identifier,
implementation: indexer.implementation,
base_url: indexer.base_url,
@ -660,6 +783,7 @@ export function IndexerUpdateForm({ isOpen, toggle, indexer }: UpdateProps) {
deleteAction={deleteAction}
onSubmit={onSubmit}
initialValues={initialValues}
extraButtons={(values) => <TestApiButton values={values as FormikValues} />}
>
{() => (
<div className="py-2 space-y-6 sm:py-0 sm:space-y-0 divide-y divide-gray-200 dark:divide-gray-700">

View file

@ -78,3 +78,10 @@ interface IndexerParseMatch {
torrentUrl: string;
encode: string[];
}
interface IndexerTestApiReq {
id?: number;
identifier?: string;
api_user?: string;
api_key: string;
}