chore(transmission): cleanup component code

This commit is contained in:
Bastien Wirtz
2025-09-27 15:20:57 +02:00
parent 35e49e3d91
commit 92a79ffdfb
2 changed files with 28 additions and 39 deletions

View File

@@ -42,7 +42,6 @@ ln -s AGENTS.md CLAUDE.md
ln -s AGENTS.md GEMINI.md ln -s AGENTS.md GEMINI.md
``` ```
### How to submit a contribution ### How to submit a contribution
The general process to submit a contribution is as follow: The general process to submit a contribution is as follow:

View File

@@ -82,27 +82,14 @@ export default {
/** /**
* Makes a request to Transmission RPC API with proper session handling * Makes a request to Transmission RPC API with proper session handling
* @param {string} method - The RPC method to call * @param {string} method - The RPC method to call
* @param {Object} requestArgs - Arguments for the RPC method
* @returns {Promise<Object>} RPC response * @returns {Promise<Object>} RPC response
*/ */
transmissionRequest: async function (method, requestArgs = {}) { transmissionRequest: async function (method) {
const requestData = { const options = this.getRequestHeaders(method);
method: method,
arguments: requestArgs,
};
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestData),
};
// Add HTTP Basic Auth if credentials are provided // Add HTTP Basic Auth if credentials are provided
if (this.item.auth) { if (this.item.auth) {
const credentials = btoa(this.item.auth); options.headers["Authorization"] = `Basic ${btoa(this.item.auth)}`;
options.headers["Authorization"] = `Basic ${credentials}`;
} }
// Add session ID header if we have one // Add session ID header if we have one
@@ -115,21 +102,16 @@ export default {
} catch (error) { } catch (error) {
// Handle Transmission's 409 session requirement // Handle Transmission's 409 session requirement
if (error.message.includes("409")) { if (error.message.includes("409")) {
// Make a direct request to get session ID const sessionOptions = this.getRequestHeaders("session-get");
let url = this.endpoint;
if (url && !url.endsWith("/")) {
url += "/";
}
url += "transmission/rpc";
const sessionResponse = await fetch(url, { const sessionResponse = this.fetch(
method: "POST", "transmission/rpc",
headers: { "Content-Type": "application/json" }, sessionOptions,
body: JSON.stringify({ method: "session-get" }), );
}); if (error.message.includes("409")) {
this.sessionId = sessionResponse.headers.get(
if (sessionResponse.status === 409) { "X-Transmission-Session-Id",
this.sessionId = sessionResponse.headers.get("X-Transmission-Session-Id"); );
if (this.sessionId) { if (this.sessionId) {
options.headers["X-Transmission-Session-Id"] = this.sessionId; options.headers["X-Transmission-Session-Id"] = this.sessionId;
return await this.fetch("transmission/rpc", options); return await this.fetch("transmission/rpc", options);
@@ -140,22 +122,30 @@ export default {
throw error; throw error;
} }
}, },
getRequestHeaders: function (method) {
return {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ method }),
};
},
getStats: async function () { getStats: async function () {
try { try {
// Get session stats for transfer rates and torrent count // Get session stats for transfer rates and torrent count
const statsResponse = await this.transmissionRequest("session-stats"); const statsResponse = await this.transmissionRequest("session-stats");
if (statsResponse?.result !== "success") {
if (statsResponse && statsResponse.result === "success") {
const stats = statsResponse.arguments;
this.dl = stats.downloadSpeed ?? 0;
this.ul = stats.uploadSpeed ?? 0;
this.count = stats.activeTorrentCount ?? 0;
this.error = false;
} else {
throw new Error( throw new Error(
`Transmission RPC failed: ${statsResponse?.result || "Unknown error"}`, `Transmission RPC failed: ${statsResponse?.result || "Unknown error"}`,
); );
} }
const stats = statsResponse.arguments;
this.dl = stats.downloadSpeed ?? 0;
this.ul = stats.uploadSpeed ?? 0;
this.count = stats.activeTorrentCount ?? 0;
this.error = false;
} catch (e) { } catch (e) {
this.error = true; this.error = true;
console.error("Transmission service error:", e); console.error("Transmission service error:", e);