Skip to content

Commit

Permalink
allow specifying http timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
aeberhart committed Mar 18, 2024
1 parent fc87423 commit 0420ab6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions dashjoin-core/src/main/java/org/dashjoin/function/RestJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Base64;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import org.dashjoin.model.JsonSchema;
import org.dashjoin.service.JSONDatabase;
import org.dashjoin.util.MapUtil;
Expand All @@ -20,8 +21,8 @@
* calls a JSON REST service
*/
@SuppressWarnings("rawtypes")
@JsonSchema(required = {"url"},
order = {"url", "username", "password", "method", "contentType", "headers", "returnText"})
@JsonSchema(required = {"url"}, order = {"url", "username", "password", "method", "contentType",
"headers", "returnText", "timeoutSeconds"})
public class RestJson extends AbstractConfigurableFunction<Object, Object> {

private static final ObjectMapper objectMapper = new ObjectMapper();
Expand Down Expand Up @@ -61,6 +62,9 @@ public class RestJson extends AbstractConfigurableFunction<Object, Object> {
@JsonSchema(title = "Return the result as raw text")
public Boolean returnText;

@JsonSchema(title = "Optional HTTP timeout (s)")
public Integer timeoutSeconds;

/**
* returns the result of the REST call with JSON mapped to a Map / List. If arg is specified,
* POSTs the arg serialized as JSON. If arg is null, GETs the result.
Expand All @@ -70,6 +74,8 @@ public class RestJson extends AbstractConfigurableFunction<Object, Object> {
public Object run(Object obj) throws Exception {
Map map = obj instanceof Map ? (Map) obj : MapUtil.of();
OkHttpClient client = Doc2data.getHttpClient();
if (this.timeoutSeconds != null)
client = client.newBuilder().readTimeout(this.timeoutSeconds, TimeUnit.SECONDS).build();
String sv = map == null ? url : (String) Template.replace(url, map, true);
Request.Builder request = new Request.Builder().url(sv);
if (username != null)
Expand Down

0 comments on commit 0420ab6

Please sign in to comment.