mirror of
https://github.com/idanoo/autobrr
synced 2025-07-22 16:29:12 +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,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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue