autobrr/internal/mock/indexer_api.go
Kyle Sanderson 3234f0d919
refactor(http): implement shared transport and clients (#1288)
* fix(http): flip to a shared transport and clients

* nice threads

* that is terrible

* fake uri for magnet

* lazy locking

* why bother with r's

* flip magic params to struct

* refactor(http-clients): use separate clients with shared transport

* refactor(http-clients): add missing license header

* refactor(http-clients): defer and fix errors

---------

Co-authored-by: ze0s <ze0s@riseup.net>
2023-12-29 23:49:22 +01:00

62 lines
1.2 KiB
Go

// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package mock
import (
"context"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
)
type IndexerApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
type IndexerClient struct {
url string
APIKey string
}
type OptFunc func(client *IndexerClient)
func WithUrl(url string) OptFunc {
return func(c *IndexerClient) {
c.url = url
}
}
func NewMockClient(apiKey string, opts ...OptFunc) IndexerApiClient {
c := &IndexerClient{
url: "",
APIKey: apiKey,
}
for _, opt := range opts {
opt(c)
}
return c
}
func (c *IndexerClient) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("mock client: must have torrentID")
}
r := &domain.TorrentBasic{
Id: torrentID,
InfoHash: "",
Size: "10GB",
}
return r, nil
}
// TestAPI try api access against torrents page
func (c *IndexerClient) TestAPI(ctx context.Context) (bool, error) {
return true, nil
}