mirror of
https://github.com/idanoo/autobrr
synced 2025-07-22 16:29:12 +00:00
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>
This commit is contained in:
parent
2a4fb7750b
commit
3234f0d919
48 changed files with 537 additions and 391 deletions
|
@ -7,42 +7,61 @@ import (
|
|||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/jsonrpc"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
const DefaultURL = "https://api.broadcasthe.net/"
|
||||
|
||||
type ApiClient interface {
|
||||
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||
TestAPI(ctx context.Context) (bool, error)
|
||||
}
|
||||
|
||||
type OptFunc func(*Client)
|
||||
|
||||
func WithUrl(url string) OptFunc {
|
||||
return func(c *Client) {
|
||||
c.url = url
|
||||
}
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
rpcClient jsonrpc.Client
|
||||
Ratelimiter *rate.Limiter
|
||||
rateLimiter *rate.Limiter
|
||||
APIKey string
|
||||
url string
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
func NewClient(url string, apiKey string) ApiClient {
|
||||
if url == "" {
|
||||
url = "https://api.broadcasthe.net/"
|
||||
}
|
||||
|
||||
func NewClient(apiKey string, opts ...OptFunc) ApiClient {
|
||||
c := &Client{
|
||||
rpcClient: jsonrpc.NewClientWithOpts(url, &jsonrpc.ClientOpts{
|
||||
Headers: map[string]string{
|
||||
"User-Agent": "autobrr",
|
||||
},
|
||||
}),
|
||||
Ratelimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
|
||||
url: DefaultURL,
|
||||
rateLimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
c.rpcClient = jsonrpc.NewClientWithOpts(c.url, &jsonrpc.ClientOpts{
|
||||
Headers: map[string]string{
|
||||
"User-Agent": "autobrr",
|
||||
},
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: time.Second * 60,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
})
|
||||
|
||||
if c.Log == nil {
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue