Skip to content

Commit

Permalink
fix qBittorrent retry context handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Feb 8, 2024
1 parent 289f639 commit ac034d5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
11 changes: 5 additions & 6 deletions integrations/qbittorrent/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ func New(ctx context.Context, host, username, password string) *API {
}
var version string
api.Wait(ctx)
if version, err = api.Version(); err != nil {
if version, err = api.Version(ctx); err != nil {
log.Fatal().Msgf("failed to connect to qBittorrent: %s", err)
}
log.Info().Msgf("connected to qBittorrent:%s", version)
return api
}

func (api *API) Do(req *http.Request) (*http.Response, error) {
ctx := req.Context()
func (api *API) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
localReq := req.Clone(ctx)
resp, err := api.client.Do(localReq)
switch {
Expand All @@ -53,12 +52,12 @@ func (api *API) Do(req *http.Request) (*http.Response, error) {
errors.Is(err, syscall.ECONNRESET):
log.Warn().Msgf("qBittorrent disconnected")
api.Wait(ctx)
return api.Do(req)
return api.Do(ctx, req)
case err == nil && resp.StatusCode >= 400:
if loginErr := api.Login(api.username, api.password); loginErr != nil {
if loginErr := api.Login(ctx, api.username, api.password); loginErr != nil {
return resp, loginErr
}
return api.Do(req)
return api.Do(ctx, req)
}
return resp, err
}
5 changes: 3 additions & 2 deletions integrations/qbittorrent/auth_login.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package qbittorrent

import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
)

func (api *API) Login(username, password string) error {
func (api *API) Login(ctx context.Context, username, password string) error {
var path = api.host + "/auth/login"
data := url.Values{
"username": []string{username},
Expand All @@ -18,7 +19,7 @@ func (api *API) Login(username, password string) error {
return fmt.Errorf("login request creation failed: %w", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := api.Do(req)
resp, err := api.Do(ctx, req)
if err != nil {
return fmt.Errorf("login failed: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions integrations/qbittorrent/torrent_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ func (api *API) AddTorrent(ctx context.Context, args ...ArgAddTorrent) error {
for _, f := range args {
f.ApplyAddTorrent(formdata)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path, &b)
req, err := http.NewRequest(http.MethodPost, path, &b)
if err != nil {
return fmt.Errorf("creating request failed: %w", err)
}
req.Header.Set("Content-Type", formdata.FormDataContentType())
resp, err := api.Do(req)
resp, err := api.Do(ctx, req)
if err != nil {
return fmt.Errorf("post torrents/add failed: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions integrations/qbittorrent/torrent_add_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (api *API) AddTorrentTags(ctx context.Context, hashes []string, args ...Add
for _, f := range args {
f.ApplyAddTorrentTags(values)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path, strings.NewReader(values.Encode()))
req, err := http.NewRequest(http.MethodPost, path, strings.NewReader(values.Encode()))
if err != nil {
return fmt.Errorf("list request failed: %w", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := api.Do(req)
resp, err := api.Do(ctx, req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions integrations/qbittorrent/torrent_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c Category) ApplyListTorrent(v url.Values) {

func (api *API) List(ctx context.Context, args ...ArgListTorrent) ([]Torrent, error) {
var path = api.host + "/torrents/info"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
req, err := http.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, fmt.Errorf("list request failed: %w", err)
}
Expand All @@ -34,7 +34,7 @@ func (api *API) List(ctx context.Context, args ...ArgListTorrent) ([]Torrent, er
f.ApplyListTorrent(values)
}
req.URL.RawQuery = values.Encode()
resp, err := api.Do(req)
resp, err := api.Do(ctx, req)
if err != nil {
return nil, fmt.Errorf("could not list torrents: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions integrations/qbittorrent/torrent_remove_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func (api *API) RemoveTorrentTags(ctx context.Context, hashes []string, args ...
for _, f := range args {
f.ApplyAddTorrentTags(values)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path, strings.NewReader(values.Encode()))
req, err := http.NewRequest(http.MethodPost, path, strings.NewReader(values.Encode()))
if err != nil {
return fmt.Errorf("list request failed: %w", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := api.Do(req)
resp, err := api.Do(ctx, req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions integrations/qbittorrent/version.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package qbittorrent

import (
"context"
"io"
"net/http"

"github.com/sonalys/animeman/internal/utils"
)

func (api *API) Version() (string, error) {
func (api *API) Version(ctx context.Context) (string, error) {
var path = api.host + "/app/version"
resp, err := api.Do(utils.Must(http.NewRequest(http.MethodGet, path, nil)))
resp, err := api.Do(ctx, utils.Must(http.NewRequest(http.MethodGet, path, nil)))
if err != nil {
return "", NewErrConnection(err)
}
Expand Down

0 comments on commit ac034d5

Please sign in to comment.