Skip to content

Commit

Permalink
integrate date support
Browse files Browse the repository at this point in the history
  • Loading branch information
Conor Flynn committed Feb 7, 2023
1 parent 19bd0a3 commit dbcbd1e
Show file tree
Hide file tree
Showing 14 changed files with 445 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,59 @@ public ResponseEntity<String> handleRequest(

return new ResponseEntity<String>("", HttpStatus.OK);
}

@PostMapping
@CrossOrigin
@RequestMapping(path = {"/request-dated"})
public ResponseEntity<String> handleRequestDated(
@RequestParam(name="name", required=true) String name,
@RequestParam(name="properties", required=false) String properties,
@RequestParam(name="headers", required=false) String headers,
@RequestParam(name="path",required=false) String path,
@RequestParam(name="startDate",required=true) String start_date,
@RequestParam(name="endDate",required=true) String end_date) {

// assert that request exists
if(!RequestManager.hasRequestFormat(name))
return new ResponseEntity<String>(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<String, String> properties_map = new HashMap<String, String>();
HashMap<String, String> headers_map = new HashMap<String, String>();
String[] path_map = new String[] {};

if(properties != null) {
String[] arr = properties.split(",");
if(arr.length % 2 != 0)
return new ResponseEntity<String>("Properties must be in the format of <key, value> 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<String>("Headers must be in the format of <key, value> pairs.", HttpStatus.PRECONDITION_FAILED);

for(int i = 0; i < arr.length; i+=2)
headers_map.put(arr[i], arr[i + 1]);
}

if(path != null) {
path_map = path.split(",");
if(path_map.length % 2 != 0)
return new ResponseEntity<String>("Path must be in the format of <key, value> pairs.", HttpStatus.PRECONDITION_FAILED);
}

// submit request
String response = request.request(path_map, properties_map, headers_map, start_date, end_date);
if(response != null)
return new ResponseEntity<String>(response, HttpStatus.SERVICE_UNAVAILABLE);

return new ResponseEntity<String>("", HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.stream.external.request.core;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.stream.Stream;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -14,6 +18,9 @@
import okhttp3.Response;

public abstract class RequestFramework {

private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

private final String name;
private String url;
private final String[] url_path;
Expand All @@ -23,6 +30,11 @@ public abstract class RequestFramework {
private final String[] recursive_location;
private final String recursive_replacement;
private final String[] path;
private final boolean is_dated;
private final String date_location;
private final String date_start_var;
private final String date_end_var;
private final DateTimeFormatter date_format;

// default constructor used for templating
public RequestFramework() {
Expand All @@ -35,10 +47,16 @@ public RequestFramework() {
this.recursive_location = null;
this.recursive_replacement = null;
this.path = null;
this.is_dated = false;
this.date_location = null;
this.date_start_var = null;
this.date_end_var = null;
this.date_format = null;
}

public RequestFramework(String name, String url, String[] url_path, HashMap<String, String> properties, HashMap<String, String> headers,
HashMap<String, String> tags, String[] recursive_location, String recursive_replacement, String[] path) {
HashMap<String, String> tags, String[] recursive_location, String recursive_replacement, String[] path,
boolean is_dated, String date_location, String date_start_var, String date_end_var, String date_format) {
this.name = name;
this.url = url;
this.url_path = url_path;
Expand All @@ -48,6 +66,43 @@ public RequestFramework(String name, String url, String[] url_path, HashMap<Stri
this.recursive_location = recursive_location;
this.recursive_replacement = recursive_replacement;
this.path = path;
this.is_dated = is_dated;
this.date_location = date_location;
this.date_start_var = date_start_var;
this.date_end_var = date_end_var;
this.date_format = DateTimeFormatter.ofPattern(date_format);
}

public final boolean hasTag(String tag) {
return this.tags.containsKey(tag);
}

public final String getTag(String tag) {
return this.tags.get(tag);
}

public final HashMap<String, String> getTags() {
return this.tags;
}

public final String getName() {
return name;
}

public final String getUrl() {
return url;
}

public final String[] getRecursiveLocation() {
return recursive_location;
}

public final String getRecursiveReplacement() {
return recursive_replacement;
}

public final String[] getPath() {
return path;
}

protected final Request getRequest(HashMap<String, String> properties, HashMap<String, String> headers) {
Expand Down Expand Up @@ -109,40 +164,9 @@ protected final Request getRequest(String url, HashMap<String, String> propertie
return builder.build();
}

public final boolean hasTag(String tag) {
return this.tags.containsKey(tag);
}

public final String getTag(String tag) {
return this.tags.get(tag);
}

public final HashMap<String, String> getTags() {
return this.tags;
}

public final String getName() {
return name;
}

public final String getUrl() {
return url;
}

public final String[] getRecursiveLocation() {
return recursive_location;
}

public final String getRecursiveReplacement() {
return recursive_replacement;
}

public final String[] getPath() {
return path;
}

public final synchronized String request(String[] url_path, HashMap<String, String> properties, HashMap<String, String> headers) {
//
// validate that url_path is correctly formatted
if(url_path.length % 2 != 0) {
System.err.println("Url path is not formatted properly and must be in <key, value> pairs.");
return "Url path is not formatted properly and must be in <key, value> pairs.";
Expand Down Expand Up @@ -170,6 +194,83 @@ public final synchronized String request(String[] url_path, HashMap<String, Stri
return process(url_builder.toString(), properties, headers);
}

public final synchronized String request(String[] url_path, HashMap<String, String> properties, HashMap<String, String> headers,
String startDate, String endDate) {

// validate that the request can be dated
if(!is_dated) {
System.err.println("Request cannot be dated.");
return "Request cannot be dated.";
}

// parse passed dates
LocalDate start, end;
try {
start = LocalDate.parse(startDate, formatter);
end = LocalDate.parse(endDate, formatter);
} catch (DateTimeParseException e) {
return "Unable to parse dates.";
}

if(start == null || end == null) {
System.err.println("Fatal error parsing dates.");
return "Fatal error parsing dates.";
}

// retrieve all dates in-between
Stream<LocalDate> dates = null;

try {
dates = start.datesUntil(end);
} catch(Exception e) {
System.err.println("End date must be after start date.");
return "End date must be after start date.";
}

// submit requests for each date
Iterator<LocalDate> itr = dates.iterator();
while(itr.hasNext()) {
// retrieve date
LocalDate date = itr.next();

// retrieve start date var and update to location
switch(date_location) {
case "properties":
properties.put(date_start_var, date.format(date_format));
break;
case "headers":
headers.put(date_start_var, date.format(date_format));
break;
default:
System.err.println("Invalid date.location parameter value.");
System.exit(1);
}

// if end date is required add one day and push to properties
if(!date_end_var.equals(".")) {
LocalDate tmr = date.plusDays(1);
switch(date_location) {
case "properties":
properties.put(date_end_var, tmr.format(date_format));
break;
case "headers":
headers.put(date_end_var, tmr.format(date_format));
break;
default:
System.err.println("Invalid date.location parameter value.");
System.exit(1);
}
}

// submit request with updated properties
String request = request(url_path, properties, headers);
if(request != null)
return request;
}

return null;
}

protected String process(String url, HashMap<String, String> properties, HashMap<String, String> headers) {
HashMap<String, String> all_properties = new HashMap<String, String>();
HashMap<String, String> all_headers = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public static void initialize() throws InstantiationException, IllegalAccessExce
String[] recursive_location = {};
String recursive_replacement = null;
String[] path = {};
boolean is_dated = false;
String date_location = null;
String date_start_var = null;
String date_end_var = null;
String date_format = null;

// parse name
if(p.containsKey("request.name")) {
Expand Down Expand Up @@ -142,21 +147,69 @@ public static void initialize() throws InstantiationException, IllegalAccessExce
recursive_replacement = p.getProperty("recursive.replacement");
}

// push to named object types
// parse date valid
if(p.containsKey("date.valid")) {
try {
is_dated = Boolean.parseBoolean(p.getProperty("date.valid"));
} catch(Exception e) {
System.err.println("Property date.valid is not of type boolean.");
System.exit(1);
}
} else {
System.err.println(String.format("Missing required property <%s>.", "date.valid"));
System.exit(1);
}

// parse date location
if(p.containsKey("date.location")) {
date_location = p.getProperty("date.location");
} else if(!p.containsKey("date.location") && is_dated) {
System.err.println("Missing required property date.location if date.valid=true.");
System.exit(1);
}

// parse date start
if(p.containsKey("date.start")) {
date_start_var = p.getProperty("date.start");
} else if(!p.containsKey("date.start") && is_dated) {
System.err.println("Missing required property date.start if date.valid=true.");
System.exit(1);
}

// parse date end
if(p.containsKey("date.end")) {
date_end_var = p.getProperty("date.end");
} else if(!p.containsKey("date.end") && is_dated) {
System.err.println("Missing required property date.end if date.valid=true.");
System.exit(1);
}

// parse date format
if(p.containsKey("date.format")) {
date_format = p.getProperty("date.format");
} else if(!p.containsKey("date.format") && is_dated) {
System.err.println("Missing required property date.format if date.valid=true.");
System.exit(1);
}

// validate templates contains proper key
if(!templates.containsKey(recursive_type)) {
System.err.println(String.format("Template not found for <%s>.", recursive_type));
System.exit(1);
}

// validate that duplicate request doesn't exist
if(RequestManager.requests.containsKey(name)) {
System.err.println(String.format("Duplicate name found <%s>. Please modify one of the names to be unique.", name));
System.exit(1);
}

RequestManager.requests.put(name, templates.get(recursive_type).getDeclaredConstructor(
String.class, String.class, String[].class, HashMap.class, HashMap.class,
HashMap.class, String[].class, String.class, String[].class).newInstance(
name, url, url_path, properties, headers, tags, recursive_location, recursive_replacement, path));
HashMap.class, String[].class, String.class, String[].class,
boolean.class, String.class, String.class, String.class, String.class).newInstance(
name, url, url_path, properties, headers, tags, recursive_location, recursive_replacement, path,
is_dated, date_location, date_start_var, date_end_var, date_format));

System.out.println(String.format("Successfully added type [%s]", name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public RequestParameterized() {
}

public RequestParameterized(String name, String url, String[] url_path, HashMap<String, String> properties, HashMap<String, String> headers,
HashMap<String, String> tags, String[] recursive_location, String recursive_replacement, String[] path) {
super(name, url, url_path, properties, headers, tags, recursive_location, recursive_replacement, path);
HashMap<String, String> tags, String[] recursive_location, String recursive_replacement, String[] path,
boolean is_dated, String date_location, String date_start_var, String date_end_var, String date_format) {
super(name, url, url_path, properties, headers, tags, recursive_location, recursive_replacement, path,
is_dated, date_location, date_start_var, date_end_var, date_format);
}

@Override
public String getType() {
return "parameterized";
return "parameterized";
}

public String handle(String json, HashMap<String, String> properties, HashMap<String, String> headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void main(String[] args) {

RequestParameterized request = new RequestParameterized("test",
"https://web3api.io/api/v2/addresses", new String[]{}, properties, headers, tags,
recursive_location, recursive_replacement, path);
recursive_location, recursive_replacement, path, false, "", "", "", "");

request.request(new String[] {}, properties, headers);
}
Expand Down
Loading

0 comments on commit dbcbd1e

Please sign in to comment.