Skip to content

Commit

Permalink
Merge pull request #54 from ckcr4lyf/feature/v5-support
Browse files Browse the repository at this point in the history
v5 support
  • Loading branch information
ckcr4lyf authored Dec 25, 2024
2 parents 95e7934 + f0e9699 commit 8d1b9f0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
6 changes: 6 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Development

These are just some notes for my personal reference for when I'm doing local testing and stuff.

API docs: https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)

7 changes: 5 additions & 2 deletions __tests__/qbittorrent/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ test('loginV2', async t => {
password: 'adminadmin'
}

const scope = nock(fakeSettings.url).post('/api/v2/auth/login', {
const scope = nock(fakeSettings.url);

scope.post('/api/v2/auth/login', {
username: 'admin',
password: 'adminadmin'
}).reply(200, {}, {
'set-cookie': 'SID=1234'
});


scope.get('/api/v2/app/version').reply(200, 'v4');
const api = await loginV2(fakeSettings);

//@ts-ignore - client is private but we want to access for testing
Expand Down
21 changes: 19 additions & 2 deletions build/src/qbittorrent/api.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion build/src/qbittorrent/auth.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qbit-race",
"version": "2.0.0-alpha.18",
"version": "2.0.0-alpha.19",
"description": "Qbit utilities for racing",
"main": "./bin/index.js",
"type": "module",
Expand Down
23 changes: 21 additions & 2 deletions src/qbittorrent/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getLoggerV3 } from '../utils/logger.js';
export class QbittorrentApi {

private client: AxiosInstance;
private version: string;

constructor(public basePath: string, public cookie: string) {

Expand All @@ -19,6 +20,19 @@ export class QbittorrentApi {
}
});

this.version = 'v4'; // default to v4
}

async getAndSetVersion(): Promise<string> {
try {
const response = await this.client.get(ApiEndpoints.version);

console.log(response.data);
this.version = response.data;
return response.data;
} catch (e){
throw new Error(`Failed to get qBittorrent version. Error: ${e}`);
}
}

async getTorrents(hashes?: string[]): Promise<QbittorrentTorrent[]> {
Expand Down Expand Up @@ -96,10 +110,11 @@ export class QbittorrentApi {

async resumeTorrents(torrents: ApiCompatibleTorrent[]){
const infohashes = torrents.map(torrent => torrent.hash);
const endpoint = this.version >= 'v5' ? ApiEndpoints.resumeTorrentsNew : ApiEndpoints.resumeTorrents;
const payload = `hashes=${infohashes.join('|')}`;

try {
await this.client.post(ApiEndpoints.resumeTorrents, payload, {
await this.client.post(endpoint, payload, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
Expand All @@ -114,9 +129,10 @@ export class QbittorrentApi {
return;
}

const endpoint = this.version >= 'v5' ? ApiEndpoints.pauseTorrentsNew : ApiEndpoints.pauseTorrents;
const payload = `hashes=${torrents.map(torrent => torrent.hash).join('|')}`

await this.client.post(ApiEndpoints.pauseTorrents, payload, {
await this.client.post(endpoint, payload, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
Expand Down Expand Up @@ -178,13 +194,16 @@ enum ApiEndpoints {
torrentsInfo = '/api/v2/torrents/info',
torrentTrackers = '/api/v2/torrents/trackers',
resumeTorrents = '/api/v2/torrents/resume',
resumeTorrentsNew = '/api/v2/torrents/start',
addTags = '/api/v2/torrents/addTags',
setCategory = '/api/v2/torrents/setCategory',
pauseTorrents = '/api/v2/torrents/pause',
pauseTorrentsNew = '/api/v2/torrents/stop',
addTorrent = '/api/v2/torrents/add',
deleteTorrents = '/api/v2/torrents/delete',
reannounce = '/api/v2/torrents/reannounce',
transferInfo = '/api/v2/transfer/info',
version = '/api/v2/app/version',
}

export const login = (qbittorrentSettings: QBITTORRENT_SETTINGS): Promise<AxiosResponse> => {
Expand Down
8 changes: 7 additions & 1 deletion src/qbittorrent/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ export const loginV2 = async (qbittorrentSettings: QBITTORRENT_SETTINGS): Promis
throw new Error(`Failed to authenticate (UNKNOWN ERROR)`);
}

return new QbittorrentApi(qbittorrentSettings.url, response.headers['set-cookie'][0]);
const api = new QbittorrentApi(qbittorrentSettings.url, response.headers['set-cookie'][0]);

// Need to get the version so we can choose which endpoints to use
// See: https://github.com/ckcr4lyf/qbit-race/issues/52
const version = await api.getAndSetVersion();
logger.info(`Detected qBitorrent version as: ${version}`);
return api;
}

0 comments on commit 8d1b9f0

Please sign in to comment.