mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
Merge pull request #144 from nosqlbench/http-script-plugin
http plugin for scripts
This commit is contained in:
commit
03a1ac88d9
@ -0,0 +1,59 @@
|
|||||||
|
package io.nosqlbench.engine.extensions.http;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
|
||||||
|
public class HttpPlugin {
|
||||||
|
private HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
|
public HttpResponse<String> get(String url) throws IOException, InterruptedException {
|
||||||
|
HttpRequest.Builder builder = HttpRequest.newBuilder();
|
||||||
|
URI uri = URI.create(url);
|
||||||
|
HttpRequest request = builder
|
||||||
|
.uri(uri)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request,
|
||||||
|
HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse<String> post(String url) throws IOException, InterruptedException {
|
||||||
|
return post(url, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse<String> post(String url, String data, String contentType) throws IOException, InterruptedException {
|
||||||
|
HttpRequest.Builder builder = HttpRequest.newBuilder();
|
||||||
|
URI uri = URI.create(url);
|
||||||
|
|
||||||
|
HttpRequest request;
|
||||||
|
if (data == null && contentType == null || contentType == null){
|
||||||
|
request = builder
|
||||||
|
.uri(uri)
|
||||||
|
.POST(HttpRequest.BodyPublishers.noBody())
|
||||||
|
.build();
|
||||||
|
} else if (data == null) {
|
||||||
|
request = builder
|
||||||
|
.uri(uri)
|
||||||
|
.header("Content-Type", contentType)
|
||||||
|
.POST(HttpRequest.BodyPublishers.noBody())
|
||||||
|
.build();
|
||||||
|
} else {
|
||||||
|
request = builder
|
||||||
|
.uri(uri)
|
||||||
|
.header("Content-Type", contentType)
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(data))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request,
|
||||||
|
HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package io.nosqlbench.engine.extensions.http;
|
||||||
|
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import io.nosqlbench.engine.api.extensions.ScriptingPluginInfo;
|
||||||
|
import io.nosqlbench.engine.extensions.optimizers.BobyqaOptimizerPlugin;
|
||||||
|
import io.nosqlbench.nb.annotations.Service;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import javax.script.ScriptContext;
|
||||||
|
|
||||||
|
@Service(ScriptingPluginInfo.class)
|
||||||
|
public class HttpPluginData implements ScriptingPluginInfo<HttpPlugin> {
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "use http get and post in scripts";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpPlugin getExtensionObject(Logger logger, MetricRegistry metricRegistry, ScriptContext scriptContext) {
|
||||||
|
return new HttpPlugin();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getBaseVariableName() {
|
||||||
|
return "http";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user