Advertisement
shakasu

Untitled

Feb 20th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. import io.vertx.core.Vertx;
  2. import io.vertx.core.http.HttpServer;
  3. import io.vertx.core.http.HttpServerResponse;
  4. import io.vertx.core.json.JsonObject;
  5. import io.vertx.core.net.SocketAddress;
  6. import io.vertx.ext.web.client.WebClient;
  7.  
  8. public class TrashMigrator {
  9.     final private static int PORT_TDG = 8181;
  10.     final private static int PORT_MY = 8080;
  11.     final private static String HOST = "localhost";
  12.     final private static String TOKEN = "9f226286-e0d8-4125-ad43-b123b4e464da";
  13.     final private static String TYPE = "application/json;charset=utf-8";
  14.  
  15.     public void get(WebClient client) {
  16.         client
  17.                 .post(PORT_TDG, HOST, "/graphql")
  18.                 .putHeader("Content-Type", "application/json")
  19.                 .sendJsonObject(
  20.                         new JsonObject()
  21.                                 .put("query", "{Book { uuid, book_name, author }}")
  22.                 )
  23.                 .onSuccess(res -> {
  24.                     if (res.statusCode() == 200 &&
  25.                             res.getHeader("content-type").equals(TYPE)) {
  26.                         JsonObject body = res.bodyAsJsonObject();
  27.                         body.stream();
  28.                         post(client, body);
  29.                     }
  30.                 });
  31.     }
  32.  
  33.     public void post(WebClient client, JsonObject body) {
  34.         client
  35.                 .post(PORT_TDG, HOST, "/http")
  36.                 .putHeader("Content-Type", TYPE)
  37.                 .putHeader("auth-token", TOKEN)
  38.                 .sendJsonObject(body)
  39.                 .onSuccess(res -> {
  40.                     //todo
  41.                 });
  42.     }
  43.  
  44.     public void run(HttpServer server, WebClient client) {
  45.         server.requestHandler(request -> {
  46.             HttpServerResponse response = request.response();
  47.             get(client);
  48.         });
  49.         server.listen(SocketAddress.domainSocketAddress(String.format("%s/%s//api/migrate/graphql", HOST, PORT_MY)));
  50.     }
  51.  
  52.     public static void main(String[] args) throws Exception {
  53.         Vertx vertx = Vertx.vertx();
  54.         WebClient client = WebClient.create(vertx);
  55.         TrashMigrator migrator = new TrashMigrator();
  56.         HttpServer server = vertx.createHttpServer();
  57.         migrator.run(server, client);
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement