Add support for using freeleech tokens if available

This commit is contained in:
Daniel Mason 2025-07-11 10:12:43 +12:00
parent 74f777340e
commit 1242c19883
Signed by: idanoo
GPG key ID: 387387CDBC02F132
21 changed files with 88 additions and 34 deletions

View file

@ -32,12 +32,12 @@ func (c *Client) TestAPI(ctx context.Context) (bool, error) {
return false, nil
}
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("btn client: must have torrentID")
}
res, err := c.rpcClient.CallCtx(ctx, "getTorrentById", [2]string{c.APIKey, torrentID})
res, err := c.rpcClient.CallCtx(ctx, "getTorrentById", [3]interface{}{c.APIKey, torrentID, freeleechToken})
if err != nil {
return nil, errors.Wrap(err, "call getTorrentById failed")
}

View file

@ -20,7 +20,7 @@ import (
const DefaultURL = "https://api.broadcasthe.net/"
type ApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}

View file

@ -27,7 +27,7 @@ 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)
GetTorrentByID(ctx context.Context, torrentID string, freelechToken bool) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
@ -243,7 +243,7 @@ func (c *Client) getJSON(ctx context.Context, params url.Values, data any) error
return nil
}
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("ggn client: must have torrentID")
}
@ -253,6 +253,9 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
params := url.Values{}
params.Add("request", "torrent")
params.Add("id", torrentID)
if freeleechToken {
params.Add("usetoken", "1")
}
err := c.getJSON(ctx, params, &response)
if err != nil {

View file

@ -86,7 +86,7 @@ func Test_client_GetTorrentByID(t *testing.T) {
Url: ts.URL,
APIKey: key,
},
args: args{torrentID: "422368"},
args: args{torrentID: "422368", freeleechToken: false},
want: &domain.TorrentBasic{
Id: "422368",
InfoHash: "78DA2811E6732012B8224198D4DC2FD49A5E950F",
@ -100,7 +100,7 @@ func Test_client_GetTorrentByID(t *testing.T) {
Url: ts.URL,
APIKey: key,
},
args: args{torrentID: "100002"},
args: args{torrentID: "100002", freeleechToken: true},
want: nil,
wantErr: false,
},
@ -109,7 +109,7 @@ func Test_client_GetTorrentByID(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID, tt.args.freeleechToken)
if tt.wantErr && assert.Error(t, err) {
t.Logf("got err: %v", err)
assert.Equal(t, tt.wantErr, err)

View file

@ -23,7 +23,7 @@ import (
const DefaultURL = "https://orpheus.network/ajax.php"
type ApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
@ -226,7 +226,7 @@ func (c *Client) getJSON(ctx context.Context, params url.Values, data any) error
return nil
}
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("orpheus client: must have torrentID")
}
@ -236,6 +236,9 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
params := url.Values{}
params.Add("action", "torrent")
params.Add("id", torrentID)
if freeleechToken {
params.Add("usetoken", "1")
}
err := c.getJSON(ctx, params, &response)
if err != nil {

View file

@ -70,7 +70,7 @@ func TestOrpheusClient_GetTorrentByID(t *testing.T) {
Url: ts.URL,
APIKey: key,
},
args: args{torrentID: "2156788"},
args: args{torrentID: "2156788", freeleechToken: false},
want: &domain.TorrentBasic{
Id: "2156788",
InfoHash: "",
@ -86,7 +86,7 @@ func TestOrpheusClient_GetTorrentByID(t *testing.T) {
Url: ts.URL,
APIKey: key,
},
args: args{torrentID: "100002"},
args: args{torrentID: "100002", freeleechToken: false},
want: nil,
wantErr: "could not get torrent by id: 100002: status code: 400 status: failure error: bad id parameter",
},
@ -96,16 +96,26 @@ func TestOrpheusClient_GetTorrentByID(t *testing.T) {
Url: ts.URL,
APIKey: "",
},
args: args{torrentID: "100002"},
args: args{torrentID: "100002", freeleechToken: false},
want: nil,
wantErr: "could not get torrent by id: 100002: orpheus client missing API key!",
},
{
name: "get_by_id_1_freeleech_token",
fields: fields{
Url: ts.URL,
APIKey: "",
},
args: args{torrentID: "100002", freeleechToken: true},
want: nil,
wantErr: "could not get torrent by id: 1. freeleech token not supported or no tokens available",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID, tt.args.freeleechToken)
if tt.wantErr != "" && assert.Error(t, err) {
assert.EqualErrorf(t, err, tt.wantErr, "Error should be: %v, got: %v", tt.wantErr, err)
}

View file

@ -26,7 +26,7 @@ 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)
GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
@ -180,7 +180,7 @@ func (c *Client) getJSON(ctx context.Context, params url.Values, data any) error
return nil
}
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("ptp client: must have torrentID")
}
@ -189,6 +189,9 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
params := url.Values{}
params.Add("torrentid", torrentID)
if freeleechToken {
params.Add("usetoken", "1")
}
err := c.getJSON(ctx, params, &response)
if err != nil {

View file

@ -72,7 +72,7 @@ func TestPTPClient_GetTorrentByID(t *testing.T) {
APIUser: user,
APIKey: key,
},
args: args{torrentID: "1"},
args: args{torrentID: "1", freeleechToken: false},
want: &domain.TorrentBasic{
Id: "1",
InfoHash: "F57AA86DFB03F87FCC7636E310D35918442EAE5C",
@ -87,7 +87,7 @@ func TestPTPClient_GetTorrentByID(t *testing.T) {
APIUser: user,
APIKey: key,
},
args: args{torrentID: "100002"},
args: args{torrentID: "100002", freeleechToken: false},
want: nil,
wantErr: true,
},
@ -96,7 +96,7 @@ func TestPTPClient_GetTorrentByID(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.APIUser, tt.fields.APIKey, WithUrl(ts.URL))
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID, tt.args.freeleechToken)
if tt.wantErr {
assert.Error(t, err)
} else {

View file

@ -23,7 +23,7 @@ import (
const DefaultURL = "https://redacted.sh/ajax.php"
type ApiClient interface {
GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error)
GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error)
TestAPI(ctx context.Context) (bool, error)
}
@ -214,7 +214,7 @@ func (c *Client) getJSON(ctx context.Context, params url.Values, data any) error
return nil
}
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.TorrentBasic, error) {
func (c *Client) GetTorrentByID(ctx context.Context, torrentID string, freeleechToken bool) (*domain.TorrentBasic, error) {
if torrentID == "" {
return nil, errors.New("red client: must have torrentID")
}
@ -224,6 +224,9 @@ func (c *Client) GetTorrentByID(ctx context.Context, torrentID string) (*domain.
params := url.Values{}
params.Add("action", "torrent")
params.Add("id", torrentID)
if freeleechToken {
params.Add("usetoken", "1")
}
err := c.getJSON(ctx, params, &response)
if err != nil {

View file

@ -70,7 +70,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
Url: ts.URL,
APIKey: key,
},
args: args{torrentID: "29991962"},
args: args{torrentID: "29991962", freeleechToken: false},
want: &domain.TorrentBasic{
Id: "29991962",
InfoHash: "B2BABD3A361EAFC6C4E9142C422DF7DDF5D7E163",
@ -86,7 +86,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
Url: ts.URL,
APIKey: key,
},
args: args{torrentID: "100002"},
args: args{torrentID: "100002", freeleechToken: false},
want: nil,
wantErr: "could not get torrent by id: 100002: status code: 400 status: failure error: bad id parameter",
},
@ -96,7 +96,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
Url: ts.URL,
APIKey: "",
},
args: args{torrentID: "100002"},
args: args{torrentID: "100002", freeleechToken: false},
want: nil,
wantErr: "could not get torrent by id: 100002: RED client missing API key!",
},
@ -105,7 +105,7 @@ func TestREDClient_GetTorrentByID(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID, tt.args.freeleechToken)
if tt.wantErr != "" && assert.Error(t, err) {
assert.EqualErrorf(t, err, tt.wantErr, "Error should be: %v, got: %v", tt.wantErr, err)
}