From 5cf18931e16f436b3f3f7d957178e4211421f547 Mon Sep 17 00:00:00 2001 From: Phillip Dykman Date: Fri, 3 Jan 2020 23:27:32 -0800 Subject: [PATCH] Untangle try-catch blocks for version checking --- .../Qbittorrent/QbittorrentAdapter.java | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java b/app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java index e0a50ad7..b23c1c60 100644 --- a/app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java +++ b/app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java @@ -89,28 +89,39 @@ public class QbittorrentAdapter implements IDaemonAdapter { // Since 4.2.0, old API is dropped. Fallback to old one if the new one failed for version <4.2.0 // The API version is only supported since qBittorrent 3.2, so otherwise we assume version 1 + boolean is_v2 = false; + + // First, try the v2 api version endpoint try { - String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion", new BasicNameValuePair("username", settings.getUsername()), - new BasicNameValuePair("password", settings.getPassword())); + String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion"); apiVersion = Float.parseFloat(apiVerText.trim()); - } catch (DaemonException | NumberFormatException e) { - if (http_response_code == 403) { - try { - ensureAuthenticated(log); - String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion"); - apiVersion = Float.parseFloat(apiVerText.trim()); - } catch (DaemonException | NumberFormatException e2) { - apiVersion = (float) 2.3; // assume this is new API since we are forbidden to access API - } - } else { - try { - String apiVerText = makeRequest(log, "/version/api"); - apiVersion = Float.parseFloat(apiVerText.trim()); - } catch (DaemonException | NumberFormatException e3) { - apiVersion = 1; - } - } - } + } catch (DaemonException | NumberFormatException e) { + is_v2 = http_response_code == 403; + } + + // Keep trying + if (is_v2) { + // Preemptive assumption, for authentication + apiVersion = (float) 2.3; + + // Authenticate, and try v2 again + try { + ensureAuthenticated(log); + String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion"); + apiVersion = Float.parseFloat(apiVerText.trim()); + } catch (DaemonException | NumberFormatException e) { + apiVersion = (float) 2.3; // assume this is new API since we are forbidden to access API + } + } else { + // Fall back to old api + try { + String apiVerText = makeRequest(log, "/version/api"); + apiVersion = Float.parseFloat(apiVerText.trim()); + } catch (DaemonException | NumberFormatException e) { + apiVersion = 1; + } + } + log.d(LOG_NAME, "qBittorrent API version is " + apiVersion); // The qBittorent version is only supported since 3.2; for earlier versions we parse the about dialog and parse it