Custom API endpoint which I can use in my plugin

Hi everyone,

I am developing a Citations plugin for OJS 3.3 (later also for 3.2 and 3.4). This plugin will give more accurate and detailed citations metadata. Furthermore, this plugin will also publish the metadata to external open access websites such as wikidata, croci, (crossref) etc.

For parsing the raw references, I created a PHP method/class. In the “Submit an Article” wizard I want to create a button, which will post the raw references from the textarea to a custom API endpoint. My plugin should than catch this endpoint, parse the raw references with this method/class and return the parsed citations as json. Well that’s the idea anyways.

My question is, how can I create such a custom API endpoint? Is there a plugin or core functionality I can examine and use as an example? Or can you point me in the right direction or advice me how to proceed?

Thanks.

@NateWr

Gazi

Edit:

I currently have a working method by using the LoadHandler and returning JSON. But this is not my preferred way, would prefer to use the API.

I did this as described in here: Example - Add Custom Page - Plugin Guide for OJS and OMP

You could check how to create API handlers here.

If you want your own endpoint (ie - api/v1/my-custom-endpoint), the LoadHandler hook along with the API Handler documentation that @henriqueramos posted is best.

If you want to extend an existing endpoint with new routes (ie - api/v1/submissions/{id}/my-custom-route), you will want to use the APIHandler::endpoints hook.

Hi @gaziyucel,

Here are the two approaches we followed internally:

  • Add a new custom route:

The code below will handle the route /api/stats/readership-analystics:
Where MY_API_HANDLER is a class extending APIHandler (e.g.: pkp-lib/PKPUserHandler.inc.php at 3d170252911785a04c51e34970c157118bed9b43 · pkp/pkp-lib · GitHub)

HookRegistry::register('Dispatcher::dispatch', function (string $hook, \PKPRequest $request): bool {
    $path = array_slice(explode('/', trim($request->getRequestPath(), '/')), -2);
    $router = $request->getRouter();
    if ($router instanceof \APIRouter && $path == ['stats', 'readership-analytics']) {
        $handler = new MY_API_HANDLER($this);
        $router->setHandler($handler);
        $handler->getApp()->run();
        exit;
    }
    return false;
});
  • Add an extra route to an existing APIHandler
    Follow the snippets to register the hook and the implementation:

Thank you @henriqueramos, @NateWr, @jonasraoni.

Idea was to create a new custom API endpoint like api/v1/optimetaCitations, but maybe it’s more practical to extend submissions endpoint at this point.

I’ll give a go with the given solutions. Thanks again.