Creating anonymous snippets

We first need a web client object to issue HTTP requests to the Gist API:

  1. webClient = WebClient.create(vertx, new WebClientOptions()
  2. .setSsl(true)
  3. .setUserAgent("vert-x3"));
Tip
Since requests are made using HTTPS, we need to configure the web client with SSL support.
Tip
We override the default user agent with vert-x3 but you may opt to use your own value instead.

We then modify the web router configuration in the HttpServerVerticle class to add a new route for triggering backups:

  1. router.get("/backup").handler(this::backupHandler);

The code for this handler is the following:

  1. private void backupHandler(RoutingContext context) {
  2. dbService.fetchAllPagesData(reply -> {
  3. if (reply.succeeded()) {
  4. JsonArray filesObject = new JsonArray();
  5. JsonObject payload = new JsonObject() (1)
  6. .put("files", filesObject)
  7. .put("language", "plaintext")
  8. .put("title", "vertx-wiki-backup")
  9. .put("public", true);
  10. reply
  11. .result()
  12. .forEach(page -> {
  13. JsonObject fileObject = new JsonObject(); (2)
  14. fileObject.put("name", page.getString("NAME"));
  15. fileObject.put("content", page.getString("CONTENT"));
  16. filesObject.add(fileObject);
  17. });
  18. webClient.post(443, "snippets.glot.io", "/snippets") (3)
  19. .putHeader("Content-Type", "application/json")
  20. .as(BodyCodec.jsonObject()) (4)
  21. .sendJsonObject(payload, ar -> { (5)
  22. if (ar.succeeded()) {
  23. HttpResponse<JsonObject> response = ar.result();
  24. if (response.statusCode() == 200) {
  25. String url = "https://glot.io/snippets/" + response.body().getString("id");
  26. context.put("backup_gist_url", url); (6)
  27. indexHandler(context);
  28. } else {
  29. StringBuilder message = new StringBuilder()
  30. .append("Could not backup the wiki: ")
  31. .append(response.statusMessage());
  32. JsonObject body = response.body();
  33. if (body != null) {
  34. message.append(System.getProperty("line.separator"))
  35. .append(body.encodePrettily());
  36. }
  37. LOGGER.error(message.toString());
  38. context.fail(502);
  39. }
  40. } else {
  41. Throwable err = ar.cause();
  42. LOGGER.error("HTTP Client error", err);
  43. context.fail(err);
  44. }
  45. });
  46. } else {
  47. context.fail(reply.cause());
  48. }
  49. });
  50. }
  1. The snippet creation request payload is a JSON document as outlined in the service API documentation.

  2. Each file is an entry under the files object of the payload, with a title and content.

  3. The web client needs to issue a POST request on port 443 (HTTPS), and the path must be /snippets.

  4. The BodyCodec class provides a helper to specify that the response will be directly converted to a Vert.x JsonObject instance. It is also possible to use BodyCodec#json(Class<T>) and the JSON data will be mapped to a Java object of type T (this uses Jackson data mapping under the hood).

  5. sendJsonObject is a helper for triggering the HTTP request with a JSON payload.

  6. Upon success we can get the snippet identifier, and construct a URL to the user-friendly web representation.