mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
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:
parent
fb9dcc23a0
commit
f3cfeed8cd
19 changed files with 475 additions and 191 deletions
|
@ -350,3 +350,10 @@ func (t TorrentBasic) ReleaseSizeBytes() uint64 {
|
||||||
}
|
}
|
||||||
return releaseSizeBytes
|
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"`
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
// 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.
|
// 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) {
|
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
|
// 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 {
|
switch release.Indexer {
|
||||||
case "ptp", "btn", "ggn", "redacted", "mock":
|
case "ptp", "btn", "ggn", "redacted", "mock":
|
||||||
if release.Size == 0 {
|
if release.Size == 0 {
|
||||||
s.log.Trace().Msgf("filter.Service.AdditionalSizeCheck: (%v) preparing to check via api", f.Name)
|
s.log.Trace().Msgf("filter.Service.AdditionalSizeCheck: (%s) preparing to check via api", f.Name)
|
||||||
torrentInfo, err := s.apiService.GetTorrentByID(release.Indexer, release.TorrentID)
|
torrentInfo, err := s.apiService.GetTorrentByID(ctx, release.Indexer, release.TorrentID)
|
||||||
if err != nil || torrentInfo == nil {
|
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
|
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()
|
release.Size = torrentInfo.ReleaseSizeBytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
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 indexer doesn't have api, download torrent and add to tmpPath
|
||||||
if err := release.DownloadTorrentFileCtx(ctx); err != nil {
|
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
|
return false, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -437,12 +442,12 @@ func (s *service) AdditionalSizeCheck(ctx context.Context, f domain.Filter, rele
|
||||||
// compare size against filter
|
// compare size against filter
|
||||||
match, err := checkSizeFilter(f.MinSize, f.MaxSize, release.Size)
|
match, err := checkSizeFilter(f.MinSize, f.MaxSize, release.Size)
|
||||||
if err != nil {
|
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
|
return false, err
|
||||||
}
|
}
|
||||||
//no match, lets continue to next filter
|
//no match, lets continue to next filter
|
||||||
if !match {
|
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
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ type indexerService interface {
|
||||||
GetAll() ([]*domain.IndexerDefinition, error)
|
GetAll() ([]*domain.IndexerDefinition, error)
|
||||||
GetTemplates() ([]domain.IndexerDefinition, error)
|
GetTemplates() ([]domain.IndexerDefinition, error)
|
||||||
Delete(ctx context.Context, id int) error
|
Delete(ctx context.Context, id int) error
|
||||||
|
TestApi(ctx context.Context, req domain.IndexerTestApiRequest) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type indexerHandler struct {
|
type indexerHandler struct {
|
||||||
|
@ -41,6 +42,7 @@ func (h indexerHandler) Routes(r chi.Router) {
|
||||||
r.Get("/", h.getAll)
|
r.Get("/", h.getAll)
|
||||||
r.Get("/options", h.list)
|
r.Get("/options", h.list)
|
||||||
r.Delete("/{indexerID}", h.delete)
|
r.Delete("/{indexerID}", h.delete)
|
||||||
|
r.Post("/{id}/api/test", h.testApi)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h indexerHandler) getSchema(w http.ResponseWriter, r *http.Request) {
|
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 {
|
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||||
|
h.encoder.Error(w, err)
|
||||||
return
|
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 {
|
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||||
|
h.encoder.Error(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,3 +136,39 @@ func (h indexerHandler) list(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
h.encoder.StatusResponse(ctx, w, indexers, http.StatusOK)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package indexer
|
package indexer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
|
@ -14,15 +16,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type APIService interface {
|
type APIService interface {
|
||||||
TestConnection(indexer string) (bool, error)
|
TestConnection(ctx context.Context, req domain.IndexerTestApiRequest) (bool, error)
|
||||||
GetTorrentByID(indexer string, torrentID string) (*domain.TorrentBasic, error)
|
GetTorrentByID(ctx context.Context, indexer string, torrentID string) (*domain.TorrentBasic, error)
|
||||||
AddClient(indexer string, settings map[string]string) error
|
AddClient(indexer string, settings map[string]string) error
|
||||||
RemoveClient(indexer string) error
|
RemoveClient(indexer string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type apiClient interface {
|
type apiClient interface {
|
||||||
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
|
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||||
TestAPI() (bool, error)
|
TestAPI(ctx context.Context) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type apiService struct {
|
type apiService struct {
|
||||||
|
@ -37,49 +39,43 @@ func NewAPIService(log logger.Logger) APIService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *apiService) GetTorrentByID(indexer string, torrentID string) (*domain.TorrentBasic, error) {
|
func (s *apiService) GetTorrentByID(ctx context.Context, indexer string, torrentID string) (*domain.TorrentBasic, error) {
|
||||||
v, ok := s.apiClients[indexer]
|
client, err := s.getApiClient(indexer)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return nil, 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 {
|
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
|
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) {
|
func (s *apiService) TestConnection(ctx context.Context, req domain.IndexerTestApiRequest) (bool, error) {
|
||||||
v, ok := s.apiClients[indexer]
|
client, err := s.getClientForTest(req)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return false, 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 {
|
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 false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return t, nil
|
return success, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *apiService) AddClient(indexer string, settings map[string]string) error {
|
func (s *apiService) AddClient(indexer string, settings map[string]string) error {
|
||||||
// basic validation
|
s.log.Trace().Msgf("api.Service.AddClient: init api client for: %s", indexer)
|
||||||
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)
|
|
||||||
|
|
||||||
// init client
|
// init client
|
||||||
switch indexer {
|
switch indexer {
|
||||||
|
@ -100,32 +96,82 @@ func (s *apiService) AddClient(indexer string, settings map[string]string) error
|
||||||
if !ok || key == "" {
|
if !ok || key == "" {
|
||||||
return errors.New("api.Service.AddClient: could not initialize ptp client: missing var 'api_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":
|
case "ggn":
|
||||||
key, ok := settings["api_key"]
|
key, ok := settings["api_key"]
|
||||||
if !ok || key == "" {
|
if !ok || key == "" {
|
||||||
return errors.New("api.Service.AddClient: could not initialize ggn client: missing var 'api_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":
|
case "redacted":
|
||||||
key, ok := settings["api_key"]
|
key, ok := settings["api_key"]
|
||||||
if !ok || key == "" {
|
if !ok || key == "" {
|
||||||
return errors.New("api.Service.AddClient: could not initialize red client: missing var 'api_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":
|
case "mock":
|
||||||
s.apiClients[indexer] = mock.NewMockClient("", "mock")
|
s.apiClients[indexer] = mock.NewMockClient("", "mock")
|
||||||
|
|
||||||
default:
|
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
|
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 {
|
func (s *apiService) RemoveClient(indexer string) error {
|
||||||
_, ok := s.apiClients[indexer]
|
_, ok := s.apiClients[indexer]
|
||||||
if ok {
|
if ok {
|
||||||
|
|
|
@ -32,13 +32,14 @@ type Service interface {
|
||||||
GetIndexersByIRCNetwork(server string) []*domain.IndexerDefinition
|
GetIndexersByIRCNetwork(server string) []*domain.IndexerDefinition
|
||||||
GetTorznabIndexers() []domain.IndexerDefinition
|
GetTorznabIndexers() []domain.IndexerDefinition
|
||||||
Start() error
|
Start() error
|
||||||
|
TestApi(ctx context.Context, req domain.IndexerTestApiRequest) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
log zerolog.Logger
|
log zerolog.Logger
|
||||||
config *domain.Config
|
config *domain.Config
|
||||||
repo domain.IndexerRepo
|
repo domain.IndexerRepo
|
||||||
apiService APIService
|
ApiService APIService
|
||||||
scheduler scheduler.Service
|
scheduler scheduler.Service
|
||||||
|
|
||||||
// contains all raw indexer definitions
|
// 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(),
|
log: log.With().Str("module", "indexer").Logger(),
|
||||||
config: config,
|
config: config,
|
||||||
repo: repo,
|
repo: repo,
|
||||||
apiService: apiService,
|
ApiService: apiService,
|
||||||
scheduler: scheduler,
|
scheduler: scheduler,
|
||||||
lookupIRCServerDefinition: make(map[string]map[string]*domain.IndexerDefinition),
|
lookupIRCServerDefinition: make(map[string]map[string]*domain.IndexerDefinition),
|
||||||
torznabIndexers: make(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
|
// add to indexerInstances
|
||||||
err = s.updateIndexer(*i)
|
if err = s.updateIndexer(*i); err != nil {
|
||||||
if err != nil {
|
|
||||||
s.log.Error().Err(err).Msgf("failed to add indexer: %v", indexer.Name)
|
s.log.Error().Err(err).Msgf("failed to add indexer: %v", indexer.Name)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -137,6 +137,10 @@ func (s *service) Delete(ctx context.Context, id int) error {
|
||||||
// remove from lookup tables
|
// remove from lookup tables
|
||||||
s.removeIndexer(*indexer)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,7 +336,7 @@ func (s *service) Start() error {
|
||||||
|
|
||||||
// check if it has api and add to api service
|
// check if it has api and add to api service
|
||||||
if indexer.Enabled && indexer.HasApi() {
|
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)
|
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)
|
s.mapIRCServerDefinitionLookup(indexerDefinition.IRC.Server, indexerDefinition)
|
||||||
|
|
||||||
// check if it has api and add to api service
|
// check if it has api and add to api service
|
||||||
if indexerDefinition.Enabled && indexerDefinition.HasApi() {
|
if indexerDefinition.HasApi() {
|
||||||
if err := s.apiService.AddClient(indexerDefinition.Identifier, indexerDefinition.SettingsMap); err != nil {
|
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)
|
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)
|
s.mapIRCServerDefinitionLookup(indexerDefinition.IRC.Server, indexerDefinition)
|
||||||
|
|
||||||
// check if it has api and add to api service
|
// check if it has api and add to api service
|
||||||
if indexerDefinition.Enabled && indexerDefinition.HasApi() {
|
if indexerDefinition.HasApi() {
|
||||||
if err := s.apiService.AddClient(indexerDefinition.Identifier, indexerDefinition.SettingsMap); err != nil {
|
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)
|
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
|
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) {
|
func (s *service) stopFeed(indexer string) {
|
||||||
// verify indexer is torznab indexer
|
// verify indexer is torznab indexer
|
||||||
_, ok := s.torznabIndexers[indexer]
|
_, ok := s.torznabIndexers[indexer]
|
||||||
|
@ -631,3 +643,30 @@ func (s *service) stopFeed(indexer string) {
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
"github.com/autobrr/autobrr/pkg/errors"
|
"github.com/autobrr/autobrr/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IndexerApiClient interface {
|
type IndexerApiClient interface {
|
||||||
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
|
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||||
TestAPI() (bool, error)
|
TestAPI(ctx context.Context) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type IndexerClient struct {
|
type IndexerClient struct {
|
||||||
|
@ -24,7 +26,7 @@ func NewMockClient(url string, apiKey string) IndexerApiClient {
|
||||||
return c
|
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 == "" {
|
if torrentID == "" {
|
||||||
return nil, errors.New("mock client: must have 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
|
// 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
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
package btn
|
package btn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
"github.com/autobrr/autobrr/pkg/errors"
|
"github.com/autobrr/autobrr/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) TestAPI() (bool, error) {
|
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
|
||||||
res, err := c.rpcClient.Call("userInfo", [2]string{c.APIKey})
|
res, err := c.rpcClient.CallCtx(ctx, "userInfo", [2]string{c.APIKey})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.Wrap(err, "test api userInfo failed")
|
return false, errors.Wrap(err, "test api userInfo failed")
|
||||||
}
|
}
|
||||||
|
@ -24,12 +26,12 @@ func (c *Client) TestAPI() (bool, error) {
|
||||||
return false, nil
|
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 == "" {
|
if torrentID == "" {
|
||||||
return nil, errors.New("btn client: must have 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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "call getTorrentById failed")
|
return nil, errors.Wrap(err, "call getTorrentById failed")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package btn
|
package btn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
@ -65,7 +66,7 @@ func TestAPI(t *testing.T) {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
c := NewClient(tt.fields.Url, tt.fields.APIKey)
|
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) {
|
if tt.wantErr && assert.Error(t, err) {
|
||||||
assert.Equal(t, tt.wantErr, 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)
|
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) {
|
if tt.wantErr && assert.Error(t, err) {
|
||||||
assert.Equal(t, tt.wantErr, err)
|
assert.Equal(t, tt.wantErr, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,46 +4,40 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/autobrr/autobrr/internal/domain"
|
"github.com/autobrr/autobrr/internal/domain"
|
||||||
"github.com/autobrr/autobrr/pkg/errors"
|
|
||||||
"github.com/autobrr/autobrr/pkg/jsonrpc"
|
"github.com/autobrr/autobrr/pkg/jsonrpc"
|
||||||
|
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BTNClient interface {
|
type ApiClient interface {
|
||||||
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
|
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||||
TestAPI() (bool, error)
|
TestAPI(ctx context.Context) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
Timeout int
|
|
||||||
client *http.Client
|
|
||||||
rpcClient jsonrpc.Client
|
rpcClient jsonrpc.Client
|
||||||
Ratelimiter *rate.Limiter
|
Ratelimiter *rate.Limiter
|
||||||
APIKey string
|
APIKey string
|
||||||
Headers http.Header
|
|
||||||
|
|
||||||
Log *log.Logger
|
Log *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(url string, apiKey string) BTNClient {
|
func NewClient(url string, apiKey string) ApiClient {
|
||||||
if url == "" {
|
if url == "" {
|
||||||
url = "https://api.broadcasthe.net/"
|
url = "https://api.broadcasthe.net/"
|
||||||
}
|
}
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
client: http.DefaultClient,
|
|
||||||
rpcClient: jsonrpc.NewClientWithOpts(url, &jsonrpc.ClientOpts{
|
rpcClient: jsonrpc.NewClientWithOpts(url, &jsonrpc.ClientOpts{
|
||||||
Headers: map[string]string{
|
Headers: map[string]string{
|
||||||
"User-Agent": "autobrr",
|
"User-Agent": "autobrr",
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
APIKey: apiKey,
|
|
||||||
Ratelimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
|
Ratelimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
|
||||||
|
APIKey: apiKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Log == nil {
|
if c.Log == nil {
|
||||||
|
@ -52,16 +46,3 @@ func NewClient(url string, apiKey string) BTNClient {
|
||||||
|
|
||||||
return c
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -16,36 +16,36 @@ import (
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client interface {
|
type ApiClient interface {
|
||||||
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
|
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||||
TestAPI() (bool, error)
|
TestAPI(ctx context.Context) (bool, error)
|
||||||
|
UseURL(url string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type client struct {
|
type Client struct {
|
||||||
Url string
|
Url string
|
||||||
Timeout int
|
|
||||||
client *http.Client
|
client *http.Client
|
||||||
Ratelimiter *rate.Limiter
|
Ratelimiter *rate.Limiter
|
||||||
APIKey string
|
APIKey string
|
||||||
Headers http.Header
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(url string, apiKey string) Client {
|
func NewClient(apiKey string) ApiClient {
|
||||||
// set default url
|
c := &Client{
|
||||||
if url == "" {
|
Url: "https://gazellegames.net/api.php",
|
||||||
url = "https://gazellegames.net/api.php"
|
client: &http.Client{
|
||||||
}
|
Timeout: time.Second * 30,
|
||||||
|
},
|
||||||
c := &client{
|
|
||||||
APIKey: apiKey,
|
|
||||||
client: http.DefaultClient,
|
|
||||||
Url: url,
|
|
||||||
Ratelimiter: rate.NewLimiter(rate.Every(5*time.Second), 1), // 5 request every 10 seconds
|
Ratelimiter: rate.NewLimiter(rate.Every(5*time.Second), 1), // 5 request every 10 seconds
|
||||||
|
APIKey: apiKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) UseURL(url string) {
|
||||||
|
c.Url = url
|
||||||
|
}
|
||||||
|
|
||||||
type Group struct {
|
type Group struct {
|
||||||
BbWikiBody string `json:"bbWikiBody"`
|
BbWikiBody string `json:"bbWikiBody"`
|
||||||
WikiBody string `json:"wikiBody"`
|
WikiBody string `json:"wikiBody"`
|
||||||
|
@ -145,7 +145,7 @@ type Response struct {
|
||||||
Error string `json:"error,omitempty"`
|
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()
|
ctx := context.Background()
|
||||||
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
|
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -158,10 +158,10 @@ func (c *client) Do(req *http.Request) (*http.Response, error) {
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) get(url string) (*http.Response, error) {
|
func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
||||||
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
|
||||||
if err != nil {
|
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)
|
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)
|
res, err := c.Do(req)
|
||||||
if err != nil {
|
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 {
|
if res.StatusCode == http.StatusUnauthorized {
|
||||||
|
@ -183,7 +183,7 @@ func (c *client) get(url string) (*http.Response, error) {
|
||||||
return res, nil
|
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 == "" {
|
if torrentID == "" {
|
||||||
return nil, errors.New("ggn client: must have 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)
|
v.Add("id", torrentID)
|
||||||
params := v.Encode()
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "error getting data")
|
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")
|
return nil, errors.Wrap(readErr, "error reading body")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &r)
|
if err = json.Unmarshal(body, &r); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "error unmarshal body")
|
return nil, errors.Wrap(err, "error unmarshal body")
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Status != "success" {
|
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{
|
t := &domain.TorrentBasic{
|
||||||
|
@ -228,8 +227,8 @@ func (c *client) GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestAPI try api access against torrents page
|
// TestAPI try api access against torrents page
|
||||||
func (c *client) TestAPI() (bool, error) {
|
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
|
||||||
resp, err := c.get(c.Url)
|
resp, err := c.get(ctx, c.Url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.Wrap(err, "error getting data")
|
return false, errors.Wrap(err, "error getting data")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ggn
|
package ggn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
@ -86,9 +87,10 @@ func Test_client_GetTorrentByID(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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) {
|
if tt.wantErr && assert.Error(t, err) {
|
||||||
assert.Equal(t, tt.wantErr, err)
|
assert.Equal(t, tt.wantErr, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,38 +15,38 @@ import (
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PTPClient interface {
|
type ApiClient interface {
|
||||||
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
|
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||||
TestAPI() (bool, error)
|
TestAPI(ctx context.Context) (bool, error)
|
||||||
|
UseURL(url string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
Url string
|
Url string
|
||||||
Timeout int
|
|
||||||
client *http.Client
|
client *http.Client
|
||||||
Ratelimiter *rate.Limiter
|
Ratelimiter *rate.Limiter
|
||||||
APIUser string
|
APIUser string
|
||||||
APIKey string
|
APIKey string
|
||||||
Headers http.Header
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(url string, apiUser string, apiKey string) PTPClient {
|
func NewClient(apiUser, apiKey string) ApiClient {
|
||||||
// set default url
|
|
||||||
if url == "" {
|
|
||||||
url = "https://passthepopcorn.me/torrents.php"
|
|
||||||
}
|
|
||||||
|
|
||||||
c := &Client{
|
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,
|
APIUser: apiUser,
|
||||||
APIKey: apiKey,
|
APIKey: apiKey,
|
||||||
client: http.DefaultClient,
|
|
||||||
Url: url,
|
|
||||||
Ratelimiter: rate.NewLimiter(rate.Every(1*time.Second), 1), // 10 request every 10 seconds
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) UseURL(url string) {
|
||||||
|
c.Url = url
|
||||||
|
}
|
||||||
|
|
||||||
type TorrentResponse struct {
|
type TorrentResponse struct {
|
||||||
Page string `json:"Page"`
|
Page string `json:"Page"`
|
||||||
Result string `json:"Result"`
|
Result string `json:"Result"`
|
||||||
|
@ -96,8 +96,8 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) get(url string) (*http.Response, error) {
|
func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
||||||
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "ptp client request error : %v", url)
|
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
|
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 == "" {
|
if torrentID == "" {
|
||||||
return nil, errors.New("ptp client: must have 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)
|
reqUrl := fmt.Sprintf("%v?%v", c.Url, params)
|
||||||
|
|
||||||
resp, err := c.get(reqUrl)
|
resp, err := c.get(ctx, reqUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "error requesting data")
|
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")
|
return nil, errors.Wrap(readErr, "could not read body")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &r)
|
if err = json.Unmarshal(body, &r); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(readErr, "could not unmarshal body")
|
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
|
// TestAPI try api access against torrents page
|
||||||
func (c *Client) TestAPI() (bool, error) {
|
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
|
||||||
resp, err := c.get(c.Url)
|
resp, err := c.get(ctx, c.Url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.Wrap(err, "error requesting data")
|
return false, errors.Wrap(err, "error requesting data")
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return true, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ptp
|
package ptp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
@ -87,9 +88,10 @@ func TestPTPClient_GetTorrentByID(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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) {
|
if tt.wantErr && assert.Error(t, err) {
|
||||||
assert.Equal(t, tt.wantErr, err)
|
assert.Equal(t, tt.wantErr, err)
|
||||||
}
|
}
|
||||||
|
@ -163,9 +165,10 @@ func Test(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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) {
|
if tt.wantErr && assert.Error(t, err) {
|
||||||
assert.Equal(t, tt.wantErr, err)
|
assert.Equal(t, tt.wantErr, err)
|
||||||
|
|
|
@ -16,34 +16,41 @@ import (
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
type REDClient interface {
|
type ApiClient interface {
|
||||||
GetTorrentByID(torrentID string) (*domain.TorrentBasic, error)
|
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||||
TestAPI() (bool, error)
|
TestAPI(ctx context.Context) (bool, error)
|
||||||
|
UseURL(url string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
URL string
|
Url string
|
||||||
Timeout int
|
|
||||||
client *http.Client
|
client *http.Client
|
||||||
RateLimiter *rate.Limiter
|
RateLimiter *rate.Limiter
|
||||||
APIKey string
|
APIKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(url string, apiKey string) REDClient {
|
func NewClient(apiKey string) ApiClient {
|
||||||
if url == "" {
|
|
||||||
url = "https://redacted.ch/ajax.php"
|
|
||||||
}
|
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
APIKey: apiKey,
|
Url: "https://redacted.ch/ajax.php",
|
||||||
client: http.DefaultClient,
|
client: &http.Client{
|
||||||
URL: url,
|
Timeout: time.Second * 30,
|
||||||
|
},
|
||||||
RateLimiter: rate.NewLimiter(rate.Every(10*time.Second), 10),
|
RateLimiter: rate.NewLimiter(rate.Every(10*time.Second), 10),
|
||||||
|
APIKey: apiKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
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 {
|
type TorrentDetailsResponse struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Response struct {
|
Response struct {
|
||||||
|
@ -115,8 +122,8 @@ type Torrent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||||
ctx := context.Background()
|
//ctx := context.Background()
|
||||||
err := c.RateLimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
|
err := c.RateLimiter.Wait(req.Context()) // This is a blocking call. Honors the rate limit
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -127,8 +134,12 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) get(url string) (*http.Response, error) {
|
func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
||||||
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "could not build request")
|
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)
|
return nil, errors.Wrap(err, "could not make request: %+v", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode == http.StatusUnauthorized {
|
// return early if not OK
|
||||||
return nil, errors.New("unauthorized: bad credentials")
|
if res.StatusCode != http.StatusOK {
|
||||||
} else if res.StatusCode == http.StatusForbidden {
|
var r ErrorResponse
|
||||||
return nil, nil
|
|
||||||
} else if res.StatusCode == http.StatusBadRequest {
|
body, readErr := io.ReadAll(res.Body)
|
||||||
return nil, errors.New("bad id parameter")
|
if readErr != nil {
|
||||||
} else if res.StatusCode == http.StatusTooManyRequests {
|
return nil, errors.Wrap(readErr, "could not read body")
|
||||||
return nil, errors.New("rate-limited")
|
}
|
||||||
|
|
||||||
|
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
|
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 == "" {
|
if torrentID == "" {
|
||||||
return nil, errors.New("red client: must have 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)
|
v.Add("id", torrentID)
|
||||||
params := v.Encode()
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "could not get torrent by id: %v", torrentID)
|
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")
|
return nil, errors.Wrap(readErr, "could not read body")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(body, &r)
|
if err := json.Unmarshal(body, &r); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(readErr, "could not unmarshal body")
|
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
|
// TestAPI try api access against torrents page
|
||||||
func (c *Client) TestAPI() (bool, error) {
|
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
|
||||||
resp, err := c.get(c.URL + "?action=index")
|
resp, err := c.get(ctx, c.Url+"?action=index")
|
||||||
if err != nil {
|
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()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return true, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package red
|
package red
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
@ -79,7 +80,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
|
||||||
},
|
},
|
||||||
args: args{torrentID: "100002"},
|
args: args{torrentID: "100002"},
|
||||||
want: nil,
|
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",
|
name: "get_by_id_3",
|
||||||
|
@ -89,14 +90,15 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
|
||||||
},
|
},
|
||||||
args: args{torrentID: "100002"},
|
args: args{torrentID: "100002"},
|
||||||
want: nil,
|
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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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) {
|
if tt.wantErr != "" && assert.Error(t, err) {
|
||||||
assert.EqualErrorf(t, err, tt.wantErr, "Error should be: %v, got: %v", tt.wantErr, err)
|
assert.EqualErrorf(t, err, tt.wantErr, "Error should be: %v, got: %v", tt.wantErr, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -135,7 +135,8 @@ export const APIClient = {
|
||||||
getSchema: () => appClient.Get<IndexerDefinition[]>("api/indexer/schema"),
|
getSchema: () => appClient.Get<IndexerDefinition[]>("api/indexer/schema"),
|
||||||
create: (indexer: Indexer) => appClient.Post<Indexer>("api/indexer", indexer),
|
create: (indexer: Indexer) => appClient.Post<Indexer>("api/indexer", indexer),
|
||||||
update: (indexer: Indexer) => appClient.Put("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: {
|
irc: {
|
||||||
getNetworks: () => appClient.Get<IrcNetworkWithHealth[]>("api/irc"),
|
getNetworks: () => appClient.Get<IrcNetworkWithHealth[]>("api/irc"),
|
||||||
|
|
|
@ -22,6 +22,7 @@ interface SlideOverProps<DataType> {
|
||||||
isTesting?: boolean;
|
isTesting?: boolean;
|
||||||
isTestSuccessful?: boolean;
|
isTestSuccessful?: boolean;
|
||||||
isTestError?: boolean;
|
isTestError?: boolean;
|
||||||
|
extraButtons?: (values: DataType) => React.ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
function SlideOver<DataType>({
|
function SlideOver<DataType>({
|
||||||
|
@ -37,7 +38,8 @@ function SlideOver<DataType>({
|
||||||
testFn,
|
testFn,
|
||||||
isTesting,
|
isTesting,
|
||||||
isTestSuccessful,
|
isTestSuccessful,
|
||||||
isTestError
|
isTestError,
|
||||||
|
extraButtons
|
||||||
}: SlideOverProps<DataType>): React.ReactElement {
|
}: SlideOverProps<DataType>): React.ReactElement {
|
||||||
const cancelModalButtonRef = useRef<HTMLInputElement | null>(null);
|
const cancelModalButtonRef = useRef<HTMLInputElement | null>(null);
|
||||||
const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false);
|
const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false);
|
||||||
|
@ -125,6 +127,10 @@ function SlideOver<DataType>({
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
<div>
|
<div>
|
||||||
|
{!!values && extraButtons !== undefined && (
|
||||||
|
extraButtons(values)
|
||||||
|
)}
|
||||||
|
|
||||||
{testFn && (
|
{testFn && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Fragment, useState } from "react";
|
import React, { Fragment, useState } from "react";
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import { useMutation, useQuery } from "react-query";
|
import { useMutation, useQuery } from "react-query";
|
||||||
import Select, { components, ControlProps, InputProps, MenuProps, OptionProps } from "react-select";
|
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 { XMarkIcon } from "@heroicons/react/24/solid";
|
||||||
import { Dialog, Transition } from "@headlessui/react";
|
import { Dialog, Transition } from "@headlessui/react";
|
||||||
|
|
||||||
import { sleep } from "../../utils";
|
import { classNames, sleep } from "../../utils";
|
||||||
import { queryClient } from "../../App";
|
import { queryClient } from "../../App";
|
||||||
import DEBUG from "../../components/debug";
|
import DEBUG from "../../components/debug";
|
||||||
import { APIClient } from "../../api/APIClient";
|
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 {
|
interface UpdateProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
toggle: () => void;
|
toggle: () => void;
|
||||||
|
@ -635,10 +758,10 @@ export function IndexerUpdateForm({ isOpen, toggle, indexer }: UpdateProps) {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialValues = {
|
const initialValues: IndexerUpdateInitialValues = {
|
||||||
id: indexer.id,
|
id: indexer.id,
|
||||||
name: indexer.name,
|
name: indexer.name,
|
||||||
enabled: indexer.enabled,
|
enabled: indexer.enabled || false,
|
||||||
identifier: indexer.identifier,
|
identifier: indexer.identifier,
|
||||||
implementation: indexer.implementation,
|
implementation: indexer.implementation,
|
||||||
base_url: indexer.base_url,
|
base_url: indexer.base_url,
|
||||||
|
@ -660,6 +783,7 @@ export function IndexerUpdateForm({ isOpen, toggle, indexer }: UpdateProps) {
|
||||||
deleteAction={deleteAction}
|
deleteAction={deleteAction}
|
||||||
onSubmit={onSubmit}
|
onSubmit={onSubmit}
|
||||||
initialValues={initialValues}
|
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">
|
<div className="py-2 space-y-6 sm:py-0 sm:space-y-0 divide-y divide-gray-200 dark:divide-gray-700">
|
||||||
|
|
7
web/src/types/Indexer.d.ts
vendored
7
web/src/types/Indexer.d.ts
vendored
|
@ -78,3 +78,10 @@ interface IndexerParseMatch {
|
||||||
torrentUrl: string;
|
torrentUrl: string;
|
||||||
encode: string[];
|
encode: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IndexerTestApiReq {
|
||||||
|
id?: number;
|
||||||
|
identifier?: string;
|
||||||
|
api_user?: string;
|
||||||
|
api_key: string;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue