72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package qbittorrent
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
)
|
|
|
|
var trace = otel.Tracer("git.kmsign.ru/royalcat/tstor/pkg/qbittorrent")
|
|
|
|
// Client represents a qBittorrent client
|
|
type Client interface {
|
|
// Authentication auth qBittorrent client
|
|
Authentication() Authentication
|
|
// Application get qBittorrent application info
|
|
Application() Application
|
|
// Log get qBittorrent log
|
|
Log() Log
|
|
// Sync get qBittorrent events
|
|
Sync() Sync
|
|
// Transfer transfer manage
|
|
Transfer() Transfer
|
|
// Torrent manage for torrent
|
|
Torrent() Torrent
|
|
// Search api for search
|
|
Search() Search
|
|
// RSS api for rss
|
|
RSS() RSS
|
|
}
|
|
|
|
func NewClient(ctx context.Context, cfg *Config) (Client, error) {
|
|
var c = &client{config: cfg, client: newClient(cfg.ConnectionMaxIdles, cfg.ConnectionTimeout)}
|
|
return c, nil
|
|
}
|
|
|
|
func LoginClient(ctx context.Context, cfg *Config) (Client, error) {
|
|
var c = &client{config: cfg, client: newClient(cfg.ConnectionMaxIdles, cfg.ConnectionTimeout)}
|
|
if err := c.Authentication().Login(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
if cfg.RefreshCookie {
|
|
go c.refreshCookie()
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// newClient creates and returns a new clientPool
|
|
func newClient(maxIdle int, timeout time.Duration) *http.Client {
|
|
if maxIdle == 0 {
|
|
maxIdle = 128
|
|
}
|
|
if timeout == 0 {
|
|
timeout = time.Second * 3
|
|
}
|
|
|
|
return &http.Client{
|
|
Transport: &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: (&net.Dialer{
|
|
Timeout: 30 * time.Second,
|
|
KeepAlive: 30 * time.Second,
|
|
}).DialContext,
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
MaxIdleConns: maxIdle,
|
|
},
|
|
Timeout: timeout,
|
|
}
|
|
}
|