Skip to content

Commit

Permalink
Route cleanups and basic multi logic
Browse files Browse the repository at this point in the history
  • Loading branch information
KamWithK committed Mar 24, 2022
1 parent 4894b81 commit 54da266
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public static byte[] getMediaData(JsonObject raw_data) {
String encoded = raw_data.get("params").getAsJsonObject().get("data").getAsString();
return Base64.decode(encoded, Base64.DEFAULT);
}

public static JsonArray getMultiActions(JsonObject raw_data) {
return raw_data.get("params").getAsJsonObject().get("actions").getAsJsonArray();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kamwithk.ankiconnectandroid.routing;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.kamwithk.ankiconnectandroid.ankidroid_api.DeckAPI;
import com.kamwithk.ankiconnectandroid.ankidroid_api.IntegratedAPI;
Expand All @@ -26,7 +28,7 @@ public AnkiAPIRouting(IntegratedAPI integratedAPI) {
mediaAPI = integratedAPI.mediaAPI;
}

public NanoHTTPD.Response findRoute(JsonObject raw_json) throws Exception {
private String findRoute(JsonObject raw_json) throws Exception {
switch (Parser.get_action(raw_json)) {
case "version":
return version();
Expand All @@ -40,20 +42,31 @@ public NanoHTTPD.Response findRoute(JsonObject raw_json) throws Exception {
return modelNamesAndIds();
case "modelFieldNames":
return modelFieldNames(raw_json);
case "findNotes":
return findNotes();
case "canAddNotes":
return canAddNotes(raw_json);
case "addNote":
return addNote(raw_json);
case "storeMediaFile":
return storeMediaFile(raw_json);
case "multi":
JsonArray actions = Parser.getMultiActions(raw_json);
JsonArray results = new JsonArray();

for (JsonElement jsonElement : actions) {
results.add(Parser.parse(findRoute(jsonElement.getAsJsonObject())));
}

return Parser.gson.toJson(results);
default:
return default_version();
}
}

public NanoHTTPD.Response findRouteHandleError(JsonObject raw_json) {
try {
return findRoute(raw_json);
return returnResponse(findRoute(raw_json));
} catch (Exception e) {
Map<String, String> response = new HashMap<>();
response.put("result", null);
Expand All @@ -63,91 +76,71 @@ public NanoHTTPD.Response findRouteHandleError(JsonObject raw_json) {
}
}

private NanoHTTPD.Response version() {
return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "text/json", "6");
private NanoHTTPD.Response returnResponse(String response) {
return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "text/json", response);
}

private String version() {
return "6";
}

private NanoHTTPD.Response default_version() {
return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "text/json", "AnkiConnect v.6");
private String default_version() {
return "AnkiConnect v.6";
}

private NanoHTTPD.Response deckNames() throws Exception {
return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
"text/json",
Parser.gson.toJson(deckAPI.deckNames())
);
private String deckNames() throws Exception {
return Parser.gson.toJson(deckAPI.deckNames());
}

private NanoHTTPD.Response deckNamesAndIds() throws Exception {
return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
"text/json",
Parser.gson.toJson(deckAPI.deckNamesAndIds())
);
private String deckNamesAndIds() throws Exception {
return Parser.gson.toJson(deckAPI.deckNamesAndIds());
}

private NanoHTTPD.Response modelNames() throws Exception {
return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
"text/json",
Parser.gson.toJson(modelAPI.modelNames())
);
private String modelNames() throws Exception {
return Parser.gson.toJson(modelAPI.modelNames());
}

private NanoHTTPD.Response modelNamesAndIds() throws Exception {
return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
"text/json",
Parser.gson.toJson(modelAPI.modelNamesAndIds(0))
);
private String modelNamesAndIds() throws Exception {
return Parser.gson.toJson(modelAPI.modelNamesAndIds(0));
}

private NanoHTTPD.Response modelFieldNames(JsonObject raw_json) throws Exception {
private String modelFieldNames(JsonObject raw_json) throws Exception {
String model_name = Parser.getModelNameFromParam(raw_json);
if (model_name != null && !model_name.equals("")) {
Long model_id = modelAPI.getModelID(model_name, 0);

return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
"text/json",
Parser.gson.toJson(modelAPI.modelFieldNames(model_id))
);
return Parser.gson.toJson(modelAPI.modelFieldNames(model_id));
} else {
Map<String, String> response = new HashMap<>();
response.put("result", null);
response.put("error", "model was not found: ");

return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
"text/json",
Parser.gson.toJson(response)
);
return Parser.gson.toJson(response);
}
}

private String findNotes() {
return "[]";
}

// TODO: Implement
private NanoHTTPD.Response canAddNotes(JsonObject raw_json) throws Exception {
private String canAddNotes(JsonObject raw_json) throws Exception {
Map<String, boolean[]> response = new HashMap<>();
response.put("result", Parser.getNoteTrues(raw_json));
response.put("error", null);

return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "text/json", Parser.gson.toJson(response));
return Parser.gson.toJson(response);
}

private NanoHTTPD.Response addNote(JsonObject raw_json) throws Exception {
return newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
"text/json",
String.valueOf(integratedAPI.addNote(
Parser.getNoteValues(raw_json),
Parser.getDeckName(raw_json),
Parser.getModelName(raw_json)
))
);
private String addNote(JsonObject raw_json) throws Exception {
return String.valueOf(integratedAPI.addNote(
Parser.getNoteValues(raw_json),
Parser.getDeckName(raw_json),
Parser.getModelName(raw_json)
));
}

private NanoHTTPD.Response storeMediaFile(JsonObject raw_json) throws Exception {
private String storeMediaFile(JsonObject raw_json) throws Exception {
Map<String, String> response = new HashMap<>();

response.put("result", integratedAPI.storeMediaFile(
Expand All @@ -156,6 +149,6 @@ private NanoHTTPD.Response storeMediaFile(JsonObject raw_json) throws Exception
));
response.put("error", null);

return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "text/json", Parser.gson.toJson(response));
return Parser.gson.toJson(response);
}
}

0 comments on commit 54da266

Please sign in to comment.