Skip to content

Instantly share code, notes, and snippets.

@alexellis
Last active January 29, 2020 16:16
Show Gist options
  • Save alexellis/1d1cdbd751f64c31ff052d73191638ce to your computer and use it in GitHub Desktop.
Save alexellis/1d1cdbd751f64c31ff052d73191638ce to your computer and use it in GitHub Desktop.
vert-x-client.java

Create a new function:

faas-cli new --lang  java11-vert-x \
  github-release-finder

Replace handler.java

Now run faas-cli up -f github-release-finder.yml

Invoke the function to find the latest release URL for a project on GitHub, without incurring API token rate limiting.

curl -i -d "" http://127.0.0.1:8080/function/github-release-finder; echo
HTTP/1.1 200 OK
Content-Length: 79
Content-Type: application/x-www-form-urlencoded
Date: Wed, 29 Jan 2020 16:15:46 GMT
X-Call-Id: fcf571d2-5b3f-481b-973a-fa19fbf27142
X-Duration-Seconds: 1.075316
X-Start-Time: 1580314545314634425

{
  "releaseUrl" : "https://github.com/openfaas/faas-cli/releases/tag/0.11.7"
}
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
vtest:
lang: java11-vert-x
handler: ./github-release-finder
image: alexellis2/github-release-finder:0.5.3
environment:
owner: openfaas
repo: faas-cli
package com.openfaas.function;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.core.json.JsonObject;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
public class Handler implements BodyHandler {
@Override
public void handle(RoutingContext routingContext) {
WebClientOptions options = new WebClientOptions();
options.setFollowRedirects(false);
WebClient client = WebClient.create(routingContext.vertx(), options);
String repo = System.getenv("repo");
String owner = System.getenv("owner");
client
.head(443, "github.com", "/"+owner+"/"+repo+"/releases/latest")
.ssl(true)
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
System.out.println("Received response with status code " + response.statusCode());
String location = response.getHeader("Location");
routingContext.response()
.putHeader("content-type", "application/json;charset=UTF-8")
.end(
new JsonObject()
.put("releaseUrl", location)
.encodePrettily()
);
} else {
System.out.println("Something went wrong " + ar.cause().getMessage());
routingContext.response()
.putHeader("content-type", "application/json;charset=UTF-8")
.end(
new JsonObject()
.put("releaseUrl", "")
.encodePrettily()
);
}
});
}
@Override
public BodyHandler setBodyLimit(long bodyLimit) {
return null;
}
@Override
public BodyHandler setUploadsDirectory(String uploadsDirectory) {
return null;
}
@Override
public BodyHandler setMergeFormAttributes(boolean mergeFormAttributes) {
return null;
}
@Override
public BodyHandler setDeleteUploadedFilesOnEnd(boolean deleteUploadedFilesOnEnd) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment