1
0
Fork 0
hats/pkg/qbittorrent/api.go

88 lines
2.0 KiB
Go
Raw Permalink Normal View History

2023-11-10 23:22:46 +00:00
package qbittorrent
import (
"fmt"
"net/http"
"strings"
2023-11-28 00:47:07 +00:00
"time"
2023-11-10 23:22:46 +00:00
"code.jhot.me/jhot/hats/internal/util"
"github.com/go-resty/resty/v2"
)
type QbittorrentClient struct {
restClient *resty.Client
}
func New(host string) *QbittorrentClient {
return &QbittorrentClient{
2023-11-28 00:47:07 +00:00
restClient: resty.New().SetTimeout(30 * time.Second).SetBaseURL(fmt.Sprintf("%s/api/v2", host)),
2023-11-10 23:22:46 +00:00
}
}
func (c *QbittorrentClient) Login(user string, pass string) error {
resp, err := util.CheckSuccess(c.restClient.R().SetFormData(map[string]string{
"username": user,
"password": pass,
}).Post("auth/login"))
if err != nil {
return err
}
authCookie := resp.Header().Get("set-cookie")
if authCookie == "" {
2023-11-27 23:47:50 +00:00
return fmt.Errorf("auth cookie not found.\nResponse: %s\nHeaders: %+v", resp.Body(), resp.Header())
2023-11-10 23:22:46 +00:00
}
first := strings.Split(authCookie, ";")[0]
parts := strings.Split(first, "=")
if len(parts) != 2 {
return fmt.Errorf("auth cookie not in expected format: %s", authCookie)
}
c.restClient.SetCookie(&http.Cookie{
Name: parts[0],
Value: parts[1],
})
return nil
}
2023-11-13 21:54:07 +00:00
func (c *QbittorrentClient) Logout() error {
_, err := util.CheckSuccess(c.restClient.R().Get("auth/logout"))
c.restClient.SetCookies([]*http.Cookie{})
return err
}
2023-11-10 23:22:46 +00:00
func (c *QbittorrentClient) GetVersion() (string, error) {
resp, err := util.CheckSuccess(c.restClient.R().Get("app/version"))
if err != nil {
return "", err
}
return string(resp.Body()), nil
}
func (c *QbittorrentClient) GetTransferInfo() (GlobalTransferInfo, error) {
var data GlobalTransferInfo
_, err := util.CheckSuccess(c.restClient.R().SetResult(&data).Get("transfer/info"))
return data, err
}
func (c *QbittorrentClient) GetAltSpeedLimitState() (bool, error) {
resp, err := util.CheckSuccess(c.restClient.R().Get("transfer/speedLimitsMode"))
if err != nil {
return false, err
}
return string(resp.Body()) == "1", nil
}
func (c *QbittorrentClient) ToggleAltSpeedLimitState() error {
_, err := util.CheckSuccess(c.restClient.R().Post("transfer/toggleSpeedLimitsMode"))
return err
}