Skip to content

Commit

Permalink
Update api-handler for request stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Conor Flynn committed Feb 1, 2023
1 parent f9e124d commit 2a98f6a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,72 +1,87 @@
package org.stream.external.requester;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import okhttp3.Request;
import okhttp3.Request.Builder;

public abstract class RequestFramework {
private final String url;
private final HashSet<String> properties;
private final HashMap<String, String> properties;
private final HashMap<String, String> headers;
private final String[] tags;

public RequestFramework(String url, HashSet<String> properties, HashMap<String, String> headers, String... tags) {
public RequestFramework(String url, HashMap<String, String> properties, HashMap<String, String> headers, String... tags) {
this.url = url;
this.properties = properties;
this.headers = headers;
this.tags = tags;
}

public final Request getRequest(HashMap<String, String> properties, HashMap<String, String> headers) {
Set<String> property_keys = properties.keySet();
Set<String> header_keys = headers.keySet();
Iterator<String> var6 = this.properties.iterator();

while (var6.hasNext()) {
String property = (String) var6.next();
if (!properties.containsKey(property)) {
System.err.println(String.format("Missing required property <%s>", property));
return null;
}
}

Set<String> req_header_keys = this.headers.keySet();
Iterator<String> var7 = req_header_keys.iterator();

while (var7.hasNext()) {
String header = (String) var7.next();
if (!header_keys.contains(header)) {
System.err.println(String.format("Missing required header <%s>", header));
return null;
}
}

HashMap<String, String> all_properties = new HashMap<String, String>();
HashMap<String, String> all_headers = new HashMap<String, String>();

// add all properties and headers
for(String property : this.properties.keySet())
all_properties.put(property, this.properties.get(property));

for(String header : this.headers.keySet())
all_headers.put(header, this.headers.get(header));

for(String property : properties.keySet())
all_properties.put(property, properties.get(property));

for(String header : headers.keySet())
all_headers.put(header, headers.get(header));

// define builder
Builder builder = new Builder();
StringBuilder url_builder = new StringBuilder(this.url);
if (!property_keys.isEmpty()) {
if(!all_properties.isEmpty())
url_builder.append("?");
}

Iterator<String> properties_itr = property_keys.iterator();

String key;
while (properties_itr.hasNext()) {
key = (String) properties_itr.next();
url_builder.append(String.format("%s=%s", key, properties.get(key)));
if (properties_itr.hasNext()) {
url_builder.append("&");

// add all required/optional properties
for(String property : all_properties.keySet()) {
String value = all_properties.get(property);

// check if empty
if(value == null || value.equals("")) {
System.err.println(String.format("Property cannot be empty <%s>", property));
return null;
}

// check if required
if(value.equals(".") && this.properties.containsKey(property)) {
System.err.println(String.format("Required property <%s> not defined.", property));
return null;
}

// add to url
url_builder.append(String.format("%s=%s&", property, value));
}


// remove final & and update builder
url_builder.deleteCharAt(url_builder.length() - 1);
builder = builder.url(url_builder.toString());
Iterator<String> var10 = header_keys.iterator();

while (var10.hasNext()) {
key = (String) var10.next();
builder.addHeader(key, (String) headers.get(key));

// add all headers
for(String header : all_headers.keySet()) {
String value = all_headers.get(header);

// check if empty
if(value == null || value.equals("")) {
System.err.println(String.format("Header cannot be empty <%s>", header));
return null;
}

// check if required
if(value.equals(".") && this.properties.containsKey(header)) {
System.err.println(String.format("Required header <%s> not defined.", header));
return null;
}

builder = builder.addHeader(header, value);
}

return builder.build();
Expand All @@ -76,5 +91,5 @@ public final String[] getTags() {
return this.tags;
}

public abstract void process(Request var1);
public abstract void process(Request request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ url.headers= header1,value1,\
# which have specific properties and handlers. Please review documentation to get a full
# list of these tags. To default with no recursive call, set this property to <single>.
# This property we will set to <parameterized> for a clearer example.
url.recursion.type=parameterized
url.recursion.type= parameterized

# This property sets all tags pertaining to the type of recursive call. Please refer to
# the documentation for the full list of all tags and specified recursive types. All tags
Expand All @@ -44,4 +44,15 @@ url.recursion.type=parameterized
# -rp: recursive parameter
# -l: limit on items from request
# -t: type of parameter (url or incremental)
# FINISH
url.recursion.tags= -rp,next,\
-l,1000,\
-t,url

# This property sets the location of the data points to be retrieved from the call. This
# should be a JSONArray which the handler can iterate through. To access these data points
# directly, the direct path must be specified (consisting of all JSONObject values). In
# the example below, we point to the path located at response->data. Note that for storing
# all non-array values and just recording all base values returned by the call, please set
# the value of this variable to '-b'. (i.e. url.data.path=-b)
url.data.path= response,\
data
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ url.headers= header1,value1,\
# which have specific properties and handlers. Please review documentation to get a full
# list of these tags. To default with no recursive call, set this property to <single>.
# This property we will set to <parameterized> for a clearer example.
url.recursion.type=parameterized
url.recursion.type= parameterized

# This property sets all tags pertaining to the type of recursive call. Please refer to
# the documentation for the full list of all tags and specified recursive types. All tags
Expand All @@ -44,4 +44,15 @@ url.recursion.type=parameterized
# -rp: recursive parameter
# -l: limit on items from request
# -t: type of parameter (url or incremental)
# FINISH
url.recursion.tags= -rp,next,\
-l,1000,\
-t,url

# This property sets the location of the data points to be retrieved from the call. This
# should be a JSONArray which the handler can iterate through. To access these data points
# directly, the direct path must be specified (consisting of all JSONObject values). In
# the example below, we point to the path located at response->data. Note that for storing
# all non-array values and just recording all base values returned by the call, please set
# the value of this variable to '-b'. (i.e. url.data.path=-b)
url.data.path= response,\
data

0 comments on commit 2a98f6a

Please sign in to comment.