Using QBit microservice libs HttpClient GET POST et al JSON Java 8 Lambda

Using QBit microservice libs HttpClient GET POST et al JSON Java 8 Lambda


Using QBit microservice libs HttpClient GET, POST, et al, JSON, Java 8 Lambda


QBit ships with an HttpClient lib. The libs tries to make it easy to make REST style calls. The HttpClient lib is geared towards HTTP and JSON.

Why?
I have been on projects where I just wanted to performance test my service. I had a hard time finding and HTTP lib that could perform, and when I did find one it was hard to use. This HTTP Client is a labor of love. I want the ability to quickly write load tests and to be able to write wrappers and routers around existing (much slower) services. This combines high-speed HTTP client with Java 8 lambdas. 
The nice thing about the lib is that is allows for Java 8 style lambdas for GET, POST and WebSocket calls.
Before we start up our HTTP client lets start up a server to echo back what we send the server.
Starting up an HTTP server
 /* Create an HTTP server. */
HttpServer httpServer = httpServerBuilder()
.setPort(8080).build();

/* Setting up a request Consumer with Java 8 Lambda expression. */
httpServer.setHttpRequestConsumer(httpRequest -> {

Map<String, Object> results = new HashMap<>();
results.put("method", httpRequest.getMethod());
results.put("uri", httpRequest.getUri());
results.put("body", httpRequest.getBodyAsString());
results.put("headers", httpRequest.getHeaders());
results.put("params", httpRequest.getParams());
httpRequest.getReceiver()
.response(200, "application/json", Boon.toJson(results));
});


/* Start the server. */
httpServer.start();

Starting HTTP client

Now, lets try out our HTTP client.
Starting up an HTTP client
 /* Setup an httpClient. */
HttpClient httpClient = httpClientBuilder()
.setHost("localhost").setPort(8080).build();
httpClient.start();
You just pass the URL, the port and then call start.

Synchronous HTTP calls

Now you can start sending HTTP requests.
No Param HTTP GET
 /* Send no param get. */
HttpResponse httpResponse = httpClient.get( "/hello/mom" );
puts( httpResponse );
An HTTP response just contains the results from the server.
No Param HTTP Response
public interface HttpResponse {

MultiMap<String, String> headers();

int code();

String contentType();

String body();

}
There are helper methods for sync HTTP GET calls.
Helper methods for GET
 /* Send one param get. */
httpResponse = httpClient.getWith1Param("/hello/singleParam",
"hi", "mom");
puts("single param", httpResponse );


/* Send two param get. */
httpResponse = httpClient.getWith2Params("/hello/twoParams",
"hi", "mom", "hello", "dad");
puts("two params", httpResponse );


/* Send two param get. */
httpResponse = httpClient.getWith3Params("/hello/3params",
"hi", "mom",
"hello", "dad",
"greetings", "kids");
puts("three params", httpResponse );


/* Send four param get. */
httpResponse = httpClient.getWith4Params("/hello/4params",
"hi", "mom",
"hello", "dad",
"greetings", "kids",
"yo", "pets");
puts("4 params", httpResponse );

/* Send five param get. */
httpResponse = httpClient.getWith5Params("/hello/5params",
"hi", "mom",
"hello", "dad",
"greetings", "kids",
"yo", "pets",
"hola", "neighbors");
puts("5 params", httpResponse );

The puts method is a helper method it does System.out.println more or less by the way.
The first five params are covered. Beyond five, you have to use the HttpBuilder.
 /* Send six params with get. */

final HttpRequest httpRequest = httpRequestBuilder()
.addParam("hi", "mom")
.addParam("hello", "dad")
.addParam("greetings", "kids")
.addParam("yo", "pets")
.addParam("hola", "pets")
.addParam("salutations", "all").build();

httpResponse = httpClient.sendRequestAndWait(httpRequest);
puts("6 params", httpResponse );

Http Async

There are async calls for GET as well.

Async calls for HTTP GET using Java 8 lambda

 /* Using Async support with lambda. */
httpClient.getAsync("/hi/async", (code, contentType, body) -> {
puts("Async text with lambda", body);
});

Sys.sleep(100);


/* Using Async support with lambda. */
httpClient.getAsyncWith1Param("/hi/async", "hi", "mom", (code, contentType, body) -> {
puts("Async text with lambda 1 param ", body);
});

Sys.sleep(100);



/* Using Async support with lambda. */
httpClient.getAsyncWith2Params("/hi/async",
"p1", "v1",
"p2", "v2",
(code, contentType, body) -> {
puts("Async text with lambda 2 params ", body);
});

Sys.sleep(100);




/* Using Async support with lambda. */
httpClient.getAsyncWith3Params("/hi/async",
"p1

download file now

Popular posts from this blog

UTAUloid Voicebank Download Utane Uta Defoko

Video Mesum Ariel Dengan Wulan Guritno