Browse Source

Untangle try-catch blocks for version checking

pull/523/head
Phillip Dykman 5 years ago
parent
commit
5cf18931e1
  1. 51
      app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java

51
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 // 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 // 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 { try {
String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion", new BasicNameValuePair("username", settings.getUsername()), String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion");
new BasicNameValuePair("password", settings.getPassword()));
apiVersion = Float.parseFloat(apiVerText.trim()); apiVersion = Float.parseFloat(apiVerText.trim());
} catch (DaemonException | NumberFormatException e) { } catch (DaemonException | NumberFormatException e) {
if (http_response_code == 403) { is_v2 = http_response_code == 403;
try { }
ensureAuthenticated(log);
String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion"); // Keep trying
apiVersion = Float.parseFloat(apiVerText.trim()); if (is_v2) {
} catch (DaemonException | NumberFormatException e2) { // Preemptive assumption, for authentication
apiVersion = (float) 2.3; // assume this is new API since we are forbidden to access API apiVersion = (float) 2.3;
}
} else { // Authenticate, and try v2 again
try { try {
String apiVerText = makeRequest(log, "/version/api"); ensureAuthenticated(log);
apiVersion = Float.parseFloat(apiVerText.trim()); String apiVerText = makeRequest(log, "/api/v2/app/webapiVersion");
} catch (DaemonException | NumberFormatException e3) { apiVersion = Float.parseFloat(apiVerText.trim());
apiVersion = 1; } 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); 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 // The qBittorent version is only supported since 3.2; for earlier versions we parse the about dialog and parse it

Loading…
Cancel
Save