Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(qBittorrent): API Version state handling #826

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions server/services/qBittorrent/clientGatewayService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ class QBittorrentClientGatewayService extends ClientGatewayService {
throw new Error();
}

const method = isApiVersionAtLeast(await this.clientRequestManager.apiVersion, '2.11.0') ? 'stopped' : 'paused';
await this.clientRequestManager
.torrentsAddFiles(fileBuffers, {
savepath: destination,
tags: tags.join(','),
[isApiVersionAtLeast(this.clientRequestManager.apiVersion, '2.11.0') ? 'stopped' : 'paused']: !start,
[method]: !start,
root_folder: !isBasePath,
contentLayout: isBasePath ? 'NoSubfolder' : undefined,
sequentialDownload: isSequential,
Expand Down Expand Up @@ -119,11 +120,12 @@ class QBittorrentClientGatewayService extends ClientGatewayService {
throw new Error();
}

const method = isApiVersionAtLeast(await this.clientRequestManager.apiVersion, '2.11.0') ? 'stopped' : 'paused';
await this.clientRequestManager
.torrentsAddURLs(urls, {
savepath: destination,
tags: tags.join(','),
[isApiVersionAtLeast(this.clientRequestManager.apiVersion, '2.11.0') ? 'stopped' : 'paused']: !start,
[method]: !start,
root_folder: !isBasePath,
contentLayout: isBasePath ? 'NoSubfolder' : undefined,
sequentialDownload: isSequential,
Expand Down Expand Up @@ -526,7 +528,7 @@ class QBittorrentClientGatewayService extends ClientGatewayService {

async testGateway(): Promise<void> {
return this.clientRequestManager
.updateAuthCookie()
.updateConnection()
.then(() => this.processClientRequestSuccess(undefined), this.processClientRequestError);
}
}
Expand Down
40 changes: 23 additions & 17 deletions server/services/qBittorrent/clientRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class ClientRequestManager {
private connectionSettings: QBittorrentConnectionSettings;
private apiBase: string;
apiVersion: string | null = null;
apiVersion: Promise<string | undefined> = Promise.resolve(undefined);
trim21 marked this conversation as resolved.
Show resolved Hide resolved
private authCookie: Promise<string | undefined> = Promise.resolve(undefined);
private isMainDataPending = false;

Expand Down Expand Up @@ -80,17 +80,27 @@
});
}

async updateAuthCookie(connectionSettings?: QBittorrentConnectionSettings): Promise<void> {
let authFailed = false;
async updateConnection(connectionSettings?: QBittorrentConnectionSettings): Promise<void> {
let failed = false;

this.authCookie = this.authenticate(connectionSettings).catch(() => {
authFailed = true;
failed = true;
return undefined;
});

this.apiVersion = this.authCookie
.then(() => {
return !failed ? this.getApiVersion() : Promise.resolve(undefined);
})
.catch(() => {
failed = true;
return undefined;

Check warning on line 97 in server/services/qBittorrent/clientRequestManager.ts

View check run for this annotation

Codecov / codecov/patch

server/services/qBittorrent/clientRequestManager.ts#L96-L97

Added lines #L96 - L97 were not covered by tests
});

await this.authCookie;
await this.apiVersion;

if (authFailed) {
if (failed) {
throw new Error();
}
}
Expand Down Expand Up @@ -120,15 +130,12 @@
});
}

async getApiVersion(): Promise<void> {
try {
const {data} = await axios.get(`${this.apiBase}/app/webapiVersion`, {
async getApiVersion(): Promise<string> {
return axios
.get<string>(`${this.apiBase}/app/webapiVersion`, {
headers: await this.getRequestHeaders(),
});
this.apiVersion = data;
} catch (error) {
this.apiVersion = null;
}
})
.then((res) => res.data);
}

async getTorrentInfos(): Promise<QBittorrentTorrentInfos> {
Expand Down Expand Up @@ -306,7 +313,7 @@
}

async torrentsPause(hashes: Array<string>): Promise<void> {
const method = isApiVersionAtLeast(this.apiVersion, '2.11.0') ? 'stop' : 'pause';
const method = isApiVersionAtLeast(await this.apiVersion, '2.11.0') ? 'stop' : 'pause';

Check warning on line 316 in server/services/qBittorrent/clientRequestManager.ts

View check run for this annotation

Codecov / codecov/patch

server/services/qBittorrent/clientRequestManager.ts#L316

Added line #L316 was not covered by tests
return axios
.post(
`${this.apiBase}/torrents/${method}`,
Expand All @@ -323,7 +330,7 @@
}

async torrentsResume(hashes: Array<string>): Promise<void> {
const method = isApiVersionAtLeast(this.apiVersion, '2.11.0') ? 'start' : 'resume';
const method = isApiVersionAtLeast(await this.apiVersion, '2.11.0') ? 'start' : 'resume';

Check warning on line 333 in server/services/qBittorrent/clientRequestManager.ts

View check run for this annotation

Codecov / codecov/patch

server/services/qBittorrent/clientRequestManager.ts#L333

Added line #L333 was not covered by tests
return axios
.post(
`${this.apiBase}/torrents/${method}`,
Expand Down Expand Up @@ -622,8 +629,7 @@
constructor(connectionSettings: QBittorrentConnectionSettings) {
this.connectionSettings = connectionSettings;
this.apiBase = `${connectionSettings.url}/api/v2`;
this.updateAuthCookie().catch(() => undefined);
this.getApiVersion();
this.updateConnection().catch(() => undefined);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/services/qBittorrent/util/apiVersionCheck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function isApiVersionAtLeast(currentVersion: string | null, targetVersion: string): boolean {
export function isApiVersionAtLeast(currentVersion: string | undefined, targetVersion: string): boolean {
if (!currentVersion) {
return false;
}
Expand Down
Loading