fixes here and there
This commit is contained in:
@@ -2,7 +2,7 @@ package dav
|
||||
|
||||
func Directory(path string) Response {
|
||||
return Response{
|
||||
Href: customPathEscape(path),
|
||||
Href: "/" + customPathEscape(path),
|
||||
Propstat: PropStat{
|
||||
Prop: Prop{
|
||||
ResourceType: ResourceType{Value: "<d:collection/>"},
|
||||
@@ -14,7 +14,7 @@ func Directory(path string) Response {
|
||||
|
||||
func File(path string, fileSize int64, added string, link string) Response {
|
||||
return Response{
|
||||
Href: customPathEscape(path),
|
||||
Href: "/" + customPathEscape(path),
|
||||
Propstat: PropStat{
|
||||
Prop: Prop{
|
||||
ContentLength: fileSize,
|
||||
|
||||
@@ -1,19 +1,32 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type HTTPClient struct {
|
||||
Client *http.Client
|
||||
MaxRetries int
|
||||
Backoff func(attempt int) time.Duration
|
||||
CheckRespStatus func(resp *http.Response, err error) bool
|
||||
CheckRespStatus func(resp *http.Response, err error, log *zap.SugaredLogger) bool
|
||||
BearerToken string
|
||||
log *zap.SugaredLogger
|
||||
config config.ConfigInterface
|
||||
}
|
||||
|
||||
func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
if r.config != nil && strings.Contains(req.Host, "download.real-debrid.") {
|
||||
if host := r.config.GetRandomPreferredHost(); host != "" {
|
||||
req.Host = r.config.GetRandomPreferredHost()
|
||||
}
|
||||
}
|
||||
if r.BearerToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+r.BearerToken)
|
||||
}
|
||||
@@ -21,7 +34,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
var err error
|
||||
for attempt := 0; attempt < r.MaxRetries; attempt++ {
|
||||
resp, err = r.Client.Do(req)
|
||||
if !r.CheckRespStatus(resp, err) {
|
||||
if !r.CheckRespStatus(resp, err, r.log) {
|
||||
return resp, err
|
||||
}
|
||||
time.Sleep(r.Backoff(attempt))
|
||||
@@ -29,7 +42,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func NewHTTPClient(token string, maxRetries int) *HTTPClient {
|
||||
func NewHTTPClient(token string, maxRetries int, c config.ConfigInterface) *HTTPClient {
|
||||
return &HTTPClient{
|
||||
BearerToken: token,
|
||||
Client: &http.Client{},
|
||||
@@ -37,15 +50,21 @@ func NewHTTPClient(token string, maxRetries int) *HTTPClient {
|
||||
Backoff: func(attempt int) time.Duration {
|
||||
return time.Duration(attempt) * time.Second
|
||||
},
|
||||
CheckRespStatus: func(resp *http.Response, err error) bool {
|
||||
CheckRespStatus: func(resp *http.Response, err error, log *zap.SugaredLogger) bool {
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
body2, _ := io.ReadAll(resp.Request.Body)
|
||||
log.Errorf("Received a %s %s from %s", resp.Status, string(body), resp.Request.URL)
|
||||
log.Errorf("request %s %s", string(body2), resp.Request.URL)
|
||||
return true
|
||||
}
|
||||
// no need to retry because the status code is 2XX
|
||||
return false
|
||||
},
|
||||
log: logutil.NewLogger().Named("http"),
|
||||
config: c,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -19,9 +20,9 @@ type RealDebrid struct {
|
||||
client *zurghttp.HTTPClient
|
||||
}
|
||||
|
||||
func NewRealDebrid(accessToken string, log *zap.SugaredLogger) *RealDebrid {
|
||||
func NewRealDebrid(accessToken string, config config.ConfigInterface, log *zap.SugaredLogger) *RealDebrid {
|
||||
maxRetries := 10
|
||||
client := zurghttp.NewHTTPClient(accessToken, maxRetries)
|
||||
client := zurghttp.NewHTTPClient(accessToken, maxRetries, nil)
|
||||
return &RealDebrid{
|
||||
log: log,
|
||||
client: client,
|
||||
@@ -52,11 +53,6 @@ func (rd *RealDebrid) UnrestrictCheck(link string) (*UnrestrictResponse, error)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from unrestrict check endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response UnrestrictResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
@@ -101,11 +97,7 @@ func (rd *RealDebrid) GetTorrents(customLimit int) ([]Torrent, int, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// if status code is not 2xx, return error
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from get torrents endpoint", resp.Status)
|
||||
return nil, 0, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
// if status code is not 2xx, return erro
|
||||
|
||||
var torrents []Torrent
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
@@ -154,11 +146,6 @@ func (rd *RealDebrid) GetTorrentInfo(id string) (*TorrentInfo, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from get info endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response TorrentInfo
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
@@ -191,11 +178,6 @@ func (rd *RealDebrid) SelectTorrentFiles(id string, files string) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from select files endpoint", resp.Status)
|
||||
return fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
rd.log.Debugf("Selected files %s for torrent id=%s", len(strings.Split(files, ",")), id)
|
||||
return nil
|
||||
}
|
||||
@@ -218,11 +200,6 @@ func (rd *RealDebrid) DeleteTorrent(id string) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from delete torrent endpoint", resp.Status)
|
||||
return fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
rd.log.Debugf("Deleted torrent with id=%s", id)
|
||||
return nil
|
||||
}
|
||||
@@ -251,11 +228,6 @@ func (rd *RealDebrid) AddMagnetHash(magnet string) (*MagnetResponse, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from add magnet endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response MagnetResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&response)
|
||||
if err != nil {
|
||||
@@ -285,11 +257,6 @@ func (rd *RealDebrid) GetActiveTorrentCount() (*ActiveTorrentCountResponse, erro
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from active torrents endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response ActiveTorrentCountResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&response)
|
||||
if err != nil {
|
||||
@@ -324,11 +291,6 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*UnrestrictResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from unrestrict link endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response UnrestrictResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
@@ -340,6 +302,6 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*UnrestrictResponse, error) {
|
||||
return nil, fmt.Errorf("can't fetch first byte")
|
||||
}
|
||||
|
||||
rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
|
||||
// rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user