Advertisement
shakasu

Untitled

Feb 25th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. import io.vertx.core.Future;
  2. import io.vertx.core.Promise;
  3. import io.vertx.core.Vertx;
  4. import io.vertx.core.buffer.Buffer;
  5. import io.vertx.core.http.HttpServer;
  6. import io.vertx.core.json.JsonObject;
  7. import io.vertx.ext.web.Router;
  8. import io.vertx.ext.web.client.HttpResponse;
  9. import io.vertx.ext.web.client.WebClient;
  10.  
  11. public class Sandbox {
  12.     final private static int PORT_TDG = 8181;
  13.     final private static int PORT_MY = 8080;
  14.     final private static String HOST = "localhost";
  15.     final private static String TOKEN = "9f226286-e0d8-4125-ad43-b123b4e464da";
  16.     final private static String TYPE = "application/json;charset=utf-8";
  17.  
  18.     public static void main(String[] args) {
  19.         Vertx vertx = Vertx.vertx();
  20.         WebClient client = WebClient.create(vertx);
  21.         Router router = Router.router(vertx);
  22.         Sandbox sandbox = new Sandbox();
  23.         HttpServer server = vertx.createHttpServer();
  24.         JsonObject request = new JsonObject().put("query", "{Book { uuid, book_name, author }}");
  25.  
  26.         router
  27.                 //.get("/get")
  28.                 .post("/post")
  29.  
  30.                 .respond(
  31.                         ctx -> sandbox.graphql(request, client)
  32.                                 .onSuccess(httpResp -> {
  33.                                     if (httpResp.statusCode() == 200) {
  34.                                         ctx.end(httpResp.bodyAsBuffer());
  35.                                     }
  36.                                 })
  37.                         );
  38.  
  39.         server.requestHandler(router).listen(8080);
  40.     }
  41.    
  42.     public Future<HttpResponse<Buffer>> graphql(JsonObject request, WebClient client) {
  43.         Promise<HttpResponse<Buffer>> promise = Promise.promise();
  44.         client
  45.                 .post(PORT_TDG, HOST, "/graphql")
  46.                 .putHeader("Content-Type", "application/json")
  47.                 .sendJson(request, t -> {
  48.                     if (t.succeeded()) {
  49.                         promise.complete(t.result());
  50.                     }
  51.                 });
  52.         return promise.future();
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement