From 69572b3a1fbf2e6905e5dacbd1255d63b276a431 Mon Sep 17 00:00:00 2001 From: NMerz Date: Sat, 3 Oct 2020 11:22:35 -0400 Subject: [PATCH 1/7] Generify endpoint groups as IntelliJ Modules This allows them to be built sperately decreasing JAR size and also creates a better structure for generic service functions and business logic. --- Lambdas/Lists/Item/src/ItemAdder.java | 27 +++++++++++++++++ Lambdas/Lists/Item/src/ItemGET.java | 11 +++++++ Lambdas/Lists/Item/src/ItemGetter.java | 9 ++++++ Lambdas/Lists/Item/src/ItemPOST.java | 11 +++++++ .../main/java => List/src}/ListAdder.java | 7 +++-- Lambdas/Lists/List/src/ListPOST.java | 11 +++++++ Lambdas/Lists/src/main/java/BasicHandler.java | 29 +++++++++++++++++++ Lambdas/Lists/src/main/java/CallHandler.java | 6 ++++ Lambdas/Lists/src/main/java/DBConnector.java | 4 +-- Lambdas/Lists/src/main/java/InputUtils.java | 4 +++ Lambdas/Lists/src/main/java/ListPOST.java | 28 ------------------ Listify/.idea/misc.xml | 2 +- 12 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 Lambdas/Lists/Item/src/ItemAdder.java create mode 100644 Lambdas/Lists/Item/src/ItemGET.java create mode 100644 Lambdas/Lists/Item/src/ItemGetter.java create mode 100644 Lambdas/Lists/Item/src/ItemPOST.java rename Lambdas/Lists/{src/main/java => List/src}/ListAdder.java (75%) create mode 100644 Lambdas/Lists/List/src/ListPOST.java create mode 100644 Lambdas/Lists/src/main/java/BasicHandler.java create mode 100644 Lambdas/Lists/src/main/java/CallHandler.java delete mode 100644 Lambdas/Lists/src/main/java/ListPOST.java diff --git a/Lambdas/Lists/Item/src/ItemAdder.java b/Lambdas/Lists/Item/src/ItemAdder.java new file mode 100644 index 0000000..bea8cfa --- /dev/null +++ b/Lambdas/Lists/Item/src/ItemAdder.java @@ -0,0 +1,27 @@ +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Map; + +public class ItemAdder implements CallHandler { + + private DBConnector connector; + private String cognitoID; + + private final String LIST_CREATE = "INSERT INTO Items (Name) VALUES (?)"; + + public ItemAdder(DBConnector connector, String cognitoID) { + this.connector = connector; + this.cognitoID = cognitoID; + } + + public String conductAction(Map bodyMap, String queryString) throws SQLException { + Connection connection = connector.getConnection(); + PreparedStatement statement = connection.prepareStatement(LIST_CREATE); + statement.setString(1, bodyMap.get("name").toString());//Needs safe checking + System.out.println(statement); + statement.executeUpdate(); + connection.commit(); + return null; + } +} diff --git a/Lambdas/Lists/Item/src/ItemGET.java b/Lambdas/Lists/Item/src/ItemGET.java new file mode 100644 index 0000000..3794b51 --- /dev/null +++ b/Lambdas/Lists/Item/src/ItemGET.java @@ -0,0 +1,11 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class ItemGET implements RequestHandler, String> { + + public String handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ItemGetter.class); + } +} diff --git a/Lambdas/Lists/Item/src/ItemGetter.java b/Lambdas/Lists/Item/src/ItemGetter.java new file mode 100644 index 0000000..57a350c --- /dev/null +++ b/Lambdas/Lists/Item/src/ItemGetter.java @@ -0,0 +1,9 @@ +import java.sql.SQLException; +import java.util.Map; + +public class ItemGetter implements CallHandler{ + @Override + public String conductAction(Map bodyMap, String cognitoID) throws SQLException { + return null; + } +} diff --git a/Lambdas/Lists/Item/src/ItemPOST.java b/Lambdas/Lists/Item/src/ItemPOST.java new file mode 100644 index 0000000..f2279c4 --- /dev/null +++ b/Lambdas/Lists/Item/src/ItemPOST.java @@ -0,0 +1,11 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class ItemPOST implements RequestHandler, String>{ + + public String handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ItemAdder.class); + } +} diff --git a/Lambdas/Lists/src/main/java/ListAdder.java b/Lambdas/Lists/List/src/ListAdder.java similarity index 75% rename from Lambdas/Lists/src/main/java/ListAdder.java rename to Lambdas/Lists/List/src/ListAdder.java index e1598b7..d7f4982 100644 --- a/Lambdas/Lists/src/main/java/ListAdder.java +++ b/Lambdas/Lists/List/src/ListAdder.java @@ -3,19 +3,19 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Map; -public class ListAdder { +public class ListAdder implements CallHandler { private DBConnector connector; private String cognitoID; private final String LIST_CREATE = "INSERT INTO Lists (Name, Owner) VALUES (?, ?)"; - ListAdder(DBConnector connector, String cognitoID) { + public ListAdder(DBConnector connector, String cognitoID) { this.connector = connector; this.cognitoID = cognitoID; } - public void add(Map bodyMap) throws SQLException { + public String conductAction(Map bodyMap, String queryString) throws SQLException { Connection connection = connector.getConnection(); PreparedStatement statement = connection.prepareStatement(LIST_CREATE); statement.setString(1, bodyMap.get("name").toString());//Needs safe checking @@ -23,5 +23,6 @@ public class ListAdder { System.out.println(statement); statement.executeUpdate(); connection.commit(); + return null; } } diff --git a/Lambdas/Lists/List/src/ListPOST.java b/Lambdas/Lists/List/src/ListPOST.java new file mode 100644 index 0000000..13c2785 --- /dev/null +++ b/Lambdas/Lists/List/src/ListPOST.java @@ -0,0 +1,11 @@ +import java.util.Map; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +public class ListPOST implements RequestHandler, String>{ + + public String handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ListAdder.class); + } +} diff --git a/Lambdas/Lists/src/main/java/BasicHandler.java b/Lambdas/Lists/src/main/java/BasicHandler.java new file mode 100644 index 0000000..8b642eb --- /dev/null +++ b/Lambdas/Lists/src/main/java/BasicHandler.java @@ -0,0 +1,29 @@ +import com.amazonaws.services.lambda.runtime.Context; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.sql.SQLException; +import java.util.Map; + +public class BasicHandler { + public static String handleRequest(Map inputMap, Context unfilled, Class toCall) { + String cognitoID = InputUtils.getCognitoIDFromBody(inputMap); + try { + DBConnector connector = new DBConnector(); + try { + Constructor constructor = toCall.getConstructor(DBConnector.class, String.class); + CallHandler callHandler = constructor.newInstance(connector, cognitoID); + callHandler.conductAction(InputUtils.getBody(inputMap), ""); + } catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } finally { + connector.close(); + } + } catch (IOException |SQLException|ClassNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } + return null; + } +} diff --git a/Lambdas/Lists/src/main/java/CallHandler.java b/Lambdas/Lists/src/main/java/CallHandler.java new file mode 100644 index 0000000..ac5aa2a --- /dev/null +++ b/Lambdas/Lists/src/main/java/CallHandler.java @@ -0,0 +1,6 @@ +import java.sql.SQLException; +import java.util.Map; + +public interface CallHandler { + String conductAction(Map bodyMap, String cognitoID) throws SQLException; +} diff --git a/Lambdas/Lists/src/main/java/DBConnector.java b/Lambdas/Lists/src/main/java/DBConnector.java index 074f4de..798cfc3 100644 --- a/Lambdas/Lists/src/main/java/DBConnector.java +++ b/Lambdas/Lists/src/main/java/DBConnector.java @@ -12,11 +12,11 @@ public class DBConnector { Connection connection; - DBConnector() throws IOException, SQLException, ClassNotFoundException { + public DBConnector() throws IOException, SQLException, ClassNotFoundException { this(loadProperties("dbProperties.json")); } - DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException { + public DBConnector(Properties dbProperties) throws SQLException, ClassNotFoundException { Class.forName("org.mariadb.jdbc.Driver"); System.out.println(dbProperties); System.out.println(DBConnector.buildURL(dbProperties)); diff --git a/Lambdas/Lists/src/main/java/InputUtils.java b/Lambdas/Lists/src/main/java/InputUtils.java index 46a0694..b10781d 100644 --- a/Lambdas/Lists/src/main/java/InputUtils.java +++ b/Lambdas/Lists/src/main/java/InputUtils.java @@ -19,6 +19,10 @@ public class InputUtils { return getMap(inputMap, "body"); } + public static String getQueryString(Map inputMap) { + return (String) (getMap(inputMap, "params").get("querystring")); + } + public static Map getMap(Map parentMap, String childKey) { if ((parentMap.get(childKey) != null) && (parentMap.get(childKey) instanceof Map)) { return ((Map) parentMap.get(childKey)); diff --git a/Lambdas/Lists/src/main/java/ListPOST.java b/Lambdas/Lists/src/main/java/ListPOST.java deleted file mode 100644 index c2fc63c..0000000 --- a/Lambdas/Lists/src/main/java/ListPOST.java +++ /dev/null @@ -1,28 +0,0 @@ -import java.io.IOException; -import java.sql.SQLException; -import java.util.Map; - -import com.amazonaws.services.lambda.runtime.Context; -import com.amazonaws.services.lambda.runtime.RequestHandler; - -public class ListPOST implements RequestHandler, String>{ - - - public String handleRequest(Map inputMap, Context unfilled) { - String cognitoID = InputUtils.getCognitoIDFromBody(inputMap); - try { - DBConnector connector = new DBConnector(); - try { - new ListAdder(connector, cognitoID).add(InputUtils.getBody(inputMap)); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - connector.close(); - } - } catch (IOException|SQLException|ClassNotFoundException e) { - e.printStackTrace(); - throw new RuntimeException(e.getMessage()); - } - return null; - } -} diff --git a/Listify/.idea/misc.xml b/Listify/.idea/misc.xml index d5d35ec..fdae1d0 100644 --- a/Listify/.idea/misc.xml +++ b/Listify/.idea/misc.xml @@ -1,6 +1,6 @@ - + From f107ab023d12cf425ff8d8793587f7e486f193d2 Mon Sep 17 00:00:00 2001 From: NMerz Date: Sat, 3 Oct 2020 17:37:46 -0400 Subject: [PATCH 2/7] GET strcuture Build out infrastructure for get requests (along with the first couple). This includes changing the request engine from Volley to OKHTTP due to issues get Volley's callbacks to call back. --- Lambdas/Lists/Item/src/Item.java | 52 ++++++++++ Lambdas/Lists/Item/src/ItemAdder.java | 25 +++-- Lambdas/Lists/Item/src/ItemGET.java | 4 +- Lambdas/Lists/Item/src/ItemGetter.java | 31 +++++- Lambdas/Lists/Item/src/ItemPOST.java | 4 +- Lambdas/Lists/List/src/ItemEntry.java | 16 ++++ Lambdas/Lists/List/src/List.java | 18 ++++ Lambdas/Lists/List/src/ListAdder.java | 8 +- Lambdas/Lists/List/src/ListGET.java | 12 +++ Lambdas/Lists/List/src/ListGetter.java | 39 ++++++++ Lambdas/Lists/List/src/ListPOST.java | 4 +- Lambdas/Lists/pom.xml | 10 ++ Lambdas/Lists/src/main/java/BasicHandler.java | 6 +- Lambdas/Lists/src/main/java/CallHandler.java | 3 +- Lambdas/Lists/src/main/java/InputUtils.java | 22 ++++- Listify/.idea/misc.xml | 2 +- Listify/app/build.gradle | 1 + .../main/java/com/example/listify/Item.java | 44 +++++++++ .../com/example/listify/MainActivity.java | 14 ++- .../java/com/example/listify/Requestor.java | 96 +++++++++++++------ .../example/listify/SynchronousReceiver.java | 39 ++++++++ 21 files changed, 398 insertions(+), 52 deletions(-) create mode 100644 Lambdas/Lists/Item/src/Item.java create mode 100644 Lambdas/Lists/List/src/ItemEntry.java create mode 100644 Lambdas/Lists/List/src/List.java create mode 100644 Lambdas/Lists/List/src/ListGET.java create mode 100644 Lambdas/Lists/List/src/ListGetter.java create mode 100644 Listify/app/src/main/java/com/example/listify/Item.java create mode 100644 Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java diff --git a/Lambdas/Lists/Item/src/Item.java b/Lambdas/Lists/Item/src/Item.java new file mode 100644 index 0000000..53ef341 --- /dev/null +++ b/Lambdas/Lists/Item/src/Item.java @@ -0,0 +1,52 @@ +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDateTime; + +public class Item { + Integer productID; + Integer chainID; + String upc; + String description; + BigDecimal price; + String imageURL; + String department; + LocalDateTime retrievedDate; + Integer fetchCounts; + + Item(ResultSet itemRow) throws SQLException { + this.productID = itemRow.getInt(1); + System.out.println(this.productID); + this.chainID = itemRow.getInt(2); + System.out.println(this.chainID); + this.upc = itemRow.getString(3); + System.out.println(this.upc); + this.description = itemRow.getString(4); + System.out.println(this.description); + this.price = itemRow.getBigDecimal(5); + System.out.println(this.price); + this.imageURL = itemRow.getString(6); + System.out.println(imageURL); + this.department = itemRow.getString(7); + System.out.println(department); + this.retrievedDate = itemRow.getObject(8, LocalDateTime.class); + System.out.println(retrievedDate); + this.fetchCounts = itemRow.getInt(9); + System.out.println(fetchCounts); + } + + @Override + public String toString() { + return "Item{" + + "productID=" + productID + + ", chainID=" + chainID + + ", upc='" + upc + '\'' + + ", description='" + description + '\'' + + ", price=" + price + + ", imageURL='" + imageURL + '\'' + + ", department='" + department + '\'' + + ", retrievedDate=" + retrievedDate + + ", fetchCounts=" + fetchCounts + + '}'; + } +} diff --git a/Lambdas/Lists/Item/src/ItemAdder.java b/Lambdas/Lists/Item/src/ItemAdder.java index bea8cfa..c698a04 100644 --- a/Lambdas/Lists/Item/src/ItemAdder.java +++ b/Lambdas/Lists/Item/src/ItemAdder.java @@ -1,6 +1,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.HashMap; import java.util.Map; public class ItemAdder implements CallHandler { @@ -8,20 +11,28 @@ public class ItemAdder implements CallHandler { private DBConnector connector; private String cognitoID; - private final String LIST_CREATE = "INSERT INTO Items (Name) VALUES (?)"; + private final String ITEM_TO_LIST = "INSERT INTO ListProduct (productID, listID, quantity, addedDate, purchased) VALUES (?, ?, ?, ?, ?)"; public ItemAdder(DBConnector connector, String cognitoID) { this.connector = connector; this.cognitoID = cognitoID; } - public String conductAction(Map bodyMap, String queryString) throws SQLException { + public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { Connection connection = connector.getConnection(); - PreparedStatement statement = connection.prepareStatement(LIST_CREATE); - statement.setString(1, bodyMap.get("name").toString());//Needs safe checking - System.out.println(statement); - statement.executeUpdate(); - connection.commit(); + try { + PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST); + statement.setInt(1, (Integer) bodyMap.get("itemID")); + statement.setInt(2, (Integer) bodyMap.get("listID")); + statement.setInt(3, (Integer) bodyMap.get("quantity")); + statement.setObject(4, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime()); + statement.setBoolean(5, (Boolean) bodyMap.get("purchased")); + System.out.println(statement); + statement.executeUpdate(); + connection.commit(); + } finally { + connection.close(); + } return null; } } diff --git a/Lambdas/Lists/Item/src/ItemGET.java b/Lambdas/Lists/Item/src/ItemGET.java index 3794b51..71c0804 100644 --- a/Lambdas/Lists/Item/src/ItemGET.java +++ b/Lambdas/Lists/Item/src/ItemGET.java @@ -3,9 +3,9 @@ import com.amazonaws.services.lambda.runtime.RequestHandler; import java.util.Map; -public class ItemGET implements RequestHandler, String> { +public class ItemGET implements RequestHandler, Object> { - public String handleRequest(Map inputMap, Context unfilled) { + public Object handleRequest(Map inputMap, Context unfilled) { return BasicHandler.handleRequest(inputMap, unfilled, ItemGetter.class); } } diff --git a/Lambdas/Lists/Item/src/ItemGetter.java b/Lambdas/Lists/Item/src/ItemGetter.java index 57a350c..2414e3a 100644 --- a/Lambdas/Lists/Item/src/ItemGetter.java +++ b/Lambdas/Lists/Item/src/ItemGetter.java @@ -1,9 +1,36 @@ +import com.google.gson.Gson; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.util.HashMap; import java.util.Map; public class ItemGetter implements CallHandler{ + private final DBConnector connector; + + private final String GET_ITEM = "SELECT * FROM Product WHERE productID = ?;"; + + public ItemGetter(DBConnector connector, String cognitoID) { + this.connector = connector; + } + @Override - public String conductAction(Map bodyMap, String cognitoID) throws SQLException { - return null; + public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { + Connection connection = connector.getConnection(); + try { + PreparedStatement statement = connection.prepareStatement(GET_ITEM); + statement.setInt(1, Integer.parseInt(queryMap.get("id"))); + System.out.println(statement); + ResultSet queryResults = statement.executeQuery(); + queryResults.first(); + System.out.println(queryResults); + Item retrievedItem = new Item(queryResults); + System.out.println(retrievedItem); + return retrievedItem; + } finally { + connection.close(); + } } } diff --git a/Lambdas/Lists/Item/src/ItemPOST.java b/Lambdas/Lists/Item/src/ItemPOST.java index f2279c4..51d107f 100644 --- a/Lambdas/Lists/Item/src/ItemPOST.java +++ b/Lambdas/Lists/Item/src/ItemPOST.java @@ -3,9 +3,9 @@ import com.amazonaws.services.lambda.runtime.RequestHandler; import java.util.Map; -public class ItemPOST implements RequestHandler, String>{ +public class ItemPOST implements RequestHandler, Object> { - public String handleRequest(Map inputMap, Context unfilled) { + public Object handleRequest(Map inputMap, Context unfilled) { return BasicHandler.handleRequest(inputMap, unfilled, ItemAdder.class); } } diff --git a/Lambdas/Lists/List/src/ItemEntry.java b/Lambdas/Lists/List/src/ItemEntry.java new file mode 100644 index 0000000..b05ddc7 --- /dev/null +++ b/Lambdas/Lists/List/src/ItemEntry.java @@ -0,0 +1,16 @@ +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDateTime; + +public class ItemEntry { + Integer productID; + Integer quantity; + LocalDateTime addedDate; + Boolean purchased; + public ItemEntry(ResultSet listRow) throws SQLException { + productID = listRow.getInt(1); + quantity = listRow.getInt(2); + addedDate = listRow.getObject(3, LocalDateTime.class); + purchased = listRow.getBoolean(4); + } +} diff --git a/Lambdas/Lists/List/src/List.java b/Lambdas/Lists/List/src/List.java new file mode 100644 index 0000000..c2ac9c6 --- /dev/null +++ b/Lambdas/Lists/List/src/List.java @@ -0,0 +1,18 @@ +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDateTime; + +public class List { + Integer itemID; + String name; + String owner; + LocalDateTime lastUpdated; + + public List(ResultSet listRow) throws SQLException { + itemID = listRow.getInt(1); + name = listRow.getString(2); + owner = listRow.getString(3); + lastUpdated = listRow.getObject(8, LocalDateTime.class); + } + +} diff --git a/Lambdas/Lists/List/src/ListAdder.java b/Lambdas/Lists/List/src/ListAdder.java index d7f4982..eb56e36 100644 --- a/Lambdas/Lists/List/src/ListAdder.java +++ b/Lambdas/Lists/List/src/ListAdder.java @@ -1,6 +1,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.HashMap; import java.util.Map; public class ListAdder implements CallHandler { @@ -8,18 +11,19 @@ public class ListAdder implements CallHandler { private DBConnector connector; private String cognitoID; - private final String LIST_CREATE = "INSERT INTO Lists (Name, Owner) VALUES (?, ?)"; + private final String LIST_CREATE = "INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?)"; public ListAdder(DBConnector connector, String cognitoID) { this.connector = connector; this.cognitoID = cognitoID; } - public String conductAction(Map bodyMap, String queryString) throws SQLException { + public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { Connection connection = connector.getConnection(); PreparedStatement statement = connection.prepareStatement(LIST_CREATE); statement.setString(1, bodyMap.get("name").toString());//Needs safe checking statement.setString(2, cognitoID); + statement.setObject(3, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime()); System.out.println(statement); statement.executeUpdate(); connection.commit(); diff --git a/Lambdas/Lists/List/src/ListGET.java b/Lambdas/Lists/List/src/ListGET.java new file mode 100644 index 0000000..12a0889 --- /dev/null +++ b/Lambdas/Lists/List/src/ListGET.java @@ -0,0 +1,12 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class ListGET implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ListGetter.class); + } + +} \ No newline at end of file diff --git a/Lambdas/Lists/List/src/ListGetter.java b/Lambdas/Lists/List/src/ListGetter.java new file mode 100644 index 0000000..9e8596a --- /dev/null +++ b/Lambdas/Lists/List/src/ListGetter.java @@ -0,0 +1,39 @@ +import com.google.gson.Gson; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class ListGetter implements CallHandler{ + private final DBConnector connector; + private final String cognitoID; + + private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;"; + + public ListGetter(DBConnector connector, String cognitoID) { + this.connector = connector; + this.cognitoID = cognitoID; + } + + @Override + public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { + Connection connection = connector.getConnection(); + try { + PreparedStatement statement = connection.prepareStatement(GET_LIST); + statement.setInt(1, Integer.parseInt(queryMap.get("id"))); + System.out.println(statement); + ResultSet queryResults = statement.executeQuery(); + queryResults.first(); + System.out.println(queryResults); + String returnValue; + List retrievedItem = new List(queryResults); + System.out.println(retrievedItem); + return retrievedItem; + } finally { + connection.close(); + } + } +} diff --git a/Lambdas/Lists/List/src/ListPOST.java b/Lambdas/Lists/List/src/ListPOST.java index 13c2785..4d47863 100644 --- a/Lambdas/Lists/List/src/ListPOST.java +++ b/Lambdas/Lists/List/src/ListPOST.java @@ -3,9 +3,9 @@ import java.util.Map; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; -public class ListPOST implements RequestHandler, String>{ +public class ListPOST implements RequestHandler, Object>{ - public String handleRequest(Map inputMap, Context unfilled) { + public Object handleRequest(Map inputMap, Context unfilled) { return BasicHandler.handleRequest(inputMap, unfilled, ListAdder.class); } } diff --git a/Lambdas/Lists/pom.xml b/Lambdas/Lists/pom.xml index 8d77579..1984615 100644 --- a/Lambdas/Lists/pom.xml +++ b/Lambdas/Lists/pom.xml @@ -39,6 +39,16 @@ mariadb-java-client 2.7.0 + + org.apache.httpcomponents + httpclient + 4.5 + + + com.google.code.gson + gson + 2.8.6 + 1.11 diff --git a/Lambdas/Lists/src/main/java/BasicHandler.java b/Lambdas/Lists/src/main/java/BasicHandler.java index 8b642eb..2a7ec6e 100644 --- a/Lambdas/Lists/src/main/java/BasicHandler.java +++ b/Lambdas/Lists/src/main/java/BasicHandler.java @@ -4,17 +4,19 @@ import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; +import java.util.HashMap; import java.util.Map; public class BasicHandler { - public static String handleRequest(Map inputMap, Context unfilled, Class toCall) { + public static Object handleRequest(Map inputMap, Context unfilled, Class toCall) { String cognitoID = InputUtils.getCognitoIDFromBody(inputMap); + HashMap queryMap = InputUtils.getQueryParams(inputMap); try { DBConnector connector = new DBConnector(); try { Constructor constructor = toCall.getConstructor(DBConnector.class, String.class); CallHandler callHandler = constructor.newInstance(connector, cognitoID); - callHandler.conductAction(InputUtils.getBody(inputMap), ""); + return callHandler.conductAction(InputUtils.getBody(inputMap), queryMap, cognitoID); } catch (SQLException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } finally { diff --git a/Lambdas/Lists/src/main/java/CallHandler.java b/Lambdas/Lists/src/main/java/CallHandler.java index ac5aa2a..a637412 100644 --- a/Lambdas/Lists/src/main/java/CallHandler.java +++ b/Lambdas/Lists/src/main/java/CallHandler.java @@ -1,6 +1,7 @@ import java.sql.SQLException; +import java.util.HashMap; import java.util.Map; public interface CallHandler { - String conductAction(Map bodyMap, String cognitoID) throws SQLException; + Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException; } diff --git a/Lambdas/Lists/src/main/java/InputUtils.java b/Lambdas/Lists/src/main/java/InputUtils.java index b10781d..2a4e8bc 100644 --- a/Lambdas/Lists/src/main/java/InputUtils.java +++ b/Lambdas/Lists/src/main/java/InputUtils.java @@ -1,3 +1,9 @@ +import org.apache.http.NameValuePair; +import org.apache.http.client.utils.URLEncodedUtils; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; import java.util.Map; public class InputUtils { @@ -19,10 +25,24 @@ public class InputUtils { return getMap(inputMap, "body"); } - public static String getQueryString(Map inputMap) { + private static String getQueryString(Map inputMap) { return (String) (getMap(inputMap, "params").get("querystring")); } + public static HashMap getQueryParams(Map inputMap) { + return (HashMap) (getMap(inputMap, "params").get("querystring")); + +// String queryString = getQueryString(inputMap); +// List queryparams = URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8); +// HashMap mappedParams = new HashMap<>(); +// for (NameValuePair param : queryparams) { +// mappedParams.put(param.getName(), param.getValue()); +// } +// return mappedParams; + } + + + public static Map getMap(Map parentMap, String childKey) { if ((parentMap.get(childKey) != null) && (parentMap.get(childKey) instanceof Map)) { return ((Map) parentMap.get(childKey)); diff --git a/Listify/.idea/misc.xml b/Listify/.idea/misc.xml index fdae1d0..d5d35ec 100644 --- a/Listify/.idea/misc.xml +++ b/Listify/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/Listify/app/build.gradle b/Listify/app/build.gradle index 099077a..0ad5dc3 100644 --- a/Listify/app/build.gradle +++ b/Listify/app/build.gradle @@ -50,5 +50,6 @@ dependencies { implementation 'org.json:json:20200518' implementation 'com.github.bumptech.glide:glide:4.11.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0' + implementation 'com.squareup.okhttp3:okhttp:4.8.1' } \ No newline at end of file diff --git a/Listify/app/src/main/java/com/example/listify/Item.java b/Listify/app/src/main/java/com/example/listify/Item.java new file mode 100644 index 0000000..f66f8ec --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/Item.java @@ -0,0 +1,44 @@ +package com.example.listify; + +import java.math.BigDecimal; +import java.time.LocalDateTime; + +public class Item { + Integer productID; + Integer chainID; + String upc; + String description; + BigDecimal price; + String imageURL; + String department; + LocalDateTime retrievedDate; + Integer fetchCounts; + + public Item(Integer productID, Integer chainID, String upc, String description, BigDecimal price, + String imageURL, String department, LocalDateTime retrievedDate, Integer fetchCounts) { + this.productID = productID; + this.chainID = chainID; + this.upc = upc; + this.description = description; + this.price = price; + this.imageURL = imageURL; + this.department = department; + this.retrievedDate = retrievedDate; + this.fetchCounts = fetchCounts; + } + + @Override + public String toString() { + return "Item{" + + "productID=" + productID + + ", chainID=" + chainID + + ", upc='" + upc + '\'' + + ", description='" + description + '\'' + + ", price=" + price + + ", imageURL='" + imageURL + '\'' + + ", department='" + department + '\'' + + ", retrievedDate=" + retrievedDate + + ", fetchCounts=" + fetchCounts + + '}'; + } +} diff --git a/Listify/app/src/main/java/com/example/listify/MainActivity.java b/Listify/app/src/main/java/com/example/listify/MainActivity.java index 8260ed5..59c63d4 100644 --- a/Listify/app/src/main/java/com/example/listify/MainActivity.java +++ b/Listify/app/src/main/java/com/example/listify/MainActivity.java @@ -25,6 +25,8 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + //------------------------------Auth Testing---------------------------------------------// AuthManager authManager = new AuthManager(); @@ -46,6 +48,7 @@ public class MainActivity extends AppCompatActivity { //------------------------------------------------------------------------------------------// + //----------------------------------API Testing---------------------------------------------// Properties configs = new Properties(); try { @@ -54,7 +57,7 @@ public class MainActivity extends AppCompatActivity { e.printStackTrace(); } - Requestor requestor = new Requestor(this, authManager,configs.getProperty("apiKey")); + Requestor requestor = new Requestor(authManager,configs.getProperty("apiKey")); List testList = new List("IAmATestList"); try { requestor.postObject(testList); @@ -62,6 +65,15 @@ public class MainActivity extends AppCompatActivity { e.printStackTrace(); } + SynchronousReceiver receiver = new SynchronousReceiver(); + requestor.getObject("1", Item.class, receiver, receiver); + try { + System.out.println(receiver.await()); + } catch (IOException receiverError) { + receiverError.printStackTrace(); + } + + //------------------------------------------------------------------------------------------// diff --git a/Listify/app/src/main/java/com/example/listify/Requestor.java b/Listify/app/src/main/java/com/example/listify/Requestor.java index 0c7b48d..3032c0d 100644 --- a/Listify/app/src/main/java/com/example/listify/Requestor.java +++ b/Listify/app/src/main/java/com/example/listify/Requestor.java @@ -1,66 +1,104 @@ package com.example.listify; -import android.content.Context; -import com.android.volley.RequestQueue; -import com.android.volley.Response; -import com.android.volley.toolbox.JsonObjectRequest; -import com.android.volley.toolbox.Volley; +import android.util.Log; import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import okhttp3.*; +import org.jetbrains.annotations.NotNull; import org.json.JSONException; -import org.json.JSONObject; -import java.util.HashMap; -import java.util.Map; +import java.io.IOException; public class Requestor { private final String DEV_BASEURL = "https://datoh7woc9.execute-api.us-east-2.amazonaws.com/Development"; AuthManager authManager; - RequestQueue queue; String apiKey; + OkHttpClient client; - Requestor(Context context, AuthManager authManager, String apiKey) { - queue = Volley.newRequestQueue(context); + Requestor(AuthManager authManager, String apiKey) { this.authManager = authManager; this.apiKey = apiKey; + client = new OkHttpClient(); } public void getObject(String id, Class classType, Receiver receiver) { - String getURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id; + getObject(id, classType, receiver, null); } - public void postObject(Object toPost, Response.ErrorListener failureHandler) throws JSONException { - String postURL = DEV_BASEURL + "/" + toPost.getClass().getSimpleName(); - queue.add(buildRequest(postURL, toPost, null, failureHandler)); + public void getObject(String id, Class classType, Receiver successHandler, RequestErrorHandler failureHandler) { + String getURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id; + Request postRequest = buildBaseRequest(getURL, "GET", null); + launchCall(postRequest, successHandler, classType, failureHandler); } public void postObject(Object toPost) throws JSONException { postObject(toPost, null); } - private JsonObjectRequest buildRequest(String url, Object toJSONify, Response.Listener successHandler, Response.ErrorListener failureHandler) throws JSONException { - return buildRequest(url, new JSONObject(new Gson().toJson(toJSONify)), successHandler, failureHandler); + public void postObject(Object toPost, RequestErrorHandler failureHandler) throws JSONException { + String postURL = DEV_BASEURL + "/" + toPost.getClass().getSimpleName(); + Request postRequest = buildBaseRequest(postURL, "POST", new Gson().toJson(toPost)); + launchCall(postRequest, null, null, failureHandler); } - private JsonObjectRequest buildRequest(String url, JSONObject jsonBody, Response.Listener successHandler, Response.ErrorListener failureHandler) { - return new JsonObjectRequest(url, jsonBody, successHandler, failureHandler) { + + private void launchCall(Request toLaunch, Receiver receiver, Class classType, RequestErrorHandler failureHandler) { + client.newCall(toLaunch).enqueue(new Callback() { @Override - public Map getHeaders() { - HashMap headers = new HashMap<>(); - System.out.println(authManager.getUserToken()); - headers.put("Authorization", authManager.getUserToken()); - headers.put("Content-Type", "application/json"); - headers.put("X-API-Key", apiKey); - return headers; + public void onResponse(@NotNull Call call, @NotNull okhttp3.Response response) throws IOException { + String responseString = response.body().string(); + if (receiver != null) { + if (classType == null) { + Log.e("Requestor Contract Error", "classType while receiver populated"); + } + try { + receiver.acceptDelivery(new Gson().fromJson(responseString, classType)); + } catch (JsonSyntaxException e) { + System.out.println(e); + Log.e("API response was not proper JSON", responseString); + throw new JsonSyntaxException(e); + } + } + Log.d("API Response", responseString); } - }; + @Override + public void onFailure(@NotNull Call call, IOException e) { + if (failureHandler != null) { + failureHandler.acceptError(e); + } else { + Log.e("Network Error", e.getLocalizedMessage()); + } + } + }); } - public class Receiver { - public void acceptDelivery(T delivered) { + public static final MediaType JSON + = MediaType.parse("application/json; charset=utf-8"); + private Request buildBaseRequest(String url, String method, String bodyJSON) { + Request.Builder requestBase = addAuthHeaders(new Request.Builder().url(url)); + if (method == "GET") { + requestBase.get(); + } else { + requestBase.method(method, RequestBody.create(bodyJSON, JSON)); } + return requestBase.build(); + } + + private Request.Builder addAuthHeaders(Request.Builder toAuthorize) { + toAuthorize.addHeader("Authorization", authManager.getUserToken()); + toAuthorize.addHeader("X-API-Key", apiKey); + return toAuthorize; + } + + public interface Receiver { + void acceptDelivery(T delivered); + } + + public interface RequestErrorHandler { + void acceptError(IOException error); } } diff --git a/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java b/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java new file mode 100644 index 0000000..7272a3e --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java @@ -0,0 +1,39 @@ +package com.example.listify; + +import java.io.IOException; + +public class SynchronousReceiver implements Requestor.Receiver, Requestor.RequestErrorHandler { + private volatile boolean waiting; + private volatile IOException error; + private T toReturn; + + public SynchronousReceiver() { + waiting = true; + error = null; + } + + public T await() throws IOException { + while (waiting) { + Thread.yield(); + } + waiting = true; + if (error != null) { + IOException toThrow = error; + error = null; + throw toThrow; + } + return toReturn; + } + + @Override + public void acceptDelivery(Object delivered) { + toReturn = (T) delivered; + waiting = false; + } + + @Override + public void acceptError(IOException error) { + waiting = false; + this.error = error + } +} From 1f361cf401a2e5c58c82d13b1a92f3d8eb2bcd73 Mon Sep 17 00:00:00 2001 From: NMerz Date: Sat, 3 Oct 2020 18:35:27 -0400 Subject: [PATCH 3/7] Finish V1 of List and Item Lambdas List and Item Lambdas and client calls should be roughly finished (pending full testing once databse config is done). --- Lambdas/Lists/Item/src/Item.java | 72 +++++++++++++++++++ Lambdas/Lists/Item/src/ItemDELETE.java | 11 +++ Lambdas/Lists/Item/src/ItemDeleter.java | 33 +++++++++ Lambdas/Lists/List/src/ItemEntry.java | 32 +++++++++ Lambdas/Lists/List/src/List.java | 44 +++++++++++- Lambdas/Lists/List/src/ListGetter.java | 39 +++++++--- .../java/com/example/listify/Requestor.java | 6 ++ .../example/listify/SynchronousReceiver.java | 2 +- 8 files changed, 227 insertions(+), 12 deletions(-) create mode 100644 Lambdas/Lists/Item/src/ItemDELETE.java create mode 100644 Lambdas/Lists/Item/src/ItemDeleter.java diff --git a/Lambdas/Lists/Item/src/Item.java b/Lambdas/Lists/Item/src/Item.java index 53ef341..dcdbdd6 100644 --- a/Lambdas/Lists/Item/src/Item.java +++ b/Lambdas/Lists/Item/src/Item.java @@ -49,4 +49,76 @@ public class Item { ", fetchCounts=" + fetchCounts + '}'; } + + public Integer getProductID() { + return productID; + } + + public void setProductID(Integer productID) { + this.productID = productID; + } + + public Integer getChainID() { + return chainID; + } + + public void setChainID(Integer chainID) { + this.chainID = chainID; + } + + public String getUpc() { + return upc; + } + + public void setUpc(String upc) { + this.upc = upc; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public String getImageURL() { + return imageURL; + } + + public void setImageURL(String imageURL) { + this.imageURL = imageURL; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public LocalDateTime getRetrievedDate() { + return retrievedDate; + } + + public void setRetrievedDate(LocalDateTime retrievedDate) { + this.retrievedDate = retrievedDate; + } + + public Integer getFetchCounts() { + return fetchCounts; + } + + public void setFetchCounts(Integer fetchCounts) { + this.fetchCounts = fetchCounts; + } } diff --git a/Lambdas/Lists/Item/src/ItemDELETE.java b/Lambdas/Lists/Item/src/ItemDELETE.java new file mode 100644 index 0000000..7d0f1ee --- /dev/null +++ b/Lambdas/Lists/Item/src/ItemDELETE.java @@ -0,0 +1,11 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class ItemDELETE implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, ItemGetter.class); + } +} \ No newline at end of file diff --git a/Lambdas/Lists/Item/src/ItemDeleter.java b/Lambdas/Lists/Item/src/ItemDeleter.java new file mode 100644 index 0000000..ad03a91 --- /dev/null +++ b/Lambdas/Lists/Item/src/ItemDeleter.java @@ -0,0 +1,33 @@ +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class ItemDeleter implements CallHandler { + + private DBConnector connector; + private String cognitoID; + + private final String REMOVE_FROM_LIST = "DELETE FROM ListProduct WHERE (ProductID = ? AND ListID = ?);"; + + public ItemDeleter(DBConnector connector, String cognitoID) { + this.connector = connector; + this.cognitoID = cognitoID; + } + + public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { + Connection connection = connector.getConnection(); + try { + PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST); + statement.setInt(1, (Integer) bodyMap.get("ProductID")); + statement.setInt(2, (Integer) bodyMap.get("ListID")); + System.out.println(statement); + statement.executeUpdate(); + connection.commit(); + } finally { + connection.close(); + } + return null; + } +} diff --git a/Lambdas/Lists/List/src/ItemEntry.java b/Lambdas/Lists/List/src/ItemEntry.java index b05ddc7..513d4ae 100644 --- a/Lambdas/Lists/List/src/ItemEntry.java +++ b/Lambdas/Lists/List/src/ItemEntry.java @@ -13,4 +13,36 @@ public class ItemEntry { addedDate = listRow.getObject(3, LocalDateTime.class); purchased = listRow.getBoolean(4); } + + public Integer getProductID() { + return productID; + } + + public void setProductID(Integer productID) { + this.productID = productID; + } + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public LocalDateTime getAddedDate() { + return addedDate; + } + + public void setAddedDate(LocalDateTime addedDate) { + this.addedDate = addedDate; + } + + public Boolean getPurchased() { + return purchased; + } + + public void setPurchased(Boolean purchased) { + this.purchased = purchased; + } } diff --git a/Lambdas/Lists/List/src/List.java b/Lambdas/Lists/List/src/List.java index c2ac9c6..5f9c4f5 100644 --- a/Lambdas/Lists/List/src/List.java +++ b/Lambdas/Lists/List/src/List.java @@ -1,18 +1,60 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; +import java.util.ArrayList; public class List { Integer itemID; String name; String owner; LocalDateTime lastUpdated; + ArrayList entries; public List(ResultSet listRow) throws SQLException { itemID = listRow.getInt(1); name = listRow.getString(2); owner = listRow.getString(3); - lastUpdated = listRow.getObject(8, LocalDateTime.class); + lastUpdated = listRow.getObject(4, LocalDateTime.class); + entries = new ArrayList<>(); } + public void addItemEntry(ItemEntry entry) { + entries.add(entry); + } + + public ItemEntry[] getEntries() { + return entries.toArray(new ItemEntry[entries.size()]); + } + + public Integer getItemID() { + return itemID; + } + + public void setItemID(Integer itemID) { + this.itemID = itemID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public LocalDateTime getLastUpdated() { + return lastUpdated; + } + + public void setLastUpdated(LocalDateTime lastUpdated) { + this.lastUpdated = lastUpdated; + } } diff --git a/Lambdas/Lists/List/src/ListGetter.java b/Lambdas/Lists/List/src/ListGetter.java index 9e8596a..3e4ff1e 100644 --- a/Lambdas/Lists/List/src/ListGetter.java +++ b/Lambdas/Lists/List/src/ListGetter.java @@ -4,6 +4,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -12,6 +13,8 @@ public class ListGetter implements CallHandler{ private final String cognitoID; private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;"; + private final String GET_LISTS = "SELECT listID FROM List WHERE owner = ?;"; + private final String GET_ENTRIES = "SELECT * FROM ListProduct WHERE listID = ?;"; public ListGetter(DBConnector connector, String cognitoID) { this.connector = connector; @@ -21,17 +24,33 @@ public class ListGetter implements CallHandler{ @Override public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { Connection connection = connector.getConnection(); + Integer id = Integer.parseInt(queryMap.get("id")); try { - PreparedStatement statement = connection.prepareStatement(GET_LIST); - statement.setInt(1, Integer.parseInt(queryMap.get("id"))); - System.out.println(statement); - ResultSet queryResults = statement.executeQuery(); - queryResults.first(); - System.out.println(queryResults); - String returnValue; - List retrievedItem = new List(queryResults); - System.out.println(retrievedItem); - return retrievedItem; + if (id == -1) { + PreparedStatement getLists = connection.prepareStatement(GET_LISTS); + getLists.setString(1, cognitoID); + ResultSet getListsResults = getLists.executeQuery(); + ArrayList listIds = new ArrayList<>(); + while (getListsResults.next()) { + listIds.add(getListsResults.getInt(1)); + } + return listIds; + } + PreparedStatement getList = connection.prepareStatement(GET_LIST); + getList.setInt(1, id); + System.out.println(getList); + ResultSet getListResults = getList.executeQuery(); + getListResults.first(); + System.out.println(getListResults); + List retrievedList = new List(getListResults); + System.out.println(retrievedList); + PreparedStatement getListEntries = connection.prepareStatement(GET_ENTRIES); + getListEntries.setInt(1, id); + ResultSet getEntryResults = getListEntries.executeQuery(); + while (getEntryResults.next()) { + retrievedList.addItemEntry(new ItemEntry(getEntryResults)); + } + return retrievedList; } finally { connection.close(); } diff --git a/Listify/app/src/main/java/com/example/listify/Requestor.java b/Listify/app/src/main/java/com/example/listify/Requestor.java index 3032c0d..2134ac8 100644 --- a/Listify/app/src/main/java/com/example/listify/Requestor.java +++ b/Listify/app/src/main/java/com/example/listify/Requestor.java @@ -28,6 +28,12 @@ public class Requestor { getObject(id, classType, receiver, null); } + public void getListOfIds(Class ofType, Receiver successHandler, RequestErrorHandler failureHandler) { + String getURL = DEV_BASEURL + "/" + ofType.getSimpleName() + "?list=-1"; + Request postRequest = buildBaseRequest(getURL, "GET", null); + launchCall(postRequest, successHandler, Integer[].class, failureHandler); + } + public void getObject(String id, Class classType, Receiver successHandler, RequestErrorHandler failureHandler) { String getURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id; Request postRequest = buildBaseRequest(getURL, "GET", null); diff --git a/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java b/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java index 7272a3e..74ca055 100644 --- a/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java +++ b/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java @@ -34,6 +34,6 @@ public class SynchronousReceiver implements Requestor.Receiver, Requestor. @Override public void acceptError(IOException error) { waiting = false; - this.error = error + this.error = error; } } From 81b4673766c1fea78523cde26bf61140dd04289c Mon Sep 17 00:00:00 2001 From: NMerz Date: Sat, 3 Oct 2020 23:00:19 -0400 Subject: [PATCH 4/7] Lamdba Integration Test Lamdbas with full pipeline of client through database --- Lambdas/Lists/List/src/ItemEntry.java | 4 +- Lambdas/Lists/List/src/List.java | 26 ++-- Lambdas/Lists/List/src/ListAdder.java | 13 +- Lambdas/Lists/List/src/ListGetter.java | 12 +- .../src/ListEntryAdder.java} | 6 +- .../src/ListEntryDELETE.java} | 4 +- .../src/ListEntryDeleter.java} | 4 +- .../src/ListEntryPOST.java} | 4 +- Lambdas/Lists/src/main/java/BasicHandler.java | 2 +- .../java/com/example/listify/AuthManager.java | 7 ++ .../main/java/com/example/listify/Item.java | 44 ------- .../main/java/com/example/listify/List.java | 8 -- .../com/example/listify/MainActivity.java | 101 +++++++++------ .../java/com/example/listify/Requestor.java | 17 ++- .../java/com/example/listify/data/Item.java | 116 ++++++++++++++++++ .../java/com/example/listify/data/List.java | 70 +++++++++++ .../com/example/listify/data/ListEntry.java | 70 +++++++++++ Tooling/EndpointSetup.sh | 2 +- 18 files changed, 386 insertions(+), 124 deletions(-) rename Lambdas/Lists/{Item/src/ItemAdder.java => ListEntry/src/ListEntryAdder.java} (87%) rename Lambdas/Lists/{Item/src/ItemDELETE.java => ListEntry/src/ListEntryDELETE.java} (57%) rename Lambdas/Lists/{Item/src/ItemDeleter.java => ListEntry/src/ListEntryDeleter.java} (89%) rename Lambdas/Lists/{Item/src/ItemPOST.java => ListEntry/src/ListEntryPOST.java} (57%) delete mode 100644 Listify/app/src/main/java/com/example/listify/Item.java delete mode 100644 Listify/app/src/main/java/com/example/listify/List.java create mode 100644 Listify/app/src/main/java/com/example/listify/data/Item.java create mode 100644 Listify/app/src/main/java/com/example/listify/data/List.java create mode 100644 Listify/app/src/main/java/com/example/listify/data/ListEntry.java diff --git a/Lambdas/Lists/List/src/ItemEntry.java b/Lambdas/Lists/List/src/ItemEntry.java index 513d4ae..f03e910 100644 --- a/Lambdas/Lists/List/src/ItemEntry.java +++ b/Lambdas/Lists/List/src/ItemEntry.java @@ -3,11 +3,13 @@ import java.sql.SQLException; import java.time.LocalDateTime; public class ItemEntry { + Integer listID; Integer productID; Integer quantity; LocalDateTime addedDate; Boolean purchased; - public ItemEntry(ResultSet listRow) throws SQLException { + public ItemEntry(Integer listID, ResultSet listRow) throws SQLException { + this.listID = listID; productID = listRow.getInt(1); quantity = listRow.getInt(2); addedDate = listRow.getObject(3, LocalDateTime.class); diff --git a/Lambdas/Lists/List/src/List.java b/Lambdas/Lists/List/src/List.java index 5f9c4f5..1216c26 100644 --- a/Lambdas/Lists/List/src/List.java +++ b/Lambdas/Lists/List/src/List.java @@ -1,5 +1,6 @@ import java.sql.ResultSet; import java.sql.SQLException; +import java.time.Instant; import java.time.LocalDateTime; import java.util.ArrayList; @@ -7,14 +8,14 @@ public class List { Integer itemID; String name; String owner; - LocalDateTime lastUpdated; + long lastUpdated; ArrayList entries; public List(ResultSet listRow) throws SQLException { - itemID = listRow.getInt(1); - name = listRow.getString(2); - owner = listRow.getString(3); - lastUpdated = listRow.getObject(4, LocalDateTime.class); + itemID = listRow.getInt("listID"); + name = listRow.getString("name"); + owner = listRow.getString("owner"); + lastUpdated = listRow.getTimestamp("lastUpdated").toInstant().toEpochMilli(); entries = new ArrayList<>(); } @@ -22,6 +23,17 @@ public class List { entries.add(entry); } + @Override + public String toString() { + return "List{" + + "itemID=" + itemID + + ", name='" + name + '\'' + + ", owner='" + owner + '\'' + + ", lastUpdated=" + lastUpdated + + ", entries=" + entries + + '}'; + } + public ItemEntry[] getEntries() { return entries.toArray(new ItemEntry[entries.size()]); } @@ -50,11 +62,11 @@ public class List { this.owner = owner; } - public LocalDateTime getLastUpdated() { + public long getLastUpdated() { return lastUpdated; } - public void setLastUpdated(LocalDateTime lastUpdated) { + public void setLastUpdated(long lastUpdated) { this.lastUpdated = lastUpdated; } } diff --git a/Lambdas/Lists/List/src/ListAdder.java b/Lambdas/Lists/List/src/ListAdder.java index eb56e36..011cbc9 100644 --- a/Lambdas/Lists/List/src/ListAdder.java +++ b/Lambdas/Lists/List/src/ListAdder.java @@ -1,6 +1,4 @@ -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; +import java.sql.*; import java.time.Instant; import java.time.ZoneOffset; import java.util.HashMap; @@ -20,13 +18,16 @@ public class ListAdder implements CallHandler { public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { Connection connection = connector.getConnection(); - PreparedStatement statement = connection.prepareStatement(LIST_CREATE); + PreparedStatement statement = connection.prepareStatement(LIST_CREATE, Statement.RETURN_GENERATED_KEYS); statement.setString(1, bodyMap.get("name").toString());//Needs safe checking statement.setString(2, cognitoID); - statement.setObject(3, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime()); + statement.setTimestamp(3, Timestamp.from(Instant.now())); System.out.println(statement); statement.executeUpdate(); + ResultSet newIDRS = statement.getGeneratedKeys(); + newIDRS.first(); + Integer newID = newIDRS.getInt(1); connection.commit(); - return null; + return newID; } } diff --git a/Lambdas/Lists/List/src/ListGetter.java b/Lambdas/Lists/List/src/ListGetter.java index 3e4ff1e..e34366c 100644 --- a/Lambdas/Lists/List/src/ListGetter.java +++ b/Lambdas/Lists/List/src/ListGetter.java @@ -23,13 +23,14 @@ public class ListGetter implements CallHandler{ @Override public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { - Connection connection = connector.getConnection(); - Integer id = Integer.parseInt(queryMap.get("id")); - try { + try (Connection connection = connector.getConnection()) { + Integer id = Integer.parseInt(queryMap.get("id")); if (id == -1) { PreparedStatement getLists = connection.prepareStatement(GET_LISTS); getLists.setString(1, cognitoID); + System.out.println(getLists); ResultSet getListsResults = getLists.executeQuery(); + System.out.println(getListsResults); ArrayList listIds = new ArrayList<>(); while (getListsResults.next()) { listIds.add(getListsResults.getInt(1)); @@ -48,11 +49,10 @@ public class ListGetter implements CallHandler{ getListEntries.setInt(1, id); ResultSet getEntryResults = getListEntries.executeQuery(); while (getEntryResults.next()) { - retrievedList.addItemEntry(new ItemEntry(getEntryResults)); + retrievedList.addItemEntry(new ItemEntry(id, getEntryResults)); } + System.out.println(retrievedList); return retrievedList; - } finally { - connection.close(); } } } diff --git a/Lambdas/Lists/Item/src/ItemAdder.java b/Lambdas/Lists/ListEntry/src/ListEntryAdder.java similarity index 87% rename from Lambdas/Lists/Item/src/ItemAdder.java rename to Lambdas/Lists/ListEntry/src/ListEntryAdder.java index c698a04..2145cd5 100644 --- a/Lambdas/Lists/Item/src/ItemAdder.java +++ b/Lambdas/Lists/ListEntry/src/ListEntryAdder.java @@ -6,14 +6,14 @@ import java.time.ZoneOffset; import java.util.HashMap; import java.util.Map; -public class ItemAdder implements CallHandler { +public class ListEntryAdder implements CallHandler { private DBConnector connector; private String cognitoID; private final String ITEM_TO_LIST = "INSERT INTO ListProduct (productID, listID, quantity, addedDate, purchased) VALUES (?, ?, ?, ?, ?)"; - public ItemAdder(DBConnector connector, String cognitoID) { + public ListEntryAdder(DBConnector connector, String cognitoID) { this.connector = connector; this.cognitoID = cognitoID; } @@ -22,7 +22,7 @@ public class ItemAdder implements CallHandler { Connection connection = connector.getConnection(); try { PreparedStatement statement = connection.prepareStatement(ITEM_TO_LIST); - statement.setInt(1, (Integer) bodyMap.get("itemID")); + statement.setInt(1, (Integer) bodyMap.get("productID")); statement.setInt(2, (Integer) bodyMap.get("listID")); statement.setInt(3, (Integer) bodyMap.get("quantity")); statement.setObject(4, Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime()); diff --git a/Lambdas/Lists/Item/src/ItemDELETE.java b/Lambdas/Lists/ListEntry/src/ListEntryDELETE.java similarity index 57% rename from Lambdas/Lists/Item/src/ItemDELETE.java rename to Lambdas/Lists/ListEntry/src/ListEntryDELETE.java index 7d0f1ee..ad3045d 100644 --- a/Lambdas/Lists/Item/src/ItemDELETE.java +++ b/Lambdas/Lists/ListEntry/src/ListEntryDELETE.java @@ -3,9 +3,9 @@ import com.amazonaws.services.lambda.runtime.RequestHandler; import java.util.Map; -public class ItemDELETE implements RequestHandler, Object> { +public class ListEntryDELETE implements RequestHandler, Object> { public Object handleRequest(Map inputMap, Context unfilled) { - return BasicHandler.handleRequest(inputMap, unfilled, ItemGetter.class); + return BasicHandler.handleRequest(inputMap, unfilled, ListEntryDeleter.class); } } \ No newline at end of file diff --git a/Lambdas/Lists/Item/src/ItemDeleter.java b/Lambdas/Lists/ListEntry/src/ListEntryDeleter.java similarity index 89% rename from Lambdas/Lists/Item/src/ItemDeleter.java rename to Lambdas/Lists/ListEntry/src/ListEntryDeleter.java index ad03a91..af074cd 100644 --- a/Lambdas/Lists/Item/src/ItemDeleter.java +++ b/Lambdas/Lists/ListEntry/src/ListEntryDeleter.java @@ -4,14 +4,14 @@ import java.sql.SQLException; import java.util.HashMap; import java.util.Map; -public class ItemDeleter implements CallHandler { +public class ListEntryDeleter implements CallHandler { private DBConnector connector; private String cognitoID; private final String REMOVE_FROM_LIST = "DELETE FROM ListProduct WHERE (ProductID = ? AND ListID = ?);"; - public ItemDeleter(DBConnector connector, String cognitoID) { + public ListEntryDeleter(DBConnector connector, String cognitoID) { this.connector = connector; this.cognitoID = cognitoID; } diff --git a/Lambdas/Lists/Item/src/ItemPOST.java b/Lambdas/Lists/ListEntry/src/ListEntryPOST.java similarity index 57% rename from Lambdas/Lists/Item/src/ItemPOST.java rename to Lambdas/Lists/ListEntry/src/ListEntryPOST.java index 51d107f..828c43c 100644 --- a/Lambdas/Lists/Item/src/ItemPOST.java +++ b/Lambdas/Lists/ListEntry/src/ListEntryPOST.java @@ -3,9 +3,9 @@ import com.amazonaws.services.lambda.runtime.RequestHandler; import java.util.Map; -public class ItemPOST implements RequestHandler, Object> { +public class ListEntryPOST implements RequestHandler, Object> { public Object handleRequest(Map inputMap, Context unfilled) { - return BasicHandler.handleRequest(inputMap, unfilled, ItemAdder.class); + return BasicHandler.handleRequest(inputMap, unfilled, ListEntryAdder.class); } } diff --git a/Lambdas/Lists/src/main/java/BasicHandler.java b/Lambdas/Lists/src/main/java/BasicHandler.java index 2a7ec6e..8c3e1d3 100644 --- a/Lambdas/Lists/src/main/java/BasicHandler.java +++ b/Lambdas/Lists/src/main/java/BasicHandler.java @@ -24,7 +24,7 @@ public class BasicHandler { } } catch (IOException |SQLException|ClassNotFoundException e) { e.printStackTrace(); - throw new RuntimeException(e.getMessage()); + throw new RuntimeException(e.getLocalizedMessage()); } return null; } diff --git a/Listify/app/src/main/java/com/example/listify/AuthManager.java b/Listify/app/src/main/java/com/example/listify/AuthManager.java index 4e67ed3..030fb88 100644 --- a/Listify/app/src/main/java/com/example/listify/AuthManager.java +++ b/Listify/app/src/main/java/com/example/listify/AuthManager.java @@ -44,6 +44,13 @@ public class AuthManager { } public String getUserToken() { + if (authSession == null) { + try { + fetchAuthSession(); + } catch (AuthException e) { + e.printStackTrace(); + } + } return authSession.getUserPoolTokens().getValue().getIdToken(); } diff --git a/Listify/app/src/main/java/com/example/listify/Item.java b/Listify/app/src/main/java/com/example/listify/Item.java deleted file mode 100644 index f66f8ec..0000000 --- a/Listify/app/src/main/java/com/example/listify/Item.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.example.listify; - -import java.math.BigDecimal; -import java.time.LocalDateTime; - -public class Item { - Integer productID; - Integer chainID; - String upc; - String description; - BigDecimal price; - String imageURL; - String department; - LocalDateTime retrievedDate; - Integer fetchCounts; - - public Item(Integer productID, Integer chainID, String upc, String description, BigDecimal price, - String imageURL, String department, LocalDateTime retrievedDate, Integer fetchCounts) { - this.productID = productID; - this.chainID = chainID; - this.upc = upc; - this.description = description; - this.price = price; - this.imageURL = imageURL; - this.department = department; - this.retrievedDate = retrievedDate; - this.fetchCounts = fetchCounts; - } - - @Override - public String toString() { - return "Item{" + - "productID=" + productID + - ", chainID=" + chainID + - ", upc='" + upc + '\'' + - ", description='" + description + '\'' + - ", price=" + price + - ", imageURL='" + imageURL + '\'' + - ", department='" + department + '\'' + - ", retrievedDate=" + retrievedDate + - ", fetchCounts=" + fetchCounts + - '}'; - } -} diff --git a/Listify/app/src/main/java/com/example/listify/List.java b/Listify/app/src/main/java/com/example/listify/List.java deleted file mode 100644 index 3a828b1..0000000 --- a/Listify/app/src/main/java/com/example/listify/List.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.listify; - -public class List { - String name; - List(String name) { - this.name = name; - } -} diff --git a/Listify/app/src/main/java/com/example/listify/MainActivity.java b/Listify/app/src/main/java/com/example/listify/MainActivity.java index 59c63d4..99ff02d 100644 --- a/Listify/app/src/main/java/com/example/listify/MainActivity.java +++ b/Listify/app/src/main/java/com/example/listify/MainActivity.java @@ -13,11 +13,18 @@ import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.amplifyframework.auth.AuthException; +import com.example.listify.data.Item; +import com.example.listify.data.ListEntry; +import com.example.listify.data.List; import com.google.android.material.navigation.NavigationView; import org.json.JSONException; import java.io.IOException; +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.Arrays; import java.util.Properties; +import java.util.Random; public class MainActivity extends AppCompatActivity { private AppBarConfiguration mAppBarConfiguration; @@ -29,51 +36,73 @@ public class MainActivity extends AppCompatActivity { //------------------------------Auth Testing---------------------------------------------// - AuthManager authManager = new AuthManager(); - try { - authManager.signIn("merzn@purdue.edu", "Password123"); - Log.i("Authentication", authManager.getAuthSession().toString()); - Log.i("Token", authManager.getAuthSession().getUserPoolTokens().getValue().getIdToken()); - } catch (AuthException e) { - Log.i("Authentication", "Login failed. User probably needs to register. Exact error: " + e.getMessage()); + boolean testAuth = false; + + if (testAuth) { + + AuthManager authManager = new AuthManager(); try { - authManager.startSignUp("merzn@purdue.edu", "Password123"); - authManager.confirmSignUp("######"); - } catch (AuthException signUpError) { - Log.e("Authentication", "SignUp error: " + signUpError.getMessage()); + authManager.signIn("merzn@purdue.edu", "Password123"); + Log.i("Authentication", authManager.getAuthSession().toString()); + Log.i("Token", authManager.getAuthSession().getUserPoolTokens().getValue().getIdToken()); + } catch (AuthException e) { + Log.i("Authentication", "Login failed. User probably needs to register. Exact error: " + e.getMessage()); + try { + authManager.startSignUp("merzn@purdue.edu", "Password123"); + authManager.confirmSignUp("######"); + } catch (AuthException signUpError) { + Log.e("Authentication", "SignUp error: " + signUpError.getMessage()); + } } } - //------------------------------------------------------------------------------------------// - + boolean testAPI = true; //----------------------------------API Testing---------------------------------------------// - Properties configs = new Properties(); - try { - configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json"); - } catch (IOException|JSONException e) { - e.printStackTrace(); + if (testAPI) { + AuthManager authManager = new AuthManager(); + try { + authManager.signIn("merzn@purdue.edu", "Password123"); + } catch (AuthException e) { + e.printStackTrace(); + } + Properties configs = new Properties(); + try { + configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json"); + } catch (IOException | JSONException e) { + e.printStackTrace(); + } + + Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey")); + //The name is the only part of this that is used, the rest is generated by the Lambda. + List testList = new List(-1, "New List", "user filled by lambda", Instant.now().toEpochMilli()); + //Everything except addedDate is used for ItemEntry + ListEntry entry = new ListEntry(1, 1, new Random().nextInt(), Instant.now().atZone(ZoneOffset.UTC).toLocalDateTime(),false); + SynchronousReceiver idReceiver = new SynchronousReceiver<>(); + try { + requestor.postObject(testList, idReceiver, idReceiver); + System.out.println(idReceiver.await()); + requestor.postObject(entry); + } catch (JSONException | IOException e) { + e.printStackTrace(); + } + + SynchronousReceiver itemReceiver = new SynchronousReceiver<>(); + requestor.getObject("1", Item.class, itemReceiver, itemReceiver); + SynchronousReceiver listReceiver = new SynchronousReceiver<>(); + requestor.getObject("39", List.class, listReceiver, listReceiver); + SynchronousReceiver listIdsReceiver = new SynchronousReceiver<>(); + requestor.getListOfIds(List.class, listIdsReceiver, listIdsReceiver); + try { + System.out.println(itemReceiver.await()); + System.out.println(listReceiver.await()); + System.out.println(Arrays.toString(listIdsReceiver.await())); + } catch (IOException receiverError) { + receiverError.printStackTrace(); + } } - - Requestor requestor = new Requestor(authManager,configs.getProperty("apiKey")); - List testList = new List("IAmATestList"); - try { - requestor.postObject(testList); - } catch (JSONException e) { - e.printStackTrace(); - } - - SynchronousReceiver receiver = new SynchronousReceiver(); - requestor.getObject("1", Item.class, receiver, receiver); - try { - System.out.println(receiver.await()); - } catch (IOException receiverError) { - receiverError.printStackTrace(); - } - - //------------------------------------------------------------------------------------------// diff --git a/Listify/app/src/main/java/com/example/listify/Requestor.java b/Listify/app/src/main/java/com/example/listify/Requestor.java index 2134ac8..5f34f98 100644 --- a/Listify/app/src/main/java/com/example/listify/Requestor.java +++ b/Listify/app/src/main/java/com/example/listify/Requestor.java @@ -29,7 +29,7 @@ public class Requestor { } public void getListOfIds(Class ofType, Receiver successHandler, RequestErrorHandler failureHandler) { - String getURL = DEV_BASEURL + "/" + ofType.getSimpleName() + "?list=-1"; + String getURL = DEV_BASEURL + "/" + ofType.getSimpleName() + "?id=-1"; Request postRequest = buildBaseRequest(getURL, "GET", null); launchCall(postRequest, successHandler, Integer[].class, failureHandler); } @@ -41,15 +41,22 @@ public class Requestor { } public void postObject(Object toPost) throws JSONException { - postObject(toPost, null); + postObject(toPost, (RequestErrorHandler) null); } public void postObject(Object toPost, RequestErrorHandler failureHandler) throws JSONException { - String postURL = DEV_BASEURL + "/" + toPost.getClass().getSimpleName(); - Request postRequest = buildBaseRequest(postURL, "POST", new Gson().toJson(toPost)); - launchCall(postRequest, null, null, failureHandler); + postObject(toPost, null, failureHandler); } + public void postObject(Object toPost, Receiver idReceiver) throws JSONException { + postObject(toPost, idReceiver, null); + } + + public void postObject(Object toPost, Receiver idReceiver, RequestErrorHandler failureHandler) throws JSONException { + String postURL = DEV_BASEURL + "/" + toPost.getClass().getSimpleName(); + Request postRequest = buildBaseRequest(postURL, "POST", new Gson().toJson(toPost)); + launchCall(postRequest, idReceiver, Integer.class, failureHandler); + } private void launchCall(Request toLaunch, Receiver receiver, Class classType, RequestErrorHandler failureHandler) { client.newCall(toLaunch).enqueue(new Callback() { diff --git a/Listify/app/src/main/java/com/example/listify/data/Item.java b/Listify/app/src/main/java/com/example/listify/data/Item.java new file mode 100644 index 0000000..07662b5 --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/data/Item.java @@ -0,0 +1,116 @@ +package com.example.listify.data; + +import java.math.BigDecimal; +import java.time.LocalDateTime; + +public class Item { + Integer productID; + Integer chainID; + String upc; + String description; + BigDecimal price; + String imageURL; + String department; + LocalDateTime retrievedDate; + Integer fetchCounts; + + public Item(Integer productID, Integer chainID, String upc, String description, BigDecimal price, + String imageURL, String department, LocalDateTime retrievedDate, Integer fetchCounts) { + this.productID = productID; + this.chainID = chainID; + this.upc = upc; + this.description = description; + this.price = price; + this.imageURL = imageURL; + this.department = department; + this.retrievedDate = retrievedDate; + this.fetchCounts = fetchCounts; + } + + @Override + public String toString() { + return "Item{" + + "productID=" + productID + + ", chainID=" + chainID + + ", upc='" + upc + '\'' + + ", description='" + description + '\'' + + ", price=" + price + + ", imageURL='" + imageURL + '\'' + + ", department='" + department + '\'' + + ", retrievedDate=" + (retrievedDate == null ? null : retrievedDate) + + ", fetchCounts=" + fetchCounts + + '}'; + } + + public Integer getProductID() { + return productID; + } + + public void setProductID(Integer productID) { + this.productID = productID; + } + + public Integer getChainID() { + return chainID; + } + + public void setChainID(Integer chainID) { + this.chainID = chainID; + } + + public String getUpc() { + return upc; + } + + public void setUpc(String upc) { + this.upc = upc; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public String getImageURL() { + return imageURL; + } + + public void setImageURL(String imageURL) { + this.imageURL = imageURL; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public LocalDateTime getRetrievedDate() { + return retrievedDate; + } + + public void setRetrievedDate(LocalDateTime retrievedDate) { + this.retrievedDate = retrievedDate; + } + + public Integer getFetchCounts() { + return fetchCounts; + } + + public void setFetchCounts(Integer fetchCounts) { + this.fetchCounts = fetchCounts; + } +} diff --git a/Listify/app/src/main/java/com/example/listify/data/List.java b/Listify/app/src/main/java/com/example/listify/data/List.java new file mode 100644 index 0000000..23d77ea --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/data/List.java @@ -0,0 +1,70 @@ +package com.example.listify.data; + +import java.util.Arrays; + +public class List { + Integer itemID; + String name; + String owner; + long lastUpdated; + final ListEntry[] entries; + + public List(Integer itemID, String name, String owner, long lastUpdated, ListEntry[] entries) { + this.itemID = itemID; + this.name = name; + this.owner = owner; + this.lastUpdated = lastUpdated; + this.entries = entries; + } + + public List(Integer itemID, String name, String owner, long lastUpdated) { + this(itemID, name, owner, lastUpdated, null); + } + + @Override + public String toString() { + return "List{" + + "itemID=" + itemID + + ", name='" + name + '\'' + + ", owner='" + owner + '\'' + + ", lastUpdated=" + lastUpdated + + ", entries=" + Arrays.toString(entries) + + '}'; + } + + public Integer getItemID() { + return itemID; + } + + public void setItemID(Integer itemID) { + this.itemID = itemID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public long getLastUpdated() { + return lastUpdated; + } + + public void setLastUpdated(long lastUpdated) { + this.lastUpdated = lastUpdated; + } + + public ListEntry[] getEntries() { + return entries; + } +} diff --git a/Listify/app/src/main/java/com/example/listify/data/ListEntry.java b/Listify/app/src/main/java/com/example/listify/data/ListEntry.java new file mode 100644 index 0000000..404f649 --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/data/ListEntry.java @@ -0,0 +1,70 @@ +package com.example.listify.data; + +import java.time.LocalDateTime; + +public class ListEntry { + Integer listID; + Integer productID; + Integer quantity; + LocalDateTime addedDate; + Boolean purchased; + + public ListEntry(Integer listID, Integer productID, Integer quantity, LocalDateTime addedDate, Boolean purchased) { + this.listID = listID; + this.productID = productID; + this.quantity = quantity; + this.addedDate = addedDate; + this.purchased = purchased; + } + + @Override + public String toString() { + return "ListEntry{" + + "listID=" + listID + + ", productID=" + productID + + ", quantity=" + quantity + + ", addedDate=" + addedDate + + ", purchased=" + purchased + + '}'; + } + + public Integer getListID() { + return listID; + } + + public void setListID(Integer listID) { + this.listID = listID; + } + + public Integer getProductID() { + return productID; + } + + public void setProductID(Integer productID) { + this.productID = productID; + } + + public Integer getQuantity() { + return quantity; + } + + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } + + public LocalDateTime getAddedDate() { + return addedDate; + } + + public void setAddedDate(LocalDateTime addedDate) { + this.addedDate = addedDate; + } + + public Boolean getPurchased() { + return purchased; + } + + public void setPurchased(Boolean purchased) { + this.purchased = purchased; + } +} diff --git a/Tooling/EndpointSetup.sh b/Tooling/EndpointSetup.sh index 9b8940d..7d72d34 100644 --- a/Tooling/EndpointSetup.sh +++ b/Tooling/EndpointSetup.sh @@ -11,7 +11,7 @@ REL_SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") source ${REL_SCRIPT_DIR}/VarSetup.sh -RAWLAMBDA=$(aws lambda create-function --function-name ${functionName}${method} --zip-file fileb://${jarPath} --runtime ${LANGUAGE} --role ${LAMBDAROLE} --handler ${functionName}${method} 2>${DEBUGFILE}) +RAWLAMBDA=$(aws lambda create-function --function-name ${functionName}${method} --zip-file fileb://${jarPath} --runtime ${LANGUAGE} --role ${LAMBDAROLE} --handler ${functionName}${method} --memory-size 512 2>${DEBUGFILE}) if [[ $? -ne 0 ]]; then From fe846fb81f8f2ba875180bb08d35c5a60b229e0f Mon Sep 17 00:00:00 2001 From: NMerz Date: Mon, 5 Oct 2020 12:29:28 -0400 Subject: [PATCH 5/7] Delete Lambda prototype Prototype the User DELETE lambda. Amplify does not include this functionality, so putting it in a Lambda with the fine-grained API. This currently does not invalidate outstanding tokens nor does it clean up the database. @claytonwwilson You will want to add the clean up code instead of the deleted stuff in UserDeleter --- .../User/resources/cognitoProperties.json | 3 + Lambdas/Lists/User/src/UserDELETE.java | 11 ++++ Lambdas/Lists/User/src/UserDeleter.java | 59 ++++++++++++++++++ Lambdas/Lists/pom.xml | 42 ++++++++++++- Lambdas/Lists/target/Lists-1.0-SNAPSHOT.jar | Bin 0 -> 6620 bytes .../target/maven-archiver/pom.properties | 5 ++ .../compile/default-compile/inputFiles.lst | 4 ++ .../java/com/example/listify/AuthManager.java | 10 +++ .../com/example/listify/MainActivity.java | 7 ++- .../java/com/example/listify/Requestor.java | 22 +++++-- .../java/com/example/listify/data/User.java | 4 ++ 11 files changed, 158 insertions(+), 9 deletions(-) create mode 100644 Lambdas/Lists/User/resources/cognitoProperties.json create mode 100644 Lambdas/Lists/User/src/UserDELETE.java create mode 100644 Lambdas/Lists/User/src/UserDeleter.java create mode 100644 Lambdas/Lists/target/Lists-1.0-SNAPSHOT.jar create mode 100644 Lambdas/Lists/target/maven-archiver/pom.properties create mode 100644 Lambdas/Lists/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst create mode 100644 Listify/app/src/main/java/com/example/listify/data/User.java diff --git a/Lambdas/Lists/User/resources/cognitoProperties.json b/Lambdas/Lists/User/resources/cognitoProperties.json new file mode 100644 index 0000000..a01aefd --- /dev/null +++ b/Lambdas/Lists/User/resources/cognitoProperties.json @@ -0,0 +1,3 @@ +{ + "userPoolId": "us-east-2_MFgSVKQMd", +} \ No newline at end of file diff --git a/Lambdas/Lists/User/src/UserDELETE.java b/Lambdas/Lists/User/src/UserDELETE.java new file mode 100644 index 0000000..b1a7076 --- /dev/null +++ b/Lambdas/Lists/User/src/UserDELETE.java @@ -0,0 +1,11 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class UserDELETE implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, UserDeleter.class); + } +} \ No newline at end of file diff --git a/Lambdas/Lists/User/src/UserDeleter.java b/Lambdas/Lists/User/src/UserDeleter.java new file mode 100644 index 0000000..bd04d9b --- /dev/null +++ b/Lambdas/Lists/User/src/UserDeleter.java @@ -0,0 +1,59 @@ +import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider; +import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProviderClientBuilder; +import com.amazonaws.services.cognitoidp.model.AdminDeleteUserRequest; +import com.amazonaws.services.cognitoidp.model.AdminUserGlobalSignOutRequest; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class UserDeleter implements CallHandler { + + private DBConnector connector; + private String cognitoID; + + //private final String REMOVE_FROM_LIST = "DELETE FROM ListProduct WHERE (ProductID = ? AND ListID = ?);"; + + public UserDeleter(DBConnector connector, String cognitoID) { + this.connector = connector; + this.cognitoID = cognitoID; + } + + public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { + AWSCognitoIdentityProvider awsCognitoIdentityProvider = AWSCognitoIdentityProviderClientBuilder.defaultClient(); + Properties cognitoProperties; + try { + cognitoProperties = DBConnector.loadProperties("cognitoProperties.json"); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + String userPoolId = cognitoProperties.get("userPoolId").toString(); + System.out.println(userPoolId); + AdminDeleteUserRequest adminDeleteUserRequest = new AdminDeleteUserRequest().withUserPoolId(userPoolId); + adminDeleteUserRequest.setUsername(cognitoID); + System.out.println(adminDeleteUserRequest); + awsCognitoIdentityProvider.adminDeleteUser(adminDeleteUserRequest); + AdminUserGlobalSignOutRequest adminUserGlobalSignOutRequest = new AdminUserGlobalSignOutRequest().withUserPoolId(userPoolId); + adminUserGlobalSignOutRequest.setUsername(cognitoID); + System.out.println(adminUserGlobalSignOutRequest); + awsCognitoIdentityProvider.adminUserGlobalSignOut(adminUserGlobalSignOutRequest); + + return null; + + // Connection connection = connector.getConnection(); +// try { +// PreparedStatement statement = connection.prepareStatement(REMOVE_FROM_LIST); +// statement.setInt(1, (Integer) bodyMap.get("ProductID")); +// statement.setInt(2, (Integer) bodyMap.get("ListID")); +// System.out.println(statement); +// statement.executeUpdate(); +// connection.commit(); +// } finally { +// connection.close(); +// } +// return null; + } +} diff --git a/Lambdas/Lists/pom.xml b/Lambdas/Lists/pom.xml index 1984615..9fb5565 100644 --- a/Lambdas/Lists/pom.xml +++ b/Lambdas/Lists/pom.xml @@ -42,13 +42,53 @@ org.apache.httpcomponents httpclient - 4.5 + 4.5.12 com.google.code.gson gson 2.8.6 + + com.amazonaws + aws-java-sdk-cognitoidentity + 1.11.875 + + + com.amazonaws + aws-java-sdk-cognitoidp + 1.11.875 + + + software.amazon.ion + ion-java + 1.5.1 + + + joda-time + joda-time + 2.10.6 + + + commons-logging + commons-logging + 1.2 + + + com.fasterxml.jackson.dataformat + jackson-dataformat-cbor + 2.11.3 + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + 2.8.5 + + + com.fasterxml.jackson.core + jackson-databind + 2.11.3 + 1.11 diff --git a/Lambdas/Lists/target/Lists-1.0-SNAPSHOT.jar b/Lambdas/Lists/target/Lists-1.0-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..a611fefc3c3059bbfccca1c0031a71d3b0e7443a GIT binary patch literal 6620 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 literal 0 HcmV?d00001 diff --git a/Lambdas/Lists/target/maven-archiver/pom.properties b/Lambdas/Lists/target/maven-archiver/pom.properties new file mode 100644 index 0000000..68bd418 --- /dev/null +++ b/Lambdas/Lists/target/maven-archiver/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Mon Oct 05 10:19:24 EDT 2020 +groupId=groupId +artifactId=Lists +version=1.0-SNAPSHOT diff --git a/Lambdas/Lists/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/Lambdas/Lists/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..db1626f --- /dev/null +++ b/Lambdas/Lists/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,4 @@ +/Users/MNM/Documents/GitHub/Listify/Lambdas/Lists/src/main/java/CallHandler.java +/Users/MNM/Documents/GitHub/Listify/Lambdas/Lists/src/main/java/DBConnector.java +/Users/MNM/Documents/GitHub/Listify/Lambdas/Lists/src/main/java/BasicHandler.java +/Users/MNM/Documents/GitHub/Listify/Lambdas/Lists/src/main/java/InputUtils.java diff --git a/Listify/app/src/main/java/com/example/listify/AuthManager.java b/Listify/app/src/main/java/com/example/listify/AuthManager.java index 030fb88..6f87694 100644 --- a/Listify/app/src/main/java/com/example/listify/AuthManager.java +++ b/Listify/app/src/main/java/com/example/listify/AuthManager.java @@ -8,6 +8,7 @@ import com.amplifyframework.auth.options.AuthSignUpOptions; import com.amplifyframework.auth.result.AuthSignInResult; import com.amplifyframework.auth.result.AuthSignUpResult; import com.amplifyframework.core.Amplify; +import com.example.listify.data.User; import org.json.JSONException; import org.json.JSONObject; @@ -85,6 +86,10 @@ public class AuthManager { waiting = false; } + public void signOutSuccess() { + waiting = false; + } + public void startSignUp(String email, String password) throws AuthException { this.email = email; this.password = password; @@ -124,6 +129,11 @@ public class AuthManager { throwIfAuthError(); } + public void deleteUser(Requestor requestor) { + requestor.deleteObject("N/A", User.class); + Amplify.Auth.signOut(this::signOutSuccess, error -> setAuthError(error)); + } + public static Properties loadProperties(Context context, String path) throws IOException, JSONException { diff --git a/Listify/app/src/main/java/com/example/listify/MainActivity.java b/Listify/app/src/main/java/com/example/listify/MainActivity.java index 0e8061b..0e84fdb 100644 --- a/Listify/app/src/main/java/com/example/listify/MainActivity.java +++ b/Listify/app/src/main/java/com/example/listify/MainActivity.java @@ -14,8 +14,8 @@ import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.amplifyframework.auth.AuthException; import com.example.listify.data.Item; -import com.example.listify.data.ListEntry; import com.example.listify.data.List; +import com.example.listify.data.ListEntry; import com.google.android.material.navigation.NavigationView; import org.json.JSONException; @@ -60,7 +60,7 @@ public class MainActivity extends AppCompatActivity { //------------------------------------------------------------------------------------------// - boolean testAPI = false; + boolean testAPI = true; //----------------------------------API Testing---------------------------------------------// if (testAPI) { @@ -78,6 +78,9 @@ public class MainActivity extends AppCompatActivity { } Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey")); + + authManager.deleteUser(requestor); + //The name is the only part of this that is used, the rest is generated by the Lambda. List testList = new List(-1, "New List", "user filled by lambda", Instant.now().toEpochMilli()); //Everything except addedDate is used for ItemEntry diff --git a/Listify/app/src/main/java/com/example/listify/Requestor.java b/Listify/app/src/main/java/com/example/listify/Requestor.java index 5f34f98..4d0c6d1 100644 --- a/Listify/app/src/main/java/com/example/listify/Requestor.java +++ b/Listify/app/src/main/java/com/example/listify/Requestor.java @@ -24,20 +24,30 @@ public class Requestor { client = new OkHttpClient(); } - public void getObject(String id, Class classType, Receiver receiver) { - getObject(id, classType, receiver, null); - } - public void getListOfIds(Class ofType, Receiver successHandler, RequestErrorHandler failureHandler) { String getURL = DEV_BASEURL + "/" + ofType.getSimpleName() + "?id=-1"; Request postRequest = buildBaseRequest(getURL, "GET", null); launchCall(postRequest, successHandler, Integer[].class, failureHandler); } + public void getObject(String id, Class classType, Receiver receiver) { + getObject(id, classType, receiver, null); + } + public void getObject(String id, Class classType, Receiver successHandler, RequestErrorHandler failureHandler) { String getURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id; - Request postRequest = buildBaseRequest(getURL, "GET", null); - launchCall(postRequest, successHandler, classType, failureHandler); + Request getRequest = buildBaseRequest(getURL, "GET", null); + launchCall(getRequest, successHandler, classType, failureHandler); + } + + public void deleteObject(String id, Class classType) { + deleteObject(id, classType, null); + } + + public void deleteObject(String id, Class classType, RequestErrorHandler failureHandler) { + String deleteURL = DEV_BASEURL + "/" + classType.getSimpleName() + "?id=" + id; + Request deleteRequest = buildBaseRequest(deleteURL, "DELETE", "{}"); + launchCall(deleteRequest, null, classType, failureHandler); } public void postObject(Object toPost) throws JSONException { diff --git a/Listify/app/src/main/java/com/example/listify/data/User.java b/Listify/app/src/main/java/com/example/listify/data/User.java new file mode 100644 index 0000000..65ba3e6 --- /dev/null +++ b/Listify/app/src/main/java/com/example/listify/data/User.java @@ -0,0 +1,4 @@ +package com.example.listify.data; + +public class User { +} From 8c526d3b8da43a1ad19fc84e13f3b2a828c3e39e Mon Sep 17 00:00:00 2001 From: NMerz Date: Mon, 5 Oct 2020 22:27:24 -0400 Subject: [PATCH 6/7] SignOut and forced signout on delete Added signout functionality and forced signout on delete. --- Lambdas/Lists/User/src/UserDeleter.java | 9 ++++----- .../java/com/example/listify/AuthManager.java | 15 ++++++++++++--- .../java/com/example/listify/MainActivity.java | 6 +++--- .../main/java/com/example/listify/Requestor.java | 7 +++++-- .../com/example/listify/SynchronousReceiver.java | 10 ++++------ 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Lambdas/Lists/User/src/UserDeleter.java b/Lambdas/Lists/User/src/UserDeleter.java index bd04d9b..14f08ae 100644 --- a/Lambdas/Lists/User/src/UserDeleter.java +++ b/Lambdas/Lists/User/src/UserDeleter.java @@ -32,15 +32,14 @@ public class UserDeleter implements CallHandler { } String userPoolId = cognitoProperties.get("userPoolId").toString(); System.out.println(userPoolId); - AdminDeleteUserRequest adminDeleteUserRequest = new AdminDeleteUserRequest().withUserPoolId(userPoolId); - adminDeleteUserRequest.setUsername(cognitoID); - System.out.println(adminDeleteUserRequest); - awsCognitoIdentityProvider.adminDeleteUser(adminDeleteUserRequest); AdminUserGlobalSignOutRequest adminUserGlobalSignOutRequest = new AdminUserGlobalSignOutRequest().withUserPoolId(userPoolId); adminUserGlobalSignOutRequest.setUsername(cognitoID); System.out.println(adminUserGlobalSignOutRequest); awsCognitoIdentityProvider.adminUserGlobalSignOut(adminUserGlobalSignOutRequest); - + AdminDeleteUserRequest adminDeleteUserRequest = new AdminDeleteUserRequest().withUserPoolId(userPoolId); + adminDeleteUserRequest.setUsername(cognitoID); + System.out.println(adminDeleteUserRequest); + awsCognitoIdentityProvider.adminDeleteUser(adminDeleteUserRequest); return null; // Connection connection = connector.getConnection(); diff --git a/Listify/app/src/main/java/com/example/listify/AuthManager.java b/Listify/app/src/main/java/com/example/listify/AuthManager.java index 6f87694..1248a05 100644 --- a/Listify/app/src/main/java/com/example/listify/AuthManager.java +++ b/Listify/app/src/main/java/com/example/listify/AuthManager.java @@ -50,8 +50,12 @@ public class AuthManager { fetchAuthSession(); } catch (AuthException e) { e.printStackTrace(); + return ""; } } + if (authSession.isSignedIn() == false) { + return ""; + } return authSession.getUserPoolTokens().getValue().getIdToken(); } @@ -129,12 +133,17 @@ public class AuthManager { throwIfAuthError(); } - public void deleteUser(Requestor requestor) { + public void deleteUser(Requestor requestor) throws AuthException { requestor.deleteObject("N/A", User.class); - Amplify.Auth.signOut(this::signOutSuccess, error -> setAuthError(error)); + signOutUser(); } - + public void signOutUser() throws AuthException { + authSession = null; + waiting = true; + Amplify.Auth.signOut(this::signOutSuccess, error -> setAuthError(error)); + throwIfAuthError(); + } public static Properties loadProperties(Context context, String path) throws IOException, JSONException { Properties toReturn = new Properties(); diff --git a/Listify/app/src/main/java/com/example/listify/MainActivity.java b/Listify/app/src/main/java/com/example/listify/MainActivity.java index 0e84fdb..a9f18e6 100644 --- a/Listify/app/src/main/java/com/example/listify/MainActivity.java +++ b/Listify/app/src/main/java/com/example/listify/MainActivity.java @@ -79,7 +79,7 @@ public class MainActivity extends AppCompatActivity { Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey")); - authManager.deleteUser(requestor); + //authManager.deleteUser(requestor); //The name is the only part of this that is used, the rest is generated by the Lambda. List testList = new List(-1, "New List", "user filled by lambda", Instant.now().toEpochMilli()); @@ -90,7 +90,7 @@ public class MainActivity extends AppCompatActivity { requestor.postObject(testList, idReceiver, idReceiver); System.out.println(idReceiver.await()); requestor.postObject(entry); - } catch (JSONException | IOException e) { + } catch (Exception e) { e.printStackTrace(); } @@ -104,7 +104,7 @@ public class MainActivity extends AppCompatActivity { System.out.println(itemReceiver.await()); System.out.println(listReceiver.await()); System.out.println(Arrays.toString(listIdsReceiver.await())); - } catch (IOException receiverError) { + } catch (Exception receiverError) { receiverError.printStackTrace(); } } diff --git a/Listify/app/src/main/java/com/example/listify/Requestor.java b/Listify/app/src/main/java/com/example/listify/Requestor.java index 4d0c6d1..84043f1 100644 --- a/Listify/app/src/main/java/com/example/listify/Requestor.java +++ b/Listify/app/src/main/java/com/example/listify/Requestor.java @@ -82,7 +82,10 @@ public class Requestor { } catch (JsonSyntaxException e) { System.out.println(e); Log.e("API response was not proper JSON", responseString); - throw new JsonSyntaxException(e); + if (failureHandler != null) { + failureHandler.acceptError(e); + } + //throw new JsonSyntaxException(e); } } Log.d("API Response", responseString); @@ -122,6 +125,6 @@ public class Requestor { } public interface RequestErrorHandler { - void acceptError(IOException error); + void acceptError(Exception error); } } diff --git a/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java b/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java index 74ca055..07e390b 100644 --- a/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java +++ b/Listify/app/src/main/java/com/example/listify/SynchronousReceiver.java @@ -1,10 +1,8 @@ package com.example.listify; -import java.io.IOException; - public class SynchronousReceiver implements Requestor.Receiver, Requestor.RequestErrorHandler { private volatile boolean waiting; - private volatile IOException error; + private volatile Exception error; private T toReturn; public SynchronousReceiver() { @@ -12,13 +10,13 @@ public class SynchronousReceiver implements Requestor.Receiver, Requestor. error = null; } - public T await() throws IOException { + public T await() throws Exception { while (waiting) { Thread.yield(); } waiting = true; if (error != null) { - IOException toThrow = error; + Exception toThrow = error; error = null; throw toThrow; } @@ -32,7 +30,7 @@ public class SynchronousReceiver implements Requestor.Receiver, Requestor. } @Override - public void acceptError(IOException error) { + public void acceptError(Exception error) { waiting = false; this.error = error; } From c96a40f0df8486653ad6e450ce64d118d227f5f0 Mon Sep 17 00:00:00 2001 From: NMerz Date: Mon, 5 Oct 2020 22:30:12 -0400 Subject: [PATCH 7/7] Disable examples Examples should be disabled by default --- .../app/src/main/java/com/example/listify/MainActivity.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Listify/app/src/main/java/com/example/listify/MainActivity.java b/Listify/app/src/main/java/com/example/listify/MainActivity.java index a9f18e6..5590ccd 100644 --- a/Listify/app/src/main/java/com/example/listify/MainActivity.java +++ b/Listify/app/src/main/java/com/example/listify/MainActivity.java @@ -55,12 +55,13 @@ public class MainActivity extends AppCompatActivity { } } } - + //NOTE: deleteUser is slightly unusual in that it requires a Requestor. See below for building one + //authManager.deleteUser(requestor); //------------------------------------------------------------------------------------------// - boolean testAPI = true; + boolean testAPI = false; //----------------------------------API Testing---------------------------------------------// if (testAPI) { @@ -79,7 +80,6 @@ public class MainActivity extends AppCompatActivity { Requestor requestor = new Requestor(authManager, configs.getProperty("apiKey")); - //authManager.deleteUser(requestor); //The name is the only part of this that is used, the rest is generated by the Lambda. List testList = new List(-1, "New List", "user filled by lambda", Instant.now().toEpochMilli());