use of io.vertx.core.http.HttpClientRequest in project okapi by folio-org.
the class OkapiPerformance method useItWithPost.
public void useItWithPost(TestContext context) {
Buffer body = Buffer.buffer();
HttpClientRequest req = httpClient.post(port, "localhost", "/testb", response -> {
context.assertEquals(200, response.statusCode());
String headers = response.headers().entries().toString();
response.handler(x -> {
body.appendBuffer(x);
});
response.endHandler(x -> {
context.assertEquals("Hello (XML) Okapi", body.toString());
declareSample2(context);
});
});
req.headers().add("X-Okapi-Token", okapiToken);
req.putHeader("X-Okapi-Tenant", okapiTenant);
req.putHeader("Content-Type", "text/xml");
req.end("Okapi");
}
use of io.vertx.core.http.HttpClientRequest in project raml-module-builder by folio-org.
the class HttpModuleClient method request.
private void request(HttpMethod method, String endpoint, Map<String, String> headers, boolean cache, Handler<HttpClientResponse> responseHandler, CompletableFuture<Response> cf2) {
if (responseHandler == null) {
CompletableFuture<Response> cf = new CompletableFuture<>();
responseHandler = new HTTPJsonResponseHandler(endpoint, cf);
}
httpClient = vertx.createHttpClient(options);
HttpClientRequest request = null;
if (absoluteHostAddr) {
request = httpClient.requestAbs(method, options.getDefaultHost() + endpoint);
} else {
request = httpClient.request(method, endpoint);
}
request.exceptionHandler(error -> {
Response r = new Response();
r.populateError(endpoint, -1, error.getMessage());
cf2.complete(r);
}).handler(responseHandler);
if (headers != null) {
this.headers.putAll(headers);
}
request.headers().setAll(this.headers);
request.end();
}
use of io.vertx.core.http.HttpClientRequest in project raml-module-builder by folio-org.
the class HttpModuleClient2 method request.
private void request(HttpMethod method, Buffer data, String endpoint, Map<String, String> headers, boolean cache, Handler<HttpClientResponse> responseHandler, CompletableFuture<Response> cf2) {
try {
HttpClientRequest request = null;
if (absoluteHostAddr) {
request = httpClient.requestAbs(method, options.getDefaultHost() + endpoint);
} else {
request = httpClient.request(method, endpoint);
}
request.exceptionHandler(error -> {
Response r = new Response();
r.populateError(endpoint, -1, error.getMessage());
cf2.complete(r);
}).handler(responseHandler);
if (headers != null) {
this.headers.putAll(headers);
}
request.headers().setAll(this.headers);
if (data != null) {
request.end(data);
} else {
request.end();
}
} catch (Exception e) {
Response r = new Response();
r.populateError(endpoint, -1, e.getMessage());
r.exception = e;
cf2.complete(r);
}
}
use of io.vertx.core.http.HttpClientRequest in project raml-module-builder by folio-org.
the class DemoRamlRestTest method postData.
/**
* for POST
*/
private void postData(TestContext context, String url, Buffer buffer, int errorCode, int mode, String contenttype, String tenant, boolean userIdHeader) {
// save stacktrace for async handler
Exception stacktrace = new RuntimeException();
Async async = context.async();
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = null;
if (mode == 0) {
request = client.putAbs(url);
} else if (mode == 1) {
request = client.postAbs(url);
} else {
request = client.deleteAbs(url);
}
request.exceptionHandler(error -> {
async.complete();
context.fail(new RuntimeException(error.getMessage(), stacktrace));
}).handler(response -> {
int statusCode = response.statusCode();
// is it 2XX
log.info(statusCode + ", " + errorCode + " expected status at " + System.currentTimeMillis() + " mode " + mode + " for " + url);
if (statusCode == errorCode) {
if (statusCode == 422) {
String str = response.getHeader("Content-type");
if (str != null && str.contains("application/json")) {
context.assertTrue(true);
} else {
context.fail(new RuntimeException("422 response code should contain a content type header of application/json", stacktrace));
}
} else if (statusCode == 201) {
response.bodyHandler(responseData -> {
String date = (String) new JsonPathParser(responseData.toJsonObject()).getValueAt("metadata.createdDate");
if (date == null && userIdHeader) {
context.fail(new RuntimeException("metaData schema createdDate missing from returned json", stacktrace));
}
});
}
context.assertTrue(true);
} else {
response.bodyHandler(responseData -> {
context.fail(new RuntimeException("got unexpected response code, expected: " + errorCode + ", received code: " + statusCode + " mode " + mode + " for url " + url + "\ndata:" + responseData.toString(), stacktrace));
});
}
if (!async.isCompleted()) {
async.complete();
}
});
request.setChunked(true);
request.putHeader("X-Okapi-Request-Id", "999999999999");
if (tenant != null) {
request.putHeader("x-okapi-tenant", tenant);
}
request.putHeader("Accept", "application/json,text/plain");
if (userIdHeader) {
request.putHeader("X-Okapi-User-Id", "af23adf0-61ba-4887-bf82-956c4aae2260");
}
if (contenttype != null) {
request.putHeader("Content-type", contenttype);
} else {
if (mode == 0 || mode == 2) {
request.putHeader("Content-type", "application/json");
} else {
request.putHeader("Content-type", "multipart/form-data; boundary=MyBoundary");
}
}
if (buffer != null) {
request.write(buffer);
}
request.end();
}
use of io.vertx.core.http.HttpClientRequest in project raml-module-builder by folio-org.
the class DemoRamlRestTest method jobsTest.
/**
* @param context
*/
private void jobsTest(TestContext context) {
String url = "/jobs/jobconfs";
Async async = context.async();
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = null;
request = client.postAbs("http://localhost:" + port + url);
request.exceptionHandler(error -> {
async.complete();
context.fail(error.getMessage());
}).handler(response -> {
int statusCode = response.statusCode();
String location = response.getHeader("Location");
// is it 2XX
log.info(statusCode + " status at " + System.currentTimeMillis() + " for " + "http://localhost:" + port + url);
if (statusCode == 201) {
checkURLs(context, "http://localhost:" + port + url, 200);
try {
postData(context, "http://localhost:" + port + url + "/" + location + "/jobs", Buffer.buffer(getFile("job.json")), 201, 1, "application/json", TENANT, false);
postData(context, "http://localhost:" + port + url + "/" + location, Buffer.buffer(getFile("job_conf_post.json")), 204, 0, null, TENANT, false);
postData(context, "http://localhost:" + port + url + "/12345", Buffer.buffer(getFile("job_conf_post.json")), 404, 2, null, TENANT, false);
postData(context, "http://localhost:" + port + url + "/" + location + "/jobs/12345", Buffer.buffer(getFile("job_conf_post.json")), 404, 2, null, TENANT, false);
} catch (Exception e) {
log.error(e.getMessage(), e);
context.fail();
}
} else {
context.fail("got incorrect response code");
}
if (!async.isCompleted()) {
async.complete();
}
});
request.setChunked(true);
request.putHeader("Accept", "application/json,text/plain");
request.putHeader("Content-type", "application/json");
try {
request.write(getFile("job_conf_post.json"));
} catch (IOException e) {
log.error(e.getMessage(), e);
context.fail("unable to read file");
}
request.end();
}
Aggregations