REST Plugin API

Setup

You can extend the REST API by adding the RestPlugin interface to your plugin class.

You can now implement two different methods to extend the REST API at different places in the API.

Global Routes

You can hook your plugin routes to the /api/v1/plugins endpoint.

Implementing the createGlobalRouter method as shown below would add the /api/v1/plugins/hello-world/hello endpoint for the plugin with the hello-world restApiName.

@Override
public Router createGlobalRouter() {
    Router router = Router.router(vertx());

    router.route("/hello").handler(rc -> {
        rc.response().end("world");
    });

    return router;
}
The method createGlobalRouter will be invoked multiple times in order to register the endpoints to all request handlers.

Project Routes

Additionally you can also hook your plugin routes to the /api/v1/:projectName/plugins endpoint.

Implementing the createProjectRouter method as shown below would add the /api/v1/:projectName/plugins/hello-world/hello-project endpoint for the plugin to all projects.

@Override
public Router createProjectRouter() {
    Router router = Router.router(vertx());

    router.route("/hello-project").handler(rc -> {
        rc.response().end("world");
    });

    return router;
}
The method createProjectRouter will be invoked multiple times in order to register the endpoints to all request handlers.

Context

The Vert.x RoutingContext contains information which can be accessed by the plugin.

The project context can be wrapped via wrap(rc) to get the PluginContext which makes it possible to access various information.

Table 1. PluginContext methods
Method Description

PluginContext#project()

Return JsonObject which contains project name, uuid.

PluginContext#projectName()

Return the project name.

PluginContext#client()

Return a new client which can be used to access the API . This client will in contrast to MeshPlugin#adminClient() use the user specific client.

Example
router.route("/projectInfo").handler(rc -> {
    PluginContext context = wrap(rc);
    rc.response().end(context.project().encodePrettily());
});

Router API

You can read more about the Vert.x router API in the Vert.x Web documentation.