From 5b89139c45018bdc7a7303b25ca02bc6f277639f Mon Sep 17 00:00:00 2001 From: Conor Flynn Date: Mon, 6 Feb 2023 09:43:43 -0500 Subject: [PATCH] integrate basic api for api-handler --- .../application/apihandler/Controller.java | 45 ++++++++++++++++++- .../request/core/RequestFramework.java | 45 +++++++++++++------ .../external/request/core/RequestManager.java | 6 ++- .../request/types/RequestParameterized.java | 30 ++++++------- 4 files changed, 94 insertions(+), 32 deletions(-) diff --git a/DeFi-Data-Engine/Api-Handler/src/main/java/org/application/apihandler/Controller.java b/DeFi-Data-Engine/Api-Handler/src/main/java/org/application/apihandler/Controller.java index cefe6e7f..d4749c80 100644 --- a/DeFi-Data-Engine/Api-Handler/src/main/java/org/application/apihandler/Controller.java +++ b/DeFi-Data-Engine/Api-Handler/src/main/java/org/application/apihandler/Controller.java @@ -3,13 +3,16 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URISyntaxException; +import java.util.HashMap; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.stream.external.request.core.RequestFramework; import org.stream.external.request.core.RequestManager; import jakarta.annotation.PostConstruct; @@ -31,7 +34,45 @@ public void initialize() { @PostMapping @CrossOrigin @RequestMapping(path = {"/request"}) - public ResponseEntity handleRequest(@RequestParam String name) { - return null; + public ResponseEntity handleRequest( + @RequestParam(name="name", required=true) String name, + @RequestParam(name="properties", required=false) String properties, + @RequestParam(name="headers", required=false) String headers) { + + // assert that request exists + if(!RequestManager.hasRequestFormat(name)) + return new ResponseEntity(String.format("Request name <%s> does not exist.", name), HttpStatus.UNPROCESSABLE_ENTITY); + + // retrieve request framework + RequestFramework request = RequestManager.getRequestFormat(name); + + // parse properties and headers + HashMap properties_map = new HashMap(); + HashMap headers_map = new HashMap(); + + if(properties != null) { + String[] arr = properties.split(","); + if(arr.length % 2 != 0) + return new ResponseEntity("Properties must be in the format of pairs.", HttpStatus.PRECONDITION_FAILED); + + for(int i = 0; i < arr.length; i+=2) + properties_map.put(arr[i], arr[i + 1]); + } + + if(headers != null) { + String[] arr = headers.split(","); + if(arr.length % 2 != 0) + return new ResponseEntity("Headers must be in the format of pairs.", HttpStatus.PRECONDITION_FAILED); + + for(int i = 0; i < arr.length; i+=2) + headers_map.put(arr[i], arr[i + 1]); + } + + // submit request + String response = request.request(properties_map, headers_map); + if(response != null) + return new ResponseEntity(response, HttpStatus.SERVICE_UNAVAILABLE); + + return new ResponseEntity("", HttpStatus.OK); } } diff --git a/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/core/RequestFramework.java b/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/core/RequestFramework.java index 8b4ca9c6..ef572867 100644 --- a/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/core/RequestFramework.java +++ b/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/core/RequestFramework.java @@ -151,49 +151,66 @@ public final String[] getPath() { return path; } - @SuppressWarnings("unchecked") - public final void request(HashMap properties, HashMap headers) { - // clone maps and push to process - HashMap cloned_properties = (HashMap) properties.clone(); - HashMap cloned_headers = (HashMap) headers.clone(); + public final synchronized String request(HashMap properties, HashMap headers) { + // create new maps and add all properties and headers + HashMap properties_combined = new HashMap(); + HashMap headers_combined = new HashMap(); - process(url, cloned_properties, cloned_headers); + // add all default properties + for(String key : this.properties.keySet()) + properties_combined.put(key, this.properties.get(key)); + + for(String key : this.headers.keySet()) + headers_combined.put(key, this.headers.get(key)); + + // add all defined properties + for(String key : properties.keySet()) + properties_combined.put(key, properties.get(key)); + + for(String key : headers.keySet()) + headers_combined.put(key, headers.get(key)); + + return process(url, properties_combined, headers_combined); } - protected void process(String url, HashMap properties, HashMap headers) { + protected String process(String url, HashMap properties, HashMap headers) { OkHttpClient client = new OkHttpClient(); Request request = getRequest(url, properties, headers); if(request == null) { System.err.println("Malformed request, killing process."); - return; + return "Malformed request, killing process."; } Response response = null; + String body = null; try { response = client.newCall(request).execute(); + body = response.body().string().toString(); if(response.code() != 200) { - System.err.println(String.format("Request Failure code <%d> url <%s>\nmsg <%s>", response.code(), request.url().toString(), response.message())); - System.exit(1); + System.err.println(String.format("Request Failure code <%d> url <%s>\nbody:\n%s", response.code(), request.url().toString(), body)); + return String.format("Request Failure code <%d> url <%s>\nbody:\n%s", response.code(), request.url().toString(), body); } } catch (IOException e) { e.printStackTrace(); } - if(response == null) { + if(body == null) { System.err.println("Response had fatal issue, killing process."); - return; + return "Response had fatal issue, killing process."; } // send to specific request handler try { - String body = response.body().string().toString(); handle(body, properties, headers); } catch(Exception e) { e.printStackTrace(); + return e.toString(); } + + return null; } - protected abstract void handle(String json, HashMap properties, HashMap headers); + protected abstract String handle(String json, HashMap properties, HashMap headers); public abstract String getType(); diff --git a/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/core/RequestManager.java b/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/core/RequestManager.java index 2f3dbb89..0e206a02 100644 --- a/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/core/RequestManager.java +++ b/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/core/RequestManager.java @@ -152,7 +152,11 @@ public static void initialize() throws InstantiationException, IllegalAccessExce } } + public static final boolean hasRequestFormat(String name) { + return requests.containsKey(name); + } + public static final RequestFramework getRequestFormat(String name) { return requests.get(name); } -} +} \ No newline at end of file diff --git a/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/types/RequestParameterized.java b/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/types/RequestParameterized.java index 796b43f9..7fc16d21 100644 --- a/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/types/RequestParameterized.java +++ b/DeFi-Data-Engine/Api-Handler/src/main/java/org/stream/external/request/types/RequestParameterized.java @@ -23,7 +23,7 @@ public String getType() { return "parameterized"; } - public void handle(String json, HashMap properties, HashMap headers) { + public String handle(String json, HashMap properties, HashMap headers) { // parse json formatting JSONObject obj = new JSONObject(json); @@ -31,13 +31,13 @@ public void handle(String json, HashMap properties, HashMap")); - return; + return "Missing required recursive parameter <-l>"; } // check for required tag -t if(!hasTag("-t")) { System.err.println(String.format("Missing required recursive parameter <-t>")); - return; + return "Missing required recursive parameter <-t>"; } // check for -l being an integer @@ -47,7 +47,7 @@ public void handle(String json, HashMap properties, HashMap must be an integer.")); - return; + return "Value following <-l> must be an integer."; } // validate that recursion location is valid @@ -62,7 +62,7 @@ public void handle(String json, HashMap properties, HashMap properties, HashMap. Cannot parse."); - return; + return "obj path type is not of type . Cannot parse."; } } } @@ -105,7 +105,7 @@ else if(obj.has(path[i])) { obj = obj.getJSONObject(path[i]); } catch(Exception e) { System.err.println("obj path type step is not of type . Cannot parse."); - return; + return "obj path type step is not of type . Cannot parse."; } } } @@ -113,7 +113,7 @@ else if(obj.has(path[i])) { // validate that data is non-empty if(data == null) { System.err.println("Data array retrieval had fatal error, killing process."); - return; + return "Data array retrieval had fatal error, killing process."; } // extract and print data @@ -125,7 +125,7 @@ else if(obj.has(path[i])) { // initiate recursive call // if under limit requested, terminate call if(data.length() < limit) - return; + return null; // extract recursive parameter and apply to next call String rec_type = getTag("-t"); @@ -150,13 +150,13 @@ else if(obj.has(path[i])) { param = Integer.parseInt(recursive_parameter); } catch(Exception e) { e.printStackTrace(); - System.err.println(String.format("Recursive parameter <%s> is not of type integer.", getTag("-rp"))); - return; + System.err.println(String.format("Recursive parameter <%s> is not of type integer.", recursive_parameter)); + return String.format("Recursive parameter <%s> is not of type integer.", recursive_parameter); } if(param == -1) { System.err.println("Fatal parsing error occured."); - return; + return "Fatal parsing error occured."; } param += 1; @@ -176,6 +176,6 @@ else if(obj.has(path[i])) { } // increment param, replace, and execute - process(url, properties, headers); + return process(url, properties, headers); } } \ No newline at end of file