forked from servertap-io/servertap
-
Notifications
You must be signed in to change notification settings - Fork 0
Examples of sync, async endpoints
Horokh Bohdan edited this page Oct 19, 2025
·
1 revision
You are gonna need this in your class:
private final CatWalkWebserverService webserverService;
public CatwalkApiExample(CatWalkWebserverService webserverService) {
this.webserverService = webserverService;
// Initialize CatwalkAPI with the current Bearer token
CatwalkAPI.setBearerToken(webserverService.getAuthKey());
}Then to create simple sync request:
@OpenApi(
path = "/api-example/sync-request",
methods = HttpMethod.POST,
summary = "Make a synchronous HTTP request",
description = "Demonstrates making a synchronous HTTP request using CatwalkAPI",
requestBody = @io.javalin.openapi.OpenApiRequestBody(
content = @OpenApiContent(from = RequestExample.class)
),
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = ApiResponse.class)),
@OpenApiResponse(status = "400", description = "Bad Request"),
@OpenApiResponse(status = "500", description = "Internal Server Error")
},
tags = {"API Example"}
)
@BridgeEventHandler(
description = "Make a synchronous HTTP request using CatwalkAPI",
requiresAuth = true,
logRequests = true
)
public void makeSyncRequest(Context ctx) {
try {
RequestExample request = ctx.bodyAsClass(RequestExample.class);
ApiResponse response;
switch (request.method().toUpperCase()) {
case "GET":
response = CatwalkAPI.sendGet(request.url(), request.headers());
break;
case "POST":
response = CatwalkAPI.sendPost(request.url(), request.body(), request.headers());
break;
case "PUT":
response = CatwalkAPI.sendPut(request.url(), request.body(), request.headers());
break;
case "DELETE":
response = CatwalkAPI.sendDelete(request.url(), request.headers());
break;
default:
response = CatwalkAPI.sendRequest(request.url(), request.method(), request.body(), request.headers());
}
ctx.json(response);
} catch (Exception e) {
ctx.status(400).json(Map.of("error", "Invalid request", "message", e.getMessage()));
}
}Yet in order to make it async, just make the argument use Context. This way you will have access to internal Javalin methods, and can make it use ctx.future, ctx.thenAccept, or whatever you need right now. In this case, you do not need to return anything, so the method should be void. The response is handled manually:
@OpenApi(
path = "/api-example/async-request",
methods = HttpMethod.POST,
summary = "Make an asynchronous HTTP request",
description = "Demonstrates making an asynchronous HTTP request using CatwalkAPI",
requestBody = @io.javalin.openapi.OpenApiRequestBody(
content = @OpenApiContent(from = RequestExample.class)
),
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = ApiResponse.class)),
@OpenApiResponse(status = "400", description = "Bad Request"),
@OpenApiResponse(status = "500", description = "Internal Server Error")
},
tags = {"API Example"}
)
@BridgeEventHandler(
description = "Make an asynchronous HTTP request using CatwalkAPI",
requiresAuth = true,
logRequests = true
)
public void makeAsyncRequest(Context ctx) {
try {
RequestExample request = ctx.bodyAsClass(RequestExample.class);
CompletableFuture<ApiResponse> futureResponse;
switch (request.method().toUpperCase()) {
case "GET":
futureResponse = CatwalkAPI.sendGetAsync(request.url());
break;
case "POST":
futureResponse = CatwalkAPI.sendPostAsync(request.url(), request.body());
break;
default:
futureResponse = CatwalkAPI.sendRequestAsync(request.url(), request.method(), request.body(), request.headers());
}
// Handle the async response
ctx.future(() -> futureResponse
.thenApply(response -> {
ctx.json(response);
return null;
})
.exceptionally(error -> {
ctx.status(500).json(Map.of("error", error.getMessage()));
return null;
})
);
ctx.json(Map.of("message", "Async request initiated", "status", "pending"));
} catch (Exception e) {
ctx.status(400).json(Map.of("error", "Invalid request", "message", e.getMessage()));
}
}Simple get endpoint example, using CatwalkAPI for making HTTP requests:
@OpenApi(
path = "/api-example/simple-get",
methods = HttpMethod.GET,
summary = "Make a simple GET request",
description = "Demonstrates making a simple GET request to httpbin.org",
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = ApiResponse.class)),
@OpenApiResponse(status = "500", description = "Internal Server Error")
},
tags = {"API Example"}
)
@BridgeEventHandler(
description = "Make a simple GET request demonstration",
requiresAuth = true
)
public void makeSimpleGet(Context ctx) {
try {
// Make a simple GET request to httpbin.org (a testing service)
ApiResponse response = CatwalkAPI.sendGet("https://httpbin.org/get");
ctx.json(response);
} catch (Exception e) {
ctx.status(500).json(Map.of("error", "Request failed", "message", e.getMessage()));
}
}Example request structure for the API endpoints:
public record RequestExample(
String url,
String method,
String body,
Map<String, String> headers
) {
public RequestExample {
if (headers == null) {
headers = Map.of();
}
}
}
}