diff --git a/internal/action/qbittorrent.go b/internal/action/qbittorrent.go index b091800..6b1f304 100644 --- a/internal/action/qbittorrent.go +++ b/internal/action/qbittorrent.go @@ -30,41 +30,35 @@ func (s *service) qbittorrent(action domain.Action, release domain.Release) ([]s return nil, errors.New("could not find client by id: %v", action.ClientID) } - qbt, exists := s.qbitClients[qbitKey{client.ID, client.Name}] - if !exists { - qbtSettings := qbittorrent.Settings{ - Name: client.Name, - Hostname: client.Host, - Port: uint(client.Port), - Username: client.Username, - Password: client.Password, - TLS: client.TLS, - TLSSkipVerify: client.TLSSkipVerify, - } + qbtSettings := qbittorrent.Settings{ + Name: client.Name, + Hostname: client.Host, + Port: uint(client.Port), + Username: client.Username, + Password: client.Password, + TLS: client.TLS, + TLSSkipVerify: client.TLSSkipVerify, + } - // setup sub logger adapter which is compatible with *log.Logger - qbtSettings.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "qBittorrent").Str("client", client.Name).Logger(), zerolog.TraceLevel) + // setup sub logger adapter which is compatible with *log.Logger + qbtSettings.Log = zstdlog.NewStdLoggerWithLevel(s.log.With().Str("type", "qBittorrent").Str("client", client.Name).Logger(), zerolog.TraceLevel) - // only set basic auth if enabled - if client.Settings.Basic.Auth { - qbtSettings.BasicAuth = client.Settings.Basic.Auth - qbtSettings.Basic.Username = client.Settings.Basic.Username - qbtSettings.Basic.Password = client.Settings.Basic.Password - } + // only set basic auth if enabled + if client.Settings.Basic.Auth { + qbtSettings.BasicAuth = client.Settings.Basic.Auth + qbtSettings.Basic.Username = client.Settings.Basic.Username + qbtSettings.Basic.Password = client.Settings.Basic.Password + } - qbt = qbittorrent.NewClient(qbtSettings) - - s.qbitClients[qbitKey{client.ID, client.Name}] = qbt + qbt := qbittorrent.NewClient(qbtSettings) + // only login if we have a password + if qbtSettings.Password != "" { if err = qbt.Login(); err != nil { return nil, errors.Wrap(err, "could not log into client: %v at %v", client.Name, client.Host) } } - if qbt == nil { - return nil, errors.New("qbit client does not exist") - } - rejections, err := s.qbittorrentCheckRulesCanDownload(action, client, qbt) if err != nil { return nil, errors.Wrap(err, "error checking client rules: %v", action.Name) @@ -157,7 +151,7 @@ func (s *service) prepareQbitOptions(action domain.Action, m Macro) (map[string] return options, nil } -func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client *domain.DownloadClient, qbt qbittorrent.Client) ([]string, error) { +func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client *domain.DownloadClient, qbt *qbittorrent.Client) ([]string, error) { s.log.Trace().Msgf("action qBittorrent: %v check rules", action.Name) // check for active downloads and other rules @@ -202,7 +196,7 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client return nil, nil } -func (s *service) reannounceTorrent(qb qbittorrent.Client, action domain.Action, hash string) error { +func (s *service) reannounceTorrent(qb *qbittorrent.Client, action domain.Action, hash string) error { announceOK := false attempts := 0 diff --git a/pkg/qbittorrent/client.go b/pkg/qbittorrent/client.go index 575fbb0..9404dcc 100644 --- a/pkg/qbittorrent/client.go +++ b/pkg/qbittorrent/client.go @@ -20,19 +20,6 @@ import ( "github.com/autobrr/autobrr/pkg/errors" ) -type Client interface { - Login() error - GetTorrents() ([]Torrent, error) - GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) - GetTorrentsActiveDownloads() ([]Torrent, error) - GetTorrentsRaw() (string, error) - GetTorrentTrackers(hash string) ([]TorrentTracker, error) - AddTorrentFromFile(file string, options map[string]string) error - DeleteTorrents(hashes []string, deleteFiles bool) error - ReAnnounceTorrents(hashes []string) error - GetTransferInfo() (*TransferInfo, error) -} - var ( backoffSchedule = []time.Duration{ 5 * time.Second, @@ -42,7 +29,7 @@ var ( timeout = 60 * time.Second ) -type client struct { +type Client struct { Name string settings Settings http *http.Client @@ -69,8 +56,8 @@ type Basic struct { Password string } -func NewClient(settings Settings) Client { - c := &client{ +func NewClient(settings Settings) *Client { + c := &Client{ settings: settings, Name: settings.Name, log: log.New(io.Discard, "", log.LstdFlags), @@ -110,7 +97,7 @@ func NewClient(settings Settings) Client { return c } -func (c *client) get(endpoint string, opts map[string]string) (*http.Response, error) { +func (c *Client) get(endpoint string, opts map[string]string) (*http.Response, error) { var err error var resp *http.Response @@ -146,7 +133,7 @@ func (c *client) get(endpoint string, opts map[string]string) (*http.Response, e return resp, nil } -func (c *client) post(endpoint string, opts map[string]string) (*http.Response, error) { +func (c *Client) post(endpoint string, opts map[string]string) (*http.Response, error) { // add optional parameters that the user wants form := url.Values{} if opts != nil { @@ -193,7 +180,7 @@ func (c *client) post(endpoint string, opts map[string]string) (*http.Response, return resp, nil } -func (c *client) postBasic(endpoint string, opts map[string]string) (*http.Response, error) { +func (c *Client) postBasic(endpoint string, opts map[string]string) (*http.Response, error) { // add optional parameters that the user wants form := url.Values{} if opts != nil { @@ -227,7 +214,7 @@ func (c *client) postBasic(endpoint string, opts map[string]string) (*http.Respo return resp, nil } -func (c *client) postFile(endpoint string, fileName string, opts map[string]string) (*http.Response, error) { +func (c *Client) postFile(endpoint string, fileName string, opts map[string]string) (*http.Response, error) { var err error var resp *http.Response @@ -308,7 +295,7 @@ func (c *client) postFile(endpoint string, fileName string, opts map[string]stri return resp, nil } -func (c *client) setCookies(cookies []*http.Cookie) { +func (c *Client) setCookies(cookies []*http.Cookie) { cookieURL, _ := url.Parse(buildUrl(c.settings, "")) c.http.Jar.SetCookies(cookieURL, cookies) diff --git a/pkg/qbittorrent/methods.go b/pkg/qbittorrent/methods.go index c37bdad..2f22746 100644 --- a/pkg/qbittorrent/methods.go +++ b/pkg/qbittorrent/methods.go @@ -12,7 +12,7 @@ import ( ) // Login https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#authentication -func (c *client) Login() error { +func (c *Client) Login() error { opts := map[string]string{ "username": c.settings.Username, "password": c.settings.Password, @@ -55,7 +55,7 @@ func (c *client) Login() error { return nil } -func (c *client) GetTorrents() ([]Torrent, error) { +func (c *Client) GetTorrents() ([]Torrent, error) { resp, err := c.get("torrents/info", nil) if err != nil { @@ -78,7 +78,7 @@ func (c *client) GetTorrents() ([]Torrent, error) { return torrents, nil } -func (c *client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) { +func (c *Client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) { opts := map[string]string{ "filter": string(filter), } @@ -104,7 +104,7 @@ func (c *client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) { return torrents, nil } -func (c *client) GetTorrentsActiveDownloads() ([]Torrent, error) { +func (c *Client) GetTorrentsActiveDownloads() ([]Torrent, error) { var filter = TorrentFilterDownloading opts := map[string]string{ @@ -141,7 +141,7 @@ func (c *client) GetTorrentsActiveDownloads() ([]Torrent, error) { return res, nil } -func (c *client) GetTorrentsRaw() (string, error) { +func (c *Client) GetTorrentsRaw() (string, error) { resp, err := c.get("torrents/info", nil) if err != nil { return "", errors.Wrap(err, "could not get torrents raw") @@ -157,7 +157,7 @@ func (c *client) GetTorrentsRaw() (string, error) { return string(data), nil } -func (c *client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) { +func (c *Client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) { opts := map[string]string{ "hash": hash, } @@ -200,7 +200,7 @@ func (c *client) GetTorrentTrackers(hash string) ([]TorrentTracker, error) { } // AddTorrentFromFile add new torrent from torrent file -func (c *client) AddTorrentFromFile(file string, options map[string]string) error { +func (c *Client) AddTorrentFromFile(file string, options map[string]string) error { res, err := c.postFile("torrents/add", file, options) if err != nil { @@ -214,7 +214,7 @@ func (c *client) AddTorrentFromFile(file string, options map[string]string) erro return nil } -func (c *client) DeleteTorrents(hashes []string, deleteFiles bool) error { +func (c *Client) DeleteTorrents(hashes []string, deleteFiles bool) error { // Add hashes together with | separator hv := strings.Join(hashes, "|") @@ -235,7 +235,7 @@ func (c *client) DeleteTorrents(hashes []string, deleteFiles bool) error { return nil } -func (c *client) ReAnnounceTorrents(hashes []string) error { +func (c *Client) ReAnnounceTorrents(hashes []string) error { // Add hashes together with | separator hv := strings.Join(hashes, "|") opts := map[string]string{ @@ -254,7 +254,7 @@ func (c *client) ReAnnounceTorrents(hashes []string) error { return nil } -func (c *client) GetTransferInfo() (*TransferInfo, error) { +func (c *Client) GetTransferInfo() (*TransferInfo, error) { resp, err := c.get("transfer/info", nil) if err != nil { return nil, errors.Wrap(err, "could not get transfer info")