From f3cfeed8cd4aa176f65993cd31359aac0737eeeb Mon Sep 17 00:00:00 2001 From: ze0s <43699394+zze0s@users.noreply.github.com> Date: Sat, 15 Apr 2023 23:34:27 +0200 Subject: [PATCH] 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 --- internal/domain/indexer.go | 7 ++ internal/filter/service.go | 23 +++-- internal/http/indexer.go | 40 +++++++ internal/indexer/api.go | 110 ++++++++++++++------ internal/indexer/service.go | 59 +++++++++-- internal/mock/indexer_api.go | 10 +- pkg/btn/btn.go | 10 +- pkg/btn/btn_test.go | 5 +- pkg/btn/client.go | 29 +----- pkg/ggn/ggn.go | 57 +++++----- pkg/ggn/ggn_test.go | 6 +- pkg/ptp/ptp.go | 49 +++++---- pkg/ptp/ptp_test.go | 11 +- pkg/red/red.go | 90 +++++++++------- pkg/red/red_test.go | 10 +- web/src/api/APIClient.ts | 3 +- web/src/components/panels/index.tsx | 8 +- web/src/forms/settings/IndexerForms.tsx | 132 +++++++++++++++++++++++- web/src/types/Indexer.d.ts | 7 ++ 19 files changed, 475 insertions(+), 191 deletions(-) diff --git a/internal/domain/indexer.go b/internal/domain/indexer.go index f527c23..0368e4f 100644 --- a/internal/domain/indexer.go +++ b/internal/domain/indexer.go @@ -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"` +} diff --git a/internal/filter/service.go b/internal/filter/service.go index cd57eff..3479a76 100644 --- a/internal/filter/service.go +++ b/internal/filter/service.go @@ -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 } diff --git a/internal/http/indexer.go b/internal/http/indexer.go index 0d7a7b8..88b578b 100644 --- a/internal/http/indexer.go +++ b/internal/http/indexer.go @@ -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) +} diff --git a/internal/indexer/api.go b/internal/indexer/api.go index dc75805..72883dc 100644 --- a/internal/indexer/api.go +++ b/internal/indexer/api.go @@ -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 { diff --git a/internal/indexer/service.go b/internal/indexer/service.go index e84fa90..61e75fc 100644 --- a/internal/indexer/service.go +++ b/internal/indexer/service.go @@ -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 +} diff --git a/internal/mock/indexer_api.go b/internal/mock/indexer_api.go index f613ebb..32b03d6 100644 --- a/internal/mock/indexer_api.go +++ b/internal/mock/indexer_api.go @@ -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 } diff --git a/pkg/btn/btn.go b/pkg/btn/btn.go index 2c02a7b..078afc7 100644 --- a/pkg/btn/btn.go +++ b/pkg/btn/btn.go @@ -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") } diff --git a/pkg/btn/btn_test.go b/pkg/btn/btn_test.go index 62f2ffe..d48f9a4 100644 --- a/pkg/btn/btn_test.go +++ b/pkg/btn/btn_test.go @@ -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) } diff --git a/pkg/btn/client.go b/pkg/btn/client.go index 0a2edae..ac79f5f 100644 --- a/pkg/btn/client.go +++ b/pkg/btn/client.go @@ -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 -} diff --git a/pkg/ggn/ggn.go b/pkg/ggn/ggn.go index 128e9e2..1f74cc2 100644 --- a/pkg/ggn/ggn.go +++ b/pkg/ggn/ggn.go @@ -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") } diff --git a/pkg/ggn/ggn_test.go b/pkg/ggn/ggn_test.go index 5873ebc..ce879d5 100644 --- a/pkg/ggn/ggn_test.go +++ b/pkg/ggn/ggn_test.go @@ -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) } diff --git a/pkg/ptp/ptp.go b/pkg/ptp/ptp.go index d8bf170..f0380bc 100644 --- a/pkg/ptp/ptp.go +++ b/pkg/ptp/ptp.go @@ -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 } diff --git a/pkg/ptp/ptp_test.go b/pkg/ptp/ptp_test.go index ff0a105..b4cd0bf 100644 --- a/pkg/ptp/ptp_test.go +++ b/pkg/ptp/ptp_test.go @@ -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) diff --git a/pkg/red/red.go b/pkg/red/red.go index 88544d9..524f394 100644 --- a/pkg/red/red.go +++ b/pkg/red/red.go @@ -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 } diff --git a/pkg/red/red_test.go b/pkg/red/red_test.go index 1b39186..985b45f 100644 --- a/pkg/red/red_test.go +++ b/pkg/red/red_test.go @@ -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) } diff --git a/web/src/api/APIClient.ts b/web/src/api/APIClient.ts index 11cc24a..04998bf 100644 --- a/web/src/api/APIClient.ts +++ b/web/src/api/APIClient.ts @@ -135,7 +135,8 @@ export const APIClient = { getSchema: () => appClient.Get("api/indexer/schema"), create: (indexer: Indexer) => appClient.Post("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(`api/indexer/${req.id}/api/test`, req) }, irc: { getNetworks: () => appClient.Get("api/irc"), diff --git a/web/src/components/panels/index.tsx b/web/src/components/panels/index.tsx index e7ac0c1..f190086 100644 --- a/web/src/components/panels/index.tsx +++ b/web/src/components/panels/index.tsx @@ -22,6 +22,7 @@ interface SlideOverProps { isTesting?: boolean; isTestSuccessful?: boolean; isTestError?: boolean; + extraButtons?: (values: DataType) => React.ReactNode; } function SlideOver({ @@ -37,7 +38,8 @@ function SlideOver({ testFn, isTesting, isTestSuccessful, - isTestError + isTestError, + extraButtons }: SlideOverProps): React.ReactElement { const cancelModalButtonRef = useRef(null); const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false); @@ -125,6 +127,10 @@ function SlideOver({ )}
+ {!!values && extraButtons !== undefined && ( + extraButtons(values) + )} + {testFn && ( + ); +} + +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) => } > {() => (
diff --git a/web/src/types/Indexer.d.ts b/web/src/types/Indexer.d.ts index aad130d..0280fde 100644 --- a/web/src/types/Indexer.d.ts +++ b/web/src/types/Indexer.d.ts @@ -78,3 +78,10 @@ interface IndexerParseMatch { torrentUrl: string; encode: string[]; } + +interface IndexerTestApiReq { + id?: number; + identifier?: string; + api_user?: string; + api_key: string; +}