diff --git a/DeFi-Data-Engine/DeFi Data Engine/src/test/java/test/stream/external/connected/connections/TestAmberDataConnection.java b/DeFi-Data-Engine/DeFi Data Engine/src/test/java/test/stream/external/connected/connections/TestAmberDataConnection.java new file mode 100644 index 00000000..536182a5 --- /dev/null +++ b/DeFi-Data-Engine/DeFi Data Engine/src/test/java/test/stream/external/connected/connections/TestAmberDataConnection.java @@ -0,0 +1,85 @@ +package test.stream.external.connected.connections; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Scanner; + +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.core.core.Core; +import org.framework.router.Response; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestAmberDataConnection { + +static { + // disable loggers + LogManager.getRootLogger().setLevel(Level.OFF); +} + + private static String apikey; + + @BeforeClass + public static void init() throws FileNotFoundException { + Scanner s = new Scanner(new File("src/test/resources/" + + "test/stream/external/connected/connections/" + + "test_amberdata_config.txt")); + apikey = s.nextLine(); + s.close(); + } + + //@Test + public void TestAuthorization() { + Core core = new Core(); + + // successful authorization + assertEquals(200, core.send("SRC", "INIT", "amber_data," + apikey).code()); + + // failed authorization + assertEquals(422, core.send("SRC", "INIT", "amber_data, does_not_exist").code()); + } + + //@Test + public void TestRequest() { + Core core = new Core(); + + Response initResponse = core.send("SRC", "INIT", "amber_data," + apikey); + + assertEquals(200, initResponse.code()); + + String key = initResponse.data().split(",")[1].trim(); + + Response valid = core.send("SRC", "RQST", String.format("%s, %s", key, "lending-latest")); + assertEquals(200, valid.code()); + + // invalid request + Response invalid = core.send("SRC", "RQST", String.format("%s, %s", key, "does_not_exist")); + assertEquals(428, invalid.code()); + } + + @Test + public void TestAaveProtocolDated() throws IOException { +// HashMap params = new HashMap(); +// params.put("start_date", "2022-09-01"); +// params.put("end_date", "2022-09-05"); +// System.out.println(AmberDataRequestHandler.requestAaveProtocolDated("2022-09-01", "2022-09-05", "UAK7ed69235426c360be22bfc2bde1809b6")[1]); + //System.out.println(AmberDataRequestHandler.requestLendingLatest(apikey, "")[1]); + //System.out.println(AmberDataRequestHandler.request("aave-protocol-dated", apikey, params)[1]); + +// OkHttpClient client = new OkHttpClient(); +// +// Request request = new Request.Builder() +// .url("https://web3api.io/api/v2/defi/lending/aavev2/protocol?startDate=2022-09-01T01:00:00&endDate=2022-09-05T01:00:00") +// .get() +// .addHeader("accept", "application/json") +// .addHeader("x-api-key", "UAK7ed69235426c360be22bfc2bde1809b6") +// .build(); +// +// okhttp3.Response response = client.newCall(request).execute(); +// System.out.println(response.body().string().substring(0, 1000)); + } +} diff --git a/R-Code-Samples/DataEnginePrimaryFunctions.Rmd b/R-Code-Samples/DataEnginePrimaryFunctions.Rmd index 8717c71e..0d375bca 100644 --- a/R-Code-Samples/DataEnginePrimaryFunctions.Rmd +++ b/R-Code-Samples/DataEnginePrimaryFunctions.Rmd @@ -112,6 +112,9 @@ request <- function(protocol, properties = "", headers = "", startdate = "", end else if (grepl("<<>>", temp, fixed=TRUE)) { temp <- readLines(socket, 1) + while(grepl("<<>>", temp, fixed=TRUE)) { + temp <- readLines(socket, 1) + } response <- fromJSON(temp) break } @@ -122,8 +125,13 @@ request <- function(protocol, properties = "", headers = "", startdate = "", end print(paste("Processed", counter, "lines for", protocol)) # add data point line to data frame - temp.df <- as.data.frame(fromJSON(temp)) - df <- rbind.fill(df, temp.df) + tryCatch(expr={ + temp.df <- as.data.frame(fromJSON(temp)) + df <- rbind.fill(df, temp.df) + }, + error=function(e){ + message("Heartbeat execption caught and not parsed.") + }) } output <- list("response"=response, "df"=df) diff --git a/R-Code-Samples/ExampleUserClusteringStarter.Rmd b/R-Code-Samples/ExampleUserClusteringStarter.Rmd index 0b767a6f..c571bd10 100644 --- a/R-Code-Samples/ExampleUserClusteringStarter.Rmd +++ b/R-Code-Samples/ExampleUserClusteringStarter.Rmd @@ -130,6 +130,9 @@ request <- function(protocol, properties = "", headers = "", startdate = "", end else if (grepl("<<>>", temp, fixed=TRUE)) { temp <- readLines(socket, 1) + while(grepl("<<>>", temp, fixed=TRUE)) { + temp <- readLines(socket, 1) + } response <- fromJSON(temp) break } @@ -140,8 +143,13 @@ request <- function(protocol, properties = "", headers = "", startdate = "", end print(paste("Processed", counter, "lines for", protocol)) # add data point line to data frame - temp.df <- as.data.frame(fromJSON(temp)) - df <- rbind.fill(df, temp.df) + tryCatch(expr={ + temp.df <- as.data.frame(fromJSON(temp)) + df <- rbind.fill(df, temp.df) + }, + error=function(e){ + message("Heartbeat execption caught and not parsed.") + }) } output <- list("response"=response, "df"=df) diff --git a/R-Code-Samples/GetTransactions.Rmd b/R-Code-Samples/GetTransactions.Rmd index 83f20a14..15667edf 100644 --- a/R-Code-Samples/GetTransactions.Rmd +++ b/R-Code-Samples/GetTransactions.Rmd @@ -114,6 +114,9 @@ request <- function(protocol, properties = "", headers = "", startdate = "", end else if (grepl("<<>>", temp, fixed=TRUE)) { temp <- readLines(socket, 1) + while(grepl("<<>>", temp, fixed=TRUE)) { + temp <- readLines(socket, 1) + } response <- fromJSON(temp) break } @@ -124,8 +127,13 @@ request <- function(protocol, properties = "", headers = "", startdate = "", end print(paste("Processed", counter, "lines for", protocol)) # add data point line to data frame - temp.df <- as.data.frame(fromJSON(temp)) - df <- rbind.fill(df, temp.df) + tryCatch(expr={ + temp.df <- as.data.frame(fromJSON(temp)) + df <- rbind.fill(df, temp.df) + }, + error=function(e){ + message("Heartbeat execption caught and not parsed.") + }) } output <- list("response"=response, "df"=df)