fix(qBittorrent): api version state handling (#826)

This commit is contained in:
Alex Thomson
2024-11-25 18:37:11 +13:00
committed by GitHub
parent 843b9c966c
commit 80825db785
3 changed files with 30 additions and 22 deletions

View File

@@ -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,
@@ -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,
@@ -526,7 +528,7 @@ class QBittorrentClientGatewayService extends ClientGatewayService {
async testGateway(): Promise<void> {
return this.clientRequestManager
.updateAuthCookie()
.updateConnection()
.then(() => this.processClientRequestSuccess(undefined), this.processClientRequestError);
}
}

View File

@@ -36,7 +36,7 @@ const EMPTY_SERVER_STATE = {
class ClientRequestManager {
private connectionSettings: QBittorrentConnectionSettings;
private apiBase: string;
apiVersion: string | null = null;
apiVersion: Promise<string | undefined> = Promise.resolve(undefined);
private authCookie: Promise<string | undefined> = Promise.resolve(undefined);
private isMainDataPending = false;
@@ -80,17 +80,27 @@ class ClientRequestManager {
});
}
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;
});
await this.authCookie;
this.apiVersion = this.authCookie
.then(() => {
return !failed ? this.getApiVersion() : Promise.resolve(undefined);
})
.catch(() => {
failed = true;
return undefined;
});
if (authFailed) {
await this.authCookie;
await this.apiVersion;
if (failed) {
throw new Error();
}
}
@@ -120,15 +130,12 @@ class ClientRequestManager {
});
}
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> {
@@ -306,7 +313,7 @@ class ClientRequestManager {
}
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';
return axios
.post(
`${this.apiBase}/torrents/${method}`,
@@ -323,7 +330,7 @@ class ClientRequestManager {
}
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';
return axios
.post(
`${this.apiBase}/torrents/${method}`,
@@ -622,8 +629,7 @@ class ClientRequestManager {
constructor(connectionSettings: QBittorrentConnectionSettings) {
this.connectionSettings = connectionSettings;
this.apiBase = `${connectionSettings.url}/api/v2`;
this.updateAuthCookie().catch(() => undefined);
this.getApiVersion();
this.updateConnection().catch(() => undefined);
}
}

View File

@@ -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;
}