From 600de44787ebc0d8488622b549f2b657652a5e6b Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Wed, 29 Nov 2023 09:34:00 +0100 Subject: [PATCH] threat forbidden as an error --- main.go | 3 +++ qbittorent.go | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/main.go b/main.go index b850368..4213ddc 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,9 @@ func main() { torrents, err := qcli.List() if err != nil { log.Printf("[QBit] List Error: %v", err) + if err.Error() == "forbidden" { + log.Fatal("Need to authenticate again. Exiting ...") + } } if len(torrents) > 0 { log.Printf("[QBit] Found %d torrents to sync", len(torrents)) diff --git a/qbittorent.go b/qbittorent.go index 33b6c67..d8397b7 100644 --- a/qbittorent.go +++ b/qbittorent.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "net/http" "strings" "github.com/go-resty/resty/v2" @@ -34,6 +35,12 @@ type Torrent struct { func NewQBittorrentCli(options *QBitTorrentOptions) (*QBittorrentCli, error) { r := resty.New().SetBaseURL(fmt.Sprintf("%s/api/v2", options.Uri)) + r.OnAfterResponse(func(c *resty.Client, r *resty.Response) error { + if r.StatusCode() == http.StatusForbidden { + return errors.New("forbidden") + } + return nil + }) result, err := r. R().