Multi-token support

This commit is contained in:
Ben Adrian Sarmiento
2024-06-28 04:47:43 +02:00
parent 5e06f04f33
commit 962845fb81
15 changed files with 214 additions and 108 deletions

View File

@@ -12,6 +12,7 @@ import (
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"
"github.com/debridmediamanager/zurg/pkg/logutil"
@@ -27,7 +28,7 @@ type HTTPClient struct {
timeoutSecs int
rateLimitSleepSecs int
backoff func(attempt int) time.Duration
bearerToken string
token atomic.Value
dnsCache cmap.ConcurrentMap[string, string]
hosts []string
log *logutil.Logger
@@ -52,7 +53,6 @@ func (e *DownloadErrorResponse) Error() string {
}
func NewHTTPClient(
token string,
maxRetries int,
timeoutSecs int,
forceIPv6 bool,
@@ -61,7 +61,6 @@ func NewHTTPClient(
log *logutil.Logger,
) *HTTPClient {
client := HTTPClient{
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
@@ -128,9 +127,14 @@ func NewHTTPClient(
return &client
}
func (r *HTTPClient) SetToken(token string) {
r.token.Store(token)
}
func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
if r.bearerToken != "" {
req.Header.Set("Authorization", "Bearer "+r.bearerToken)
token := r.token.Load()
if token != nil && token.(string) != "" {
req.Header.Set("Authorization", "Bearer "+token.(string))
}
var resp *http.Response
@@ -321,8 +325,8 @@ func backoffFunc(attempt int) time.Duration {
return time.Duration(backoff) * time.Second
}
func (r *HTTPClient) VerifyURL(url string) error {
req, err := http.NewRequest(http.MethodHead, url, nil)
func (r *HTTPClient) VerifyLink(link string) error {
req, err := http.NewRequest(http.MethodHead, link, nil)
if err != nil {
return err
}