]> git.ktnx.net Git - mobile-ledger.git/commitdiff
API version-dependent transaction sending
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 28 Dec 2020 09:39:23 +0000 (11:39 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 28 Dec 2020 09:39:23 +0000 (11:39 +0200)
app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java
app/src/main/java/net/ktnx/mobileledger/json/Gateway.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/json/v1_14/Gateway.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/json/v1_15/Gateway.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/Gateway.java [new file with mode: 0644]

index dc0c24875469a74b5d6404097a21034d1690672a..14d41f9f14a9bffeef548d94b264db5382628b20 100644 (file)
@@ -20,9 +20,8 @@ package net.ktnx.mobileledger.async;
 import android.os.AsyncTask;
 import android.util.Log;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.ObjectWriter;
-
+import net.ktnx.mobileledger.json.API;
+import net.ktnx.mobileledger.json.Gateway;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
@@ -74,35 +73,14 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         mProfile = profile;
         simulate = false;
     }
-    private boolean send_1_15_OK() throws IOException {
-        HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
-        http.setRequestMethod("PUT");
-        http.setRequestProperty("Content-Type", "application/json");
-        http.setRequestProperty("Accept", "*/*");
-
-        net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction jsonTransaction =
-                net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.fromLedgerTransaction(
-                        transaction);
-        ObjectMapper mapper = new ObjectMapper();
-        ObjectWriter writer =
-                mapper.writerFor(net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.class);
-        String body = writer.writeValueAsString(jsonTransaction);
-
-        return sendRequest(http, body);
-    }
-    private boolean send_1_14_OK() throws IOException {
+    private boolean sendOK(API apiVersion) throws IOException {
         HttpURLConnection http = NetworkUtil.prepareConnection(mProfile, "add");
         http.setRequestMethod("PUT");
         http.setRequestProperty("Content-Type", "application/json");
         http.setRequestProperty("Accept", "*/*");
 
-        net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction jsonTransaction =
-                net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.fromLedgerTransaction(
-                        transaction);
-        ObjectMapper mapper = new ObjectMapper();
-        ObjectWriter writer =
-                mapper.writerFor(net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.class);
-        String body = writer.writeValueAsString(jsonTransaction);
+        Gateway gateway = Gateway.forApiVersion(apiVersion);
+        String body = gateway.transactionSaveRequest(transaction);
 
         return sendRequest(http, body);
     }
@@ -155,7 +133,7 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
                             Logger.debug("network", line);
                             count++;
                         }
-                        return false; // will cause a retry with the legacy method
+                        return false; // will cause a retry with another method
                     }
                     default:
                         BufferedReader reader = new BufferedReader(new InputStreamReader(resp));
@@ -269,36 +247,34 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
         try {
             transaction = ledgerTransactions[0];
 
-            switch (mProfile.getApiVersion()) {
+            final API profileApiVersion = mProfile.getApiVersion();
+            switch (profileApiVersion) {
                 case auto:
-                    Logger.debug("network", "Trying version 1.5.");
-                    if (!send_1_15_OK()) {
-                        Logger.debug("network", "Version 1.5 request failed. Trying with 1.14");
-                        if (!send_1_14_OK()) {
-                            Logger.debug("network",
-                                    "Version 1.14 failed too. Trying HTML form emulation");
-                            legacySendOkWithRetry();
-                        }
-                        else {
-                            Logger.debug("network", "Version 1.14 request succeeded");
+                    boolean sendOK = false;
+                    for (API ver : API.allVersions) {
+                        Logger.debug("network", "Trying version " + ver);
+                        if (sendOK(ver)) {
+                            sendOK = true;
+                            Logger.debug("network", "Version " + ver + " request succeeded");
+
+                            break;
                         }
                     }
-                    else {
-                        Logger.debug("network", "Version 1.15 request succeeded");
+                    if (!sendOK) {
+                        Logger.debug("network", "Trying HTML form emulation");
+                        legacySendOkWithRetry();
                     }
                     break;
                 case html:
                     legacySendOkWithRetry();
                     break;
                 case v1_14:
-                    send_1_14_OK();
-                    break;
                 case v1_15:
-                    send_1_15_OK();
+                case v1_19_1:
+                    sendOK(profileApiVersion);
                     break;
                 default:
-                    throw new IllegalStateException(
-                            "Unexpected API version: " + mProfile.getApiVersion());
+                    throw new IllegalStateException("Unexpected API version: " + profileApiVersion);
             }
         }
         catch (Exception e) {
diff --git a/app/src/main/java/net/ktnx/mobileledger/json/Gateway.java b/app/src/main/java/net/ktnx/mobileledger/json/Gateway.java
new file mode 100644 (file)
index 0000000..9778967
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright © 2020 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.json;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+import net.ktnx.mobileledger.model.LedgerTransaction;
+
+abstract public class Gateway {
+    public static Gateway forApiVersion(API apiVersion) {
+        switch (apiVersion) {
+            case v1_14:
+                return new net.ktnx.mobileledger.json.v1_14.Gateway();
+            case v1_15:
+                return new net.ktnx.mobileledger.json.v1_15.Gateway();
+            case v1_19_1:
+                return new net.ktnx.mobileledger.json.v1_19_1.Gateway();
+            default:
+                throw new RuntimeException("Unsupported JSON API version " + apiVersion);
+        }
+    }
+    public abstract String transactionSaveRequest(LedgerTransaction ledgerTransaction)
+            throws JsonProcessingException;
+}
diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_14/Gateway.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/Gateway.java
new file mode 100644 (file)
index 0000000..25712af
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2020 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.json.v1_14;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+
+import net.ktnx.mobileledger.model.LedgerTransaction;
+
+public class Gateway extends net.ktnx.mobileledger.json.Gateway {
+    @Override
+    public String transactionSaveRequest(LedgerTransaction ledgerTransaction)
+            throws JsonProcessingException {
+        net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction jsonTransaction =
+                net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.fromLedgerTransaction(
+                        ledgerTransaction);
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectWriter writer =
+                mapper.writerFor(net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.class);
+        return writer.writeValueAsString(jsonTransaction);
+    }
+}
diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/Gateway.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/Gateway.java
new file mode 100644 (file)
index 0000000..a4a1268
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2020 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.json.v1_15;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+
+import net.ktnx.mobileledger.model.LedgerTransaction;
+
+public class Gateway extends net.ktnx.mobileledger.json.Gateway {
+    @Override
+    public String transactionSaveRequest(LedgerTransaction ledgerTransaction)
+            throws JsonProcessingException {
+        net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction jsonTransaction =
+                net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.fromLedgerTransaction(
+                        ledgerTransaction);
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectWriter writer =
+                mapper.writerFor(net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.class);
+        return writer.writeValueAsString(jsonTransaction);
+    }
+}
diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/Gateway.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/Gateway.java
new file mode 100644 (file)
index 0000000..239ec2d
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2020 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.json.v1_19_1;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+
+import net.ktnx.mobileledger.model.LedgerTransaction;
+
+public class Gateway extends net.ktnx.mobileledger.json.Gateway {
+    @Override
+    public String transactionSaveRequest(LedgerTransaction ledgerTransaction)
+            throws JsonProcessingException {
+        net.ktnx.mobileledger.json.v1_19_1.ParsedLedgerTransaction jsonTransaction =
+                net.ktnx.mobileledger.json.v1_19_1.ParsedLedgerTransaction.fromLedgerTransaction(
+                        ledgerTransaction);
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectWriter writer =
+                mapper.writerFor(net.ktnx.mobileledger.json.v1_19_1.ParsedLedgerTransaction.class);
+        return writer.writeValueAsString(jsonTransaction);
+    }
+}