tstor/pkg/qbittorrent/client.go

73 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-08-31 23:00:13 +00:00
package qbittorrent
2024-09-08 18:39:11 +00:00
import (
"context"
"crypto/tls"
"net"
"net/http"
"time"
"go.opentelemetry.io/otel"
)
var trace = otel.Tracer("git.kmsign.ru/royalcat/tstor/pkg/qbittorrent")
2024-08-31 23:00:13 +00:00
// 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) {
2024-09-08 18:39:11 +00:00
var c = &client{config: cfg, client: newClient(cfg.ConnectionMaxIdles, cfg.ConnectionTimeout)}
2024-08-31 23:00:13 +00:00
return c, nil
}
func LoginClient(ctx context.Context, cfg *Config) (Client, error) {
2024-09-08 18:39:11 +00:00
var c = &client{config: cfg, client: newClient(cfg.ConnectionMaxIdles, cfg.ConnectionTimeout)}
2024-08-31 23:00:13 +00:00
if err := c.Authentication().Login(ctx); err != nil {
return nil, err
}
if cfg.RefreshCookie {
go c.refreshCookie()
}
return c, nil
}
2024-09-08 18:39:11 +00:00
// 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,
}
}