mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
refactor(http): implement shared transport and clients (#1288)
* fix(http): flip to a shared transport and clients * nice threads * that is terrible * fake uri for magnet * lazy locking * why bother with r's * flip magic params to struct * refactor(http-clients): use separate clients with shared transport * refactor(http-clients): add missing license header * refactor(http-clients): defer and fix errors --------- Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
parent
2a4fb7750b
commit
3234f0d919
48 changed files with 537 additions and 391 deletions
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package btn
|
||||
|
||||
import (
|
||||
|
@ -14,9 +16,8 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAPI(t *testing.T) {
|
||||
|
@ -67,7 +68,7 @@ func TestAPI(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := NewClient(tt.fields.Url, tt.fields.APIKey)
|
||||
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
|
||||
|
||||
got, err := c.TestAPI(context.Background())
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
|
@ -166,8 +167,7 @@ func TestClient_GetTorrentByID(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
c := NewClient(tt.fields.Url, tt.fields.APIKey)
|
||||
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
|
||||
|
||||
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
|
|
|
@ -7,42 +7,61 @@ import (
|
|||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/jsonrpc"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
const DefaultURL = "https://api.broadcasthe.net/"
|
||||
|
||||
type ApiClient interface {
|
||||
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||
TestAPI(ctx context.Context) (bool, error)
|
||||
}
|
||||
|
||||
type OptFunc func(*Client)
|
||||
|
||||
func WithUrl(url string) OptFunc {
|
||||
return func(c *Client) {
|
||||
c.url = url
|
||||
}
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
rpcClient jsonrpc.Client
|
||||
Ratelimiter *rate.Limiter
|
||||
rateLimiter *rate.Limiter
|
||||
APIKey string
|
||||
url string
|
||||
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
func NewClient(url string, apiKey string) ApiClient {
|
||||
if url == "" {
|
||||
url = "https://api.broadcasthe.net/"
|
||||
}
|
||||
|
||||
func NewClient(apiKey string, opts ...OptFunc) ApiClient {
|
||||
c := &Client{
|
||||
rpcClient: jsonrpc.NewClientWithOpts(url, &jsonrpc.ClientOpts{
|
||||
Headers: map[string]string{
|
||||
"User-Agent": "autobrr",
|
||||
},
|
||||
}),
|
||||
Ratelimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
|
||||
url: DefaultURL,
|
||||
rateLimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
c.rpcClient = jsonrpc.NewClientWithOpts(c.url, &jsonrpc.ClientOpts{
|
||||
Headers: map[string]string{
|
||||
"User-Agent": "autobrr",
|
||||
},
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: time.Second * 60,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
})
|
||||
|
||||
if c.Log == nil {
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
}
|
||||
|
|
|
@ -15,38 +15,53 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
const DefaultURL = "https://gazellegames.net/api.php"
|
||||
|
||||
var ErrUnauthorized = errors.New("unauthorized: bad credentials")
|
||||
var ErrForbidden = errors.New("forbidden")
|
||||
var ErrTooManyRequests = errors.New("too many requests: rate-limit reached")
|
||||
|
||||
type ApiClient interface {
|
||||
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||
TestAPI(ctx context.Context) (bool, error)
|
||||
UseURL(url string)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
Url string
|
||||
url string
|
||||
client *http.Client
|
||||
Ratelimiter *rate.Limiter
|
||||
rateLimiter *rate.Limiter
|
||||
APIKey string
|
||||
}
|
||||
|
||||
func NewClient(apiKey string) ApiClient {
|
||||
type OptFunc func(*Client)
|
||||
|
||||
func WithUrl(url string) OptFunc {
|
||||
return func(c *Client) {
|
||||
c.url = url
|
||||
}
|
||||
}
|
||||
|
||||
func NewClient(apiKey string, opts ...OptFunc) ApiClient {
|
||||
c := &Client{
|
||||
Url: "https://gazellegames.net/api.php",
|
||||
url: DefaultURL,
|
||||
client: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
Ratelimiter: rate.NewLimiter(rate.Every(5*time.Second), 1), // 5 request every 10 seconds
|
||||
rateLimiter: rate.NewLimiter(rate.Every(5*time.Second), 1), // 5 request every 10 seconds
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
func (c *Client) UseURL(url string) {
|
||||
c.Url = url
|
||||
return c
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
|
@ -150,13 +165,13 @@ type Response struct {
|
|||
|
||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
ctx := context.Background()
|
||||
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
|
||||
err := c.rateLimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error waiting for ratelimiter")
|
||||
}
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error making request")
|
||||
return resp, errors.Wrap(err, "error making request")
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
@ -172,15 +187,15 @@ func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
|||
|
||||
res, err := c.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "ggn client request error : %s", url)
|
||||
return res, errors.Wrap(err, "ggn client request error : %s", url)
|
||||
}
|
||||
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
return res, ErrUnauthorized
|
||||
} else if res.StatusCode == http.StatusForbidden {
|
||||
return nil, nil
|
||||
return res, ErrForbidden
|
||||
} else if res.StatusCode == http.StatusTooManyRequests {
|
||||
return nil, nil
|
||||
return res, ErrTooManyRequests
|
||||
}
|
||||
|
||||
return res, nil
|
||||
|
@ -197,7 +212,7 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
|
|||
v.Add("id", torrentID)
|
||||
params := v.Encode()
|
||||
|
||||
reqUrl := fmt.Sprintf("%s?%s&%s", c.Url, "request=torrent", params)
|
||||
reqUrl := fmt.Sprintf("%s?%s&%s", c.url, "request=torrent", params)
|
||||
|
||||
resp, err := c.get(ctx, reqUrl)
|
||||
if err != nil {
|
||||
|
@ -231,7 +246,7 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
|
|||
|
||||
// TestAPI try api access against torrents page
|
||||
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
|
||||
resp, err := c.get(ctx, c.Url)
|
||||
resp, err := c.get(ctx, c.url)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "error getting data")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package ggn
|
||||
|
||||
import (
|
||||
|
@ -8,13 +10,12 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
func Test_client_GetTorrentByID(t *testing.T) {
|
||||
|
@ -32,18 +33,26 @@ func Test_client_GetTorrentByID(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
if !strings.Contains(r.RequestURI, "422368") {
|
||||
jsonPayload, _ := os.ReadFile("testdata/ggn_get_torrent_by_id_not_found.json")
|
||||
id := r.URL.Query().Get("id")
|
||||
var jsonPayload []byte
|
||||
switch id {
|
||||
case "422368":
|
||||
jsonPayload, _ = os.ReadFile("testdata/ggn_get_torrent_by_id.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
return
|
||||
break
|
||||
|
||||
case "100002":
|
||||
jsonPayload, _ = os.ReadFile("testdata/ggn_get_torrent_by_id_not_found.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
break
|
||||
}
|
||||
|
||||
// read json response
|
||||
jsonPayload, _ := os.ReadFile("testdata/ggn_get_torrent_by_id.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
//jsonPayload, _ := os.ReadFile("testdata/ggn_get_torrent_by_id.json")
|
||||
//w.Header().Set("Content-Type", "application/json")
|
||||
//w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
@ -77,7 +86,7 @@ func Test_client_GetTorrentByID(t *testing.T) {
|
|||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "get_by_id_2",
|
||||
name: "get_by_invalid_id",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: key,
|
||||
|
@ -89,9 +98,7 @@ func Test_client_GetTorrentByID(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
c := NewClient(tt.fields.APIKey)
|
||||
c.UseURL(tt.fields.Url)
|
||||
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
|
||||
|
||||
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
|
|
|
@ -166,7 +166,6 @@ func (c *rpcClient) newRequest(ctx context.Context, req interface{}) (*http.Requ
|
|||
}
|
||||
|
||||
func (c *rpcClient) doCall(ctx context.Context, request RPCRequest) (*RPCResponse, error) {
|
||||
|
||||
httpRequest, err := c.newRequest(ctx, request)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not create rpc http request")
|
||||
|
|
|
@ -71,14 +71,14 @@ func (c *client) post(ctx context.Context, endpoint string, data interface{}) (*
|
|||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "lidarr client request error: %v", reqUrl)
|
||||
return res, errors.Wrap(err, "lidarr client request error: %v", reqUrl)
|
||||
}
|
||||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("lidarr: unauthorized: bad credentials")
|
||||
return res, errors.New("lidarr: unauthorized: bad credentials")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("lidarr: bad request")
|
||||
return res, errors.New("lidarr: bad request")
|
||||
}
|
||||
|
||||
// return raw response and let the caller handle json unmarshal of body
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -42,19 +43,19 @@ type client struct {
|
|||
|
||||
// New create new lidarr client
|
||||
func New(config Config) Client {
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 120,
|
||||
Timeout: time.Second * 120,
|
||||
Transport: sharedhttp.Transport,
|
||||
}
|
||||
|
||||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
Log: config.Log,
|
||||
Log: log.New(io.Discard, "", log.LstdFlags),
|
||||
}
|
||||
|
||||
if config.Log == nil {
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
if config.Log != nil {
|
||||
c.Log = config.Log
|
||||
}
|
||||
|
||||
return c
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package lidarr
|
||||
|
||||
import (
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
const DefaultTimeout = 60
|
||||
|
@ -63,7 +64,8 @@ type Capabilities struct {
|
|||
|
||||
func NewClient(config Config) Client {
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * DefaultTimeout,
|
||||
Timeout: time.Second * DefaultTimeout,
|
||||
Transport: sharedhttp.Transport,
|
||||
}
|
||||
|
||||
if config.Timeout > 0 {
|
||||
|
@ -176,7 +178,7 @@ func (c *client) getData(ctx context.Context, endpoint string, queryParams map[s
|
|||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make request. %+v", req)
|
||||
return resp, errors.Wrap(err, "could not make request. %+v", req)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
package ops
|
||||
|
||||
import (
|
||||
|
@ -12,38 +15,49 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
const DefaultURL = "https://orpheus.network/ajax.php"
|
||||
|
||||
type ApiClient interface {
|
||||
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||
TestAPI(ctx context.Context) (bool, error)
|
||||
UseURL(url string)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
Url string
|
||||
url string
|
||||
client *http.Client
|
||||
RateLimiter *rate.Limiter
|
||||
APIKey string
|
||||
}
|
||||
|
||||
func NewClient(apiKey string) ApiClient {
|
||||
type OptFunc func(*Client)
|
||||
|
||||
func WithUrl(url string) OptFunc {
|
||||
return func(c *Client) {
|
||||
c.url = url
|
||||
}
|
||||
}
|
||||
|
||||
func NewClient(apiKey string, opts ...OptFunc) ApiClient {
|
||||
c := &Client{
|
||||
Url: "https://orpheus.network/ajax.php",
|
||||
url: DefaultURL,
|
||||
client: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
RateLimiter: rate.NewLimiter(rate.Every(10*time.Second), 5),
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
func (c *Client) UseURL(url string) {
|
||||
c.Url = url
|
||||
return c
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
|
@ -127,7 +141,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|||
}
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return resp, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
@ -147,13 +161,15 @@ func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
|||
|
||||
res, err := c.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make request: %+v", req)
|
||||
return res, errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
// return early if not OK
|
||||
if res.StatusCode != http.StatusOK {
|
||||
var r ErrorResponse
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, readErr := io.ReadAll(res.Body)
|
||||
if readErr != nil {
|
||||
return nil, errors.Wrap(readErr, "could not read body")
|
||||
|
@ -163,8 +179,6 @@ func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
|||
return nil, errors.Wrap(err, "could not unmarshal body")
|
||||
}
|
||||
|
||||
res.Body.Close()
|
||||
|
||||
return nil, errors.New("status code: %d status: %s error: %s", res.StatusCode, r.Status, r.Error)
|
||||
}
|
||||
|
||||
|
@ -182,7 +196,7 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
|
|||
v.Add("id", torrentID)
|
||||
params := v.Encode()
|
||||
|
||||
reqUrl := fmt.Sprintf("%s?action=torrent&%s", c.Url, params)
|
||||
reqUrl := fmt.Sprintf("%s?action=torrent&%s", c.url, params)
|
||||
|
||||
resp, err := c.get(ctx, reqUrl)
|
||||
if err != nil {
|
||||
|
@ -212,7 +226,7 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
|
|||
|
||||
// TestAPI try api access against torrents page
|
||||
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
|
||||
resp, err := c.get(ctx, c.Url+"?action=index")
|
||||
resp, err := c.get(ctx, c.url+"?action=index")
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "test api error")
|
||||
}
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package ops
|
||||
|
||||
import (
|
||||
|
@ -9,6 +14,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -28,18 +34,18 @@ func TestOrpheusClient_GetTorrentByID(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
if !strings.Contains(r.RequestURI, "2156788") {
|
||||
jsonPayload, _ := os.ReadFile("testdata/get_torrent_by_id_not_found.json")
|
||||
if strings.Contains(r.RequestURI, "2156788") {
|
||||
jsonPayload, _ := os.ReadFile("testdata/get_torrent_by_id.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
return
|
||||
}
|
||||
|
||||
// read json response
|
||||
jsonPayload, _ := os.ReadFile("testdata/get_torrent_by_id.json")
|
||||
jsonPayload, _ := os.ReadFile("testdata/get_torrent_by_id_not_found.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write(jsonPayload)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
@ -74,7 +80,7 @@ func TestOrpheusClient_GetTorrentByID(t *testing.T) {
|
|||
wantErr: "",
|
||||
},
|
||||
{
|
||||
name: "get_by_id_2",
|
||||
name: "invalid_torrent_id",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: key,
|
||||
|
@ -84,7 +90,7 @@ func TestOrpheusClient_GetTorrentByID(t *testing.T) {
|
|||
wantErr: "could not get torrent by id: 100002: status code: 400 status: failure error: bad id parameter",
|
||||
},
|
||||
{
|
||||
name: "get_by_id_3",
|
||||
name: "missing_api_key",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: "",
|
||||
|
@ -96,8 +102,7 @@ func TestOrpheusClient_GetTorrentByID(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := NewClient(tt.fields.APIKey)
|
||||
c.UseURL(tt.fields.Url)
|
||||
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
|
||||
|
||||
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
|
||||
if tt.wantErr != "" && assert.Error(t, err) {
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
package porla
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -12,6 +11,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/jsonrpc"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -62,20 +62,17 @@ func NewClient(cfg Config) *Client {
|
|||
c.timeout = time.Duration(cfg.Timeout) * time.Second
|
||||
}
|
||||
|
||||
c.http = &http.Client{
|
||||
Timeout: c.timeout,
|
||||
}
|
||||
|
||||
customTransport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
if cfg.TLSSkipVerify {
|
||||
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
}
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: c.timeout,
|
||||
Transport: customTransport,
|
||||
Transport: sharedhttp.Transport,
|
||||
}
|
||||
|
||||
if cfg.TLSSkipVerify {
|
||||
httpClient.Transport = sharedhttp.TransportTLSInsecure
|
||||
}
|
||||
|
||||
c.http = httpClient
|
||||
|
||||
token := cfg.AuthToken
|
||||
|
||||
if !strings.HasPrefix(token, "Bearer ") {
|
||||
|
|
|
@ -14,40 +14,55 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
const DefaultURL = "https://passthepopcorn.me/torrents.php"
|
||||
|
||||
var ErrUnauthorized = errors.New("unauthorized: bad credentials")
|
||||
var ErrForbidden = errors.New("forbidden")
|
||||
var ErrTooManyRequests = errors.New("too many requests: rate-limit reached")
|
||||
|
||||
type ApiClient interface {
|
||||
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||
TestAPI(ctx context.Context) (bool, error)
|
||||
UseURL(url string)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
Url string
|
||||
url string
|
||||
client *http.Client
|
||||
Ratelimiter *rate.Limiter
|
||||
rateLimiter *rate.Limiter
|
||||
APIUser string
|
||||
APIKey string
|
||||
}
|
||||
|
||||
func NewClient(apiUser, apiKey string) ApiClient {
|
||||
type OptFunc func(*Client)
|
||||
|
||||
func WithUrl(url string) OptFunc {
|
||||
return func(c *Client) {
|
||||
c.url = url
|
||||
}
|
||||
}
|
||||
|
||||
func NewClient(apiUser, apiKey string, opts ...OptFunc) ApiClient {
|
||||
c := &Client{
|
||||
Url: "https://passthepopcorn.me/torrents.php",
|
||||
url: DefaultURL,
|
||||
client: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
Ratelimiter: rate.NewLimiter(rate.Every(1*time.Second), 1), // 10 request every 10 seconds
|
||||
rateLimiter: rate.NewLimiter(rate.Every(1*time.Second), 1), // 10 request every 10 seconds
|
||||
APIUser: apiUser,
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
func (c *Client) UseURL(url string) {
|
||||
c.Url = url
|
||||
return c
|
||||
}
|
||||
|
||||
type TorrentResponse struct {
|
||||
|
@ -88,13 +103,13 @@ type Torrent struct {
|
|||
|
||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
ctx := context.Background()
|
||||
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
|
||||
err := c.rateLimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error waiting for ratelimiter")
|
||||
}
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error making request")
|
||||
return resp, errors.Wrap(err, "error making request")
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
@ -111,15 +126,15 @@ func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
|||
|
||||
res, err := c.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "ptp client request error : %v", url)
|
||||
return res, errors.Wrap(err, "ptp client request error : %v", url)
|
||||
}
|
||||
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
return res, ErrUnauthorized
|
||||
} else if res.StatusCode == http.StatusForbidden {
|
||||
return nil, nil
|
||||
return res, ErrForbidden
|
||||
} else if res.StatusCode == http.StatusTooManyRequests {
|
||||
return nil, nil
|
||||
return res, ErrTooManyRequests
|
||||
}
|
||||
|
||||
return res, nil
|
||||
|
@ -136,14 +151,16 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
|
|||
v.Add("torrentid", torrentID)
|
||||
params := v.Encode()
|
||||
|
||||
reqUrl := fmt.Sprintf("%v?%v", c.Url, params)
|
||||
reqUrl := fmt.Sprintf("%v?%v", c.url, params)
|
||||
|
||||
resp, err := c.get(ctx, reqUrl)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error requesting data")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
defer func() {
|
||||
resp.Body.Close()
|
||||
}()
|
||||
|
||||
body, readErr := io.ReadAll(resp.Body)
|
||||
if readErr != nil {
|
||||
|
@ -169,7 +186,7 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
|
|||
|
||||
// TestAPI try api access against torrents page
|
||||
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
|
||||
resp, err := c.get(ctx, c.Url)
|
||||
resp, err := c.get(ctx, c.url)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "error requesting data")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package ptp
|
||||
|
||||
import (
|
||||
|
@ -91,8 +93,7 @@ func TestPTPClient_GetTorrentByID(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := NewClient(tt.fields.APIUser, tt.fields.APIKey)
|
||||
c.UseURL(tt.fields.Url)
|
||||
c := NewClient(tt.fields.APIUser, tt.fields.APIKey, WithUrl(ts.URL))
|
||||
|
||||
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
|
@ -168,8 +169,7 @@ func Test(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := NewClient(tt.fields.APIUser, tt.fields.APIKey)
|
||||
c.UseURL(tt.fields.Url)
|
||||
c := NewClient(tt.fields.APIUser, tt.fields.APIKey, WithUrl(ts.URL))
|
||||
|
||||
got, err := c.TestAPI(context.Background())
|
||||
|
||||
|
|
|
@ -71,16 +71,16 @@ func (c *client) post(ctx context.Context, endpoint string, data interface{}) (*
|
|||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make request: %+v", req)
|
||||
return res, errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
return res, errors.New("unauthorized: bad credentials")
|
||||
} else if res.StatusCode == http.StatusBadRequest {
|
||||
return nil, errors.New("radarr: bad request")
|
||||
return res, errors.New("radarr: bad request")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("radarr: bad request")
|
||||
return res, errors.New("radarr: bad request")
|
||||
}
|
||||
|
||||
// return raw response and let the caller handle json unmarshal of body
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -41,19 +42,19 @@ type client struct {
|
|||
}
|
||||
|
||||
func New(config Config) Client {
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 120,
|
||||
Timeout: time.Second * 120,
|
||||
Transport: sharedhttp.Transport,
|
||||
}
|
||||
|
||||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
Log: config.Log,
|
||||
Log: log.New(io.Discard, "", log.LstdFlags),
|
||||
}
|
||||
|
||||
if config.Log == nil {
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
if config.Log != nil {
|
||||
c.Log = config.Log
|
||||
}
|
||||
|
||||
return c
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package radarr
|
||||
|
||||
import (
|
||||
|
@ -12,9 +14,8 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_client_Push(t *testing.T) {
|
||||
|
|
|
@ -76,9 +76,9 @@ func (c *client) post(ctx context.Context, endpoint string, data interface{}) (*
|
|||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
return res, errors.New("unauthorized: bad credentials")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("readarr: bad request")
|
||||
return res, errors.New("readarr: bad request")
|
||||
}
|
||||
|
||||
// return raw response and let the caller handle json unmarshal of body
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"log"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -43,20 +44,19 @@ type client struct {
|
|||
|
||||
// New create new readarr client
|
||||
func New(config Config) Client {
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 120,
|
||||
Timeout: time.Second * 120,
|
||||
Transport: sharedhttp.Transport,
|
||||
}
|
||||
|
||||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
Log: config.Log,
|
||||
Log: log.New(io.Discard, "", log.LstdFlags),
|
||||
}
|
||||
|
||||
if config.Log == nil {
|
||||
// if no provided logger then use io.Discard
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
if config.Log != nil {
|
||||
c.Log = config.Log
|
||||
}
|
||||
|
||||
return c
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package readarr
|
||||
|
||||
import (
|
||||
|
|
|
@ -15,38 +15,49 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
const DefaultURL = "https://redacted.ch/ajax.php"
|
||||
|
||||
type ApiClient interface {
|
||||
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
|
||||
TestAPI(ctx context.Context) (bool, error)
|
||||
UseURL(url string)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
Url string
|
||||
url string
|
||||
client *http.Client
|
||||
RateLimiter *rate.Limiter
|
||||
rateLimiter *rate.Limiter
|
||||
APIKey string
|
||||
}
|
||||
|
||||
func NewClient(apiKey string) ApiClient {
|
||||
type OptFunc func(*Client)
|
||||
|
||||
func WithUrl(url string) OptFunc {
|
||||
return func(c *Client) {
|
||||
c.url = url
|
||||
}
|
||||
}
|
||||
|
||||
func NewClient(apiKey string, opts ...OptFunc) ApiClient {
|
||||
c := &Client{
|
||||
Url: "https://redacted.ch/ajax.php",
|
||||
url: DefaultURL,
|
||||
client: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
RateLimiter: rate.NewLimiter(rate.Every(10*time.Second), 10),
|
||||
rateLimiter: rate.NewLimiter(rate.Every(10*time.Second), 10),
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
func (c *Client) UseURL(url string) {
|
||||
c.Url = url
|
||||
return c
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
|
@ -126,13 +137,13 @@ type Torrent struct {
|
|||
|
||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
||||
//ctx := context.Background()
|
||||
err := c.RateLimiter.Wait(req.Context()) // This is a blocking call. Honors the rate limit
|
||||
err := c.rateLimiter.Wait(req.Context()) // This is a blocking call. Honors the rate limit
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return resp, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
@ -152,7 +163,7 @@ func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
|||
|
||||
res, err := c.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make request: %+v", req)
|
||||
return res, errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
// return early if not OK
|
||||
|
@ -161,16 +172,16 @@ func (c *Client) get(ctx context.Context, url string) (*http.Response, error) {
|
|||
|
||||
body, readErr := io.ReadAll(res.Body)
|
||||
if readErr != nil {
|
||||
return nil, errors.Wrap(readErr, "could not read body")
|
||||
return res, errors.Wrap(readErr, "could not read body")
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(body, &r); err != nil {
|
||||
return nil, errors.Wrap(readErr, "could not unmarshal body")
|
||||
return res, errors.Wrap(readErr, "could not unmarshal body")
|
||||
}
|
||||
|
||||
res.Body.Close()
|
||||
|
||||
return nil, errors.New("status code: %d status: %s error: %s", res.StatusCode, r.Status, r.Error)
|
||||
return res, errors.New("status code: %d status: %s error: %s", res.StatusCode, r.Status, r.Error)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
|
@ -187,7 +198,7 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
|
|||
v.Add("id", torrentID)
|
||||
params := v.Encode()
|
||||
|
||||
reqUrl := fmt.Sprintf("%s?action=torrent&%s", c.Url, params)
|
||||
reqUrl := fmt.Sprintf("%s?action=torrent&%s", c.url, params)
|
||||
|
||||
resp, err := c.get(ctx, reqUrl)
|
||||
if err != nil {
|
||||
|
@ -215,7 +226,7 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
|
|||
|
||||
// TestAPI try api access against torrents page
|
||||
func (c *Client) TestAPI(ctx context.Context) (bool, error) {
|
||||
resp, err := c.get(ctx, c.Url+"?action=index")
|
||||
resp, err := c.get(ctx, c.url+"?action=index")
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "test api error")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package red
|
||||
|
||||
import (
|
||||
|
@ -12,6 +14,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -98,8 +101,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := NewClient(tt.fields.APIKey)
|
||||
c.UseURL(tt.fields.Url)
|
||||
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
|
||||
|
||||
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
|
||||
if tt.wantErr != "" && assert.Error(t, err) {
|
||||
|
|
|
@ -12,6 +12,8 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
|
@ -23,7 +25,7 @@ type Client struct {
|
|||
|
||||
log *log.Logger
|
||||
|
||||
Http *http.Client
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
|
@ -43,8 +45,9 @@ func New(opts Options) *Client {
|
|||
basicUser: opts.BasicUser,
|
||||
basicPass: opts.BasicPass,
|
||||
log: log.New(io.Discard, "", log.LstdFlags),
|
||||
Http: &http.Client{
|
||||
Timeout: time.Second * 60,
|
||||
http: &http.Client{
|
||||
Timeout: time.Second * 60,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -88,7 +91,7 @@ func (c *Client) AddFromUrl(ctx context.Context, r AddNzbRequest) (*AddFileRespo
|
|||
req.SetBasicAuth(c.basicUser, c.basicPass)
|
||||
}
|
||||
|
||||
res, err := c.Http.Do(req)
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -137,7 +140,7 @@ func (c *Client) Version(ctx context.Context) (*VersionResponse, error) {
|
|||
req.SetBasicAuth(c.basicUser, c.basicPass)
|
||||
}
|
||||
|
||||
res, err := c.Http.Do(req)
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
84
pkg/sharedhttp/http.go
Normal file
84
pkg/sharedhttp/http.go
Normal file
|
@ -0,0 +1,84 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
package sharedhttp
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Transport = &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second, // default transport value
|
||||
KeepAlive: 30 * time.Second, // default transport value
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: true, // default is true; since HTTP/2 multiplexes a single TCP connection. we'd want to use HTTP/1, which would use multiple TCP connections.
|
||||
MaxIdleConns: 100, // default transport value
|
||||
MaxIdleConnsPerHost: 10, // default is 2, so we want to increase the number to use establish more connections.
|
||||
IdleConnTimeout: 90 * time.Second, // default transport value
|
||||
TLSHandshakeTimeout: 10 * time.Second, // default transport value
|
||||
ExpectContinueTimeout: 1 * time.Second, // default transport value
|
||||
ReadBufferSize: 65536,
|
||||
WriteBufferSize: 65536,
|
||||
TLSClientConfig: &tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
},
|
||||
}
|
||||
|
||||
var TransportTLSInsecure = &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second, // default transport value
|
||||
KeepAlive: 30 * time.Second, // default transport value
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: true, // default is true; since HTTP/2 multiplexes a single TCP connection. we'd want to use HTTP/1, which would use multiple TCP connections.
|
||||
MaxIdleConns: 100, // default transport value
|
||||
MaxIdleConnsPerHost: 10, // default is 2, so we want to increase the number to use establish more connections.
|
||||
IdleConnTimeout: 90 * time.Second, // default transport value
|
||||
TLSHandshakeTimeout: 10 * time.Second, // default transport value
|
||||
ExpectContinueTimeout: 1 * time.Second, // default transport value
|
||||
ReadBufferSize: 65536,
|
||||
WriteBufferSize: 65536,
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
var Client = &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
Transport: Transport,
|
||||
}
|
||||
|
||||
type MagnetRoundTripper struct{}
|
||||
|
||||
func (rt *MagnetRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
if r.URL.Scheme == "magnet" {
|
||||
responseBody := r.URL.String()
|
||||
respReader := io.NopCloser(strings.NewReader(responseBody))
|
||||
|
||||
resp := &http.Response{
|
||||
Status: http.StatusText(http.StatusOK),
|
||||
StatusCode: http.StatusOK,
|
||||
Body: respReader,
|
||||
ContentLength: int64(len(responseBody)),
|
||||
Header: map[string][]string{
|
||||
"Content-Type": {"text/plain"},
|
||||
"Location": {responseBody},
|
||||
},
|
||||
Proto: "HTTP/2.0",
|
||||
ProtoMajor: 2,
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
return Transport.RoundTrip(r)
|
||||
}
|
||||
|
||||
var MagnetTransport = &MagnetRoundTripper{}
|
|
@ -71,14 +71,14 @@ func (c *client) post(ctx context.Context, endpoint string, data interface{}) (*
|
|||
|
||||
res, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not make request: %+v", req)
|
||||
return res, errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
// validate response
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
return nil, errors.New("unauthorized: bad credentials")
|
||||
return res, errors.New("unauthorized: bad credentials")
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("sonarr: bad request")
|
||||
return res, errors.New("sonarr: bad request")
|
||||
}
|
||||
|
||||
// return raw response and let the caller handle json unmarshal of body
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"log"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -43,20 +44,19 @@ type client struct {
|
|||
|
||||
// New create new sonarr client
|
||||
func New(config Config) Client {
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 120,
|
||||
Timeout: time.Second * 120,
|
||||
Transport: sharedhttp.Transport,
|
||||
}
|
||||
|
||||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
Log: config.Log,
|
||||
Log: log.New(io.Discard, "", log.LstdFlags),
|
||||
}
|
||||
|
||||
if config.Log == nil {
|
||||
// if no provided logger then use io.Discard
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
if config.Log != nil {
|
||||
c.Log = config.Log
|
||||
}
|
||||
|
||||
return c
|
||||
|
|
|
@ -1,26 +1,27 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package sonarr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_client_Push(t *testing.T) {
|
||||
// disable logger
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
log.SetOutput(ioutil.Discard)
|
||||
log.SetOutput(io.Discard)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
ts := httptest.NewServer(mux)
|
||||
|
@ -125,7 +126,7 @@ func Test_client_Push(t *testing.T) {
|
|||
func Test_client_Test(t *testing.T) {
|
||||
// disable logger
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
log.SetOutput(ioutil.Discard)
|
||||
log.SetOutput(io.Discard)
|
||||
|
||||
key := "mock-key"
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
|
@ -61,7 +62,8 @@ type Capabilities struct {
|
|||
|
||||
func NewClient(config Config) Client {
|
||||
httpClient := &http.Client{
|
||||
Timeout: config.Timeout,
|
||||
Timeout: config.Timeout,
|
||||
Transport: sharedhttp.Transport,
|
||||
}
|
||||
|
||||
c := &client{
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package torznab
|
||||
|
||||
import (
|
||||
|
@ -51,7 +53,7 @@ import (
|
|||
// {
|
||||
// name: "get feed",
|
||||
// fields: fields{
|
||||
// Host: srv.URL + "/api",
|
||||
// Host: srv.url + "/api",
|
||||
// ApiKey: key,
|
||||
// BasicAuth: BasicAuth{},
|
||||
// },
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/hekmon/transmissionrpc/v3"
|
||||
)
|
||||
|
||||
|
@ -43,7 +45,7 @@ type customTransport struct {
|
|||
}
|
||||
|
||||
func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
dt := http.DefaultTransport.(*http.Transport).Clone()
|
||||
dt := sharedhttp.Transport
|
||||
if t.TLSSkipVerify {
|
||||
dt.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
goversion "github.com/hashicorp/go-version"
|
||||
)
|
||||
|
@ -72,6 +73,8 @@ type Checker struct {
|
|||
Owner string
|
||||
Repo string
|
||||
CurrentVersion string
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewChecker(owner, repo, currentVersion string) *Checker {
|
||||
|
@ -79,6 +82,10 @@ func NewChecker(owner, repo, currentVersion string) *Checker {
|
|||
Owner: owner,
|
||||
Repo: repo,
|
||||
CurrentVersion: currentVersion,
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,12 +100,11 @@ func (c *Checker) get(ctx context.Context) (*Release, error) {
|
|||
req.Header.Set("Accept", "application/vnd.github.v3+json")
|
||||
req.Header.Set("User-Agent", c.buildUserAgent())
|
||||
|
||||
client := http.DefaultClient
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -40,19 +41,19 @@ type client struct {
|
|||
}
|
||||
|
||||
func New(config Config) Client {
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 120,
|
||||
Timeout: time.Second * 120,
|
||||
Transport: sharedhttp.Transport,
|
||||
}
|
||||
|
||||
c := &client{
|
||||
config: config,
|
||||
http: httpClient,
|
||||
Log: config.Log,
|
||||
Log: log.New(io.Discard, "", log.LstdFlags),
|
||||
}
|
||||
|
||||
if config.Log == nil {
|
||||
c.Log = log.New(io.Discard, "", log.LstdFlags)
|
||||
if config.Log != nil {
|
||||
c.Log = config.Log
|
||||
}
|
||||
|
||||
return c
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue