|
|
@@ -23,7 +23,7 @@ import java.util.Map; |
|
|
|
import static java.util.concurrent.TimeUnit.SECONDS; |
|
|
|
import static org.cobbzilla.util.daemon.ZillaRuntime.*; |
|
|
|
import static org.cobbzilla.util.http.HttpSchemes.isHttpOrHttps; |
|
|
|
import static org.cobbzilla.util.json.JsonUtil.json; |
|
|
|
import static org.cobbzilla.util.json.JsonUtil.*; |
|
|
|
import static org.cobbzilla.util.system.Sleep.sleep; |
|
|
|
import static org.cobbzilla.util.time.TimeUtil.parseDuration; |
|
|
|
import static org.cobbzilla.wizard.client.script.ApiRunner.standardHandlebars; |
|
|
@@ -38,6 +38,8 @@ public class SimpleApiRunnerListener extends ApiRunnerListenerBase { |
|
|
|
public static final String AWAIT_URL = "await_url"; |
|
|
|
public static final String VERIFY_UNREACHABLE = "verify_unreachable"; |
|
|
|
public static final String RESPONSE_VAR = "await_json"; |
|
|
|
public static final String ECHO_IN_LOG = "echo_in_log "; |
|
|
|
public static final String ADD_TO_CTX = "add_to_ctx "; |
|
|
|
|
|
|
|
public static final long DEFAULT_AWAIT_URL_CHECK_INTERVAL = SECONDS.toMillis(10); |
|
|
|
public static final long DEFAULT_VERIFY_UNAVAILABLE_TIMEOUT = SECONDS.toMillis(10); |
|
|
@@ -65,6 +67,10 @@ public class SimpleApiRunnerListener extends ApiRunnerListenerBase { |
|
|
|
handleAwaitUrl(before, ctx); |
|
|
|
} else if (before.startsWith(VERIFY_UNREACHABLE)) { |
|
|
|
handleVerifyUnreachable(before, ctx); |
|
|
|
} else if (before.startsWith(ECHO_IN_LOG)) { |
|
|
|
handleEcho(before, ctx); |
|
|
|
} else if (before.startsWith(ADD_TO_CTX)) { |
|
|
|
handleAddToCtx(before, ctx); |
|
|
|
} else { |
|
|
|
super.beforeScript(before, ctx); |
|
|
|
} |
|
|
@@ -83,6 +89,10 @@ public class SimpleApiRunnerListener extends ApiRunnerListenerBase { |
|
|
|
handleAwaitUrl(after, ctx); |
|
|
|
} else if (after.startsWith(VERIFY_UNREACHABLE)) { |
|
|
|
handleVerifyUnreachable(after, ctx); |
|
|
|
} else if (after.startsWith(ECHO_IN_LOG)) { |
|
|
|
handleEcho(after, ctx); |
|
|
|
} else if (after.startsWith(ADD_TO_CTX)) { |
|
|
|
handleAddToCtx(after, ctx); |
|
|
|
} else { |
|
|
|
super.afterScript(after, ctx); |
|
|
|
} |
|
|
@@ -177,6 +187,22 @@ public class SimpleApiRunnerListener extends ApiRunnerListenerBase { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@NonNull private String handleEcho(@NonNull final String arg, @NonNull final Map<String, Object> ctx) { |
|
|
|
final var parts = arg.split("\\s+", 2); |
|
|
|
if (parts.length != 2) return die(ECHO_IN_LOG + ": no variables specified"); |
|
|
|
final var output = HandlebarsUtil.apply(getHandlebars(), parts[1], ctx); |
|
|
|
log.info("ECHO:\n" + output); |
|
|
|
return output; |
|
|
|
} |
|
|
|
|
|
|
|
@NonNull private Map<String, Object> handleAddToCtx(@NonNull final String arg, |
|
|
|
@NonNull final Map<String, Object> ctx) { |
|
|
|
final var parts = arg.split("\\s+", 2); |
|
|
|
if (parts.length != 2) return die(ADD_TO_CTX + ": no variables specified"); |
|
|
|
ctx.putAll(fromJsonOrDie(parts[1], Map.class)); |
|
|
|
return ctx; |
|
|
|
} |
|
|
|
|
|
|
|
private String formatUrl(String url) { |
|
|
|
final ApiClientBase currentApi = currentApi(); |
|
|
|
if (!isHttpOrHttps(url)) { |
|
|
|