@@ -82,27 +82,14 @@ export default {
8282 /**
8383 * Makes a request to Transmission RPC API with proper session handling
8484 * @param {string} method - The RPC method to call
85- * @param {Object} requestArgs - Arguments for the RPC method
8685 * @returns {Promise<Object>} RPC response
8786 */
88- transmissionRequest : async function (method , requestArgs = {}) {
89- const requestData = {
90- method: method,
91- arguments: requestArgs,
92- };
93-
94- const options = {
95- method: " POST" ,
96- headers: {
97- " Content-Type" : " application/json" ,
98- },
99- body: JSON .stringify (requestData),
100- };
87+ transmissionRequest : async function (method ) {
88+ const options = this .getRequestHeaders (method);
10189
10290 // Add HTTP Basic Auth if credentials are provided
10391 if (this .item .auth ) {
104- const credentials = btoa (this .item .auth );
105- options .headers [" Authorization" ] = ` Basic ${ credentials} ` ;
92+ options .headers [" Authorization" ] = ` Basic ${ btoa (this .item .auth )} ` ;
10693 }
10794
10895 // Add session ID header if we have one
@@ -115,21 +102,16 @@ export default {
115102 } catch (error) {
116103 // Handle Transmission's 409 session requirement
117104 if (error .message .includes (" 409" )) {
118- // Make a direct request to get session ID
119- let url = this .endpoint ;
120- if (url && ! url .endsWith (" /" )) {
121- url += " /" ;
122- }
123- url += " transmission/rpc" ;
124-
125- const sessionResponse = await fetch (url, {
126- method: " POST" ,
127- headers: { " Content-Type" : " application/json" },
128- body: JSON .stringify ({ method: " session-get" }),
129- });
105+ const sessionOptions = this .getRequestHeaders (" session-get" );
130106
131- if (sessionResponse .status === 409 ) {
132- this .sessionId = sessionResponse .headers .get (" X-Transmission-Session-Id" );
107+ const sessionResponse = this .fetch (
108+ " transmission/rpc" ,
109+ sessionOptions,
110+ );
111+ if (error .message .includes (" 409" )) {
112+ this .sessionId = sessionResponse .headers .get (
113+ " X-Transmission-Session-Id" ,
114+ );
133115 if (this .sessionId ) {
134116 options .headers [" X-Transmission-Session-Id" ] = this .sessionId ;
135117 return await this .fetch (" transmission/rpc" , options);
@@ -140,22 +122,30 @@ export default {
140122 throw error;
141123 }
142124 },
125+ getRequestHeaders : function (method ) {
126+ return {
127+ method: " POST" ,
128+ headers: {
129+ " Content-Type" : " application/json" ,
130+ },
131+ body: JSON .stringify ({ method }),
132+ };
133+ },
143134 getStats : async function () {
144135 try {
145136 // Get session stats for transfer rates and torrent count
146137 const statsResponse = await this .transmissionRequest (" session-stats" );
147-
148- if (statsResponse && statsResponse .result === " success" ) {
149- const stats = statsResponse .arguments ;
150- this .dl = stats .downloadSpeed ?? 0 ;
151- this .ul = stats .uploadSpeed ?? 0 ;
152- this .count = stats .activeTorrentCount ?? 0 ;
153- this .error = false ;
154- } else {
138+ if (statsResponse? .result !== " success" ) {
155139 throw new Error (
156140 ` Transmission RPC failed: ${ statsResponse? .result || " Unknown error" }` ,
157141 );
158142 }
143+
144+ const stats = statsResponse.arguments;
145+ this.dl = stats.downloadSpeed ?? 0;
146+ this.ul = stats.uploadSpeed ?? 0;
147+ this.count = stats.activeTorrentCount ?? 0;
148+ this.error = false;
159149 } catch (e) {
160150 this.error = true;
161151 console.error("Transmission service error:", e);
0 commit comments