|
| 1 | +package io.a2a.server.apps.quarkus; |
| 2 | + |
| 3 | +import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE; |
| 4 | +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; |
| 5 | +import static jakarta.ws.rs.core.MediaType.TEXT_PLAIN; |
| 6 | + |
| 7 | +import jakarta.inject.Inject; |
| 8 | +import jakarta.inject.Singleton; |
| 9 | + |
| 10 | +import io.a2a.server.apps.common.TestUtilsBean; |
| 11 | +import io.a2a.spec.Task; |
| 12 | +import io.a2a.util.Utils; |
| 13 | +import io.quarkus.vertx.web.Body; |
| 14 | +import io.quarkus.vertx.web.Param; |
| 15 | +import io.quarkus.vertx.web.Route; |
| 16 | +import io.vertx.ext.web.RoutingContext; |
| 17 | + |
| 18 | +@Singleton |
| 19 | +public class A2ATestRoutes { |
| 20 | + @Inject |
| 21 | + TestUtilsBean testUtilsBean; |
| 22 | + |
| 23 | + @Route(path = "/test/task", methods = {Route.HttpMethod.POST}, consumes = {APPLICATION_JSON}, type = Route.HandlerType.BLOCKING) |
| 24 | + public void saveTask(@Body String body, RoutingContext rc) { |
| 25 | + try { |
| 26 | + Task task = Utils.OBJECT_MAPPER.readValue(body, Task.class); |
| 27 | + testUtilsBean.saveTask(task); |
| 28 | + rc.response() |
| 29 | + .setStatusCode(200) |
| 30 | + .end(); |
| 31 | + } catch (Throwable t) { |
| 32 | + errorResponse(t, rc); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Route(path = "/test/task/:taskId", methods = {Route.HttpMethod.GET}, produces = {APPLICATION_JSON}, type = Route.HandlerType.BLOCKING) |
| 37 | + public void getTask(@Param String taskId, RoutingContext rc) { |
| 38 | + try { |
| 39 | + Task task = testUtilsBean.getTask(taskId); |
| 40 | + if (task == null) { |
| 41 | + rc.response() |
| 42 | + .setStatusCode(404) |
| 43 | + .end(); |
| 44 | + return; |
| 45 | + } |
| 46 | + rc.response() |
| 47 | + .setStatusCode(200) |
| 48 | + .putHeader(CONTENT_TYPE, APPLICATION_JSON) |
| 49 | + .end(Utils.OBJECT_MAPPER.writeValueAsString(task)); |
| 50 | + |
| 51 | + } catch (Throwable t) { |
| 52 | + errorResponse(t, rc); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Route(path = "/test/task/:taskId", methods = {Route.HttpMethod.DELETE}, type = Route.HandlerType.BLOCKING) |
| 57 | + public void deleteTask(@Param String taskId, RoutingContext rc) { |
| 58 | + try { |
| 59 | + Task task = testUtilsBean.getTask(taskId); |
| 60 | + if (task == null) { |
| 61 | + rc.response() |
| 62 | + .setStatusCode(404) |
| 63 | + .end(); |
| 64 | + return; |
| 65 | + } |
| 66 | + testUtilsBean.deleteTask(taskId); |
| 67 | + rc.response() |
| 68 | + .setStatusCode(200) |
| 69 | + .putHeader(CONTENT_TYPE, APPLICATION_JSON) |
| 70 | + .end(); |
| 71 | + } catch (Throwable t) { |
| 72 | + errorResponse(t, rc); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void errorResponse(Throwable t, RoutingContext rc) { |
| 77 | + rc.response() |
| 78 | + .setStatusCode(200) |
| 79 | + .putHeader(CONTENT_TYPE, TEXT_PLAIN) |
| 80 | + .end(); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments