Register API Handler OPS 3.4

Hi,

I want to implement a custom API endpoint. I found this post Custom API endpoint which I can use in my plugin here, but I think it is outdated.
Moreover, searching the PKP Code for a call to LoadHandlersuggests that the suggested handler is not called in case of API requests, but only in case of page requests?.Any suggestions on how to register a custom API handler?

I’m using OPS 3.4.0.1.

Thanks,
Felix

Hi @felixhelix,

the plugin I am working on is only compatible with 3.3.0 and is not upgraded yet to 3.4.0.
This means I can show you how to do it in 3.3.0, but you or someone else needs to translate this to 3.4.0.

  1. This is the hook I used (https://github.com/TIBHannover/optimetaCitations/blob/35f7bf2bcb701db4b970a55b95e2977265c45d17/OptimetaCitationsPlugin.inc.php#L144)
HookRegistry::register('Dispatcher::dispatch', array($this, 'apiHandler'));
  1. This is the corresponding method (https://github.com/TIBHannover/optimetaCitations/blob/35f7bf2bcb701db4b970a55b95e2977265c45d17/OptimetaCitationsPlugin.inc.php#L401)
public function apiHandler(string $hookName, PKPRequest $request): bool
{
    try {
        $router = $request->getRouter();
        if ($router instanceof \APIRouter && strpos(' ' .
                $request->getRequestPath() . ' ', 'api/v1/' . OPTIMETA_CITATIONS_API_ENDPOINT) !== false) {
            $handler = new PluginAPIHandler($this);
            $router->setHandler($handler);
            $handler->getApp()->run();
            exit;
        }
    } catch (Throwable $e) {
    }

    return false;
}
  1. The class which handles the requests from the api is this class:
  1. If you have done this for 3.4.0, can you please put a reply to this post. Will help me and others doing this in the future. Thanks.

Hope this helps.
Gazi

@gaziyucel Thanks Gazi!
Finally got it working in 3.4
Things to change:

“HookRegistry” seems to be no longer available. But you can use
Hook::add('Dispatcher::dispatch', [$this, 'setYourApiHandler']);

The Hook in Dispatcher.php is
Hook::call('Dispatcher::dispatch', [$request]);
Since it submits an array now, and not longer the $request object directly, the function to return the handler has to be

public function setYourApiHandler(string $hookName, array $args): bool 
    {
        $request = $args[0];

Yours,
Felix

@felixhelix Thank you for posting your solution.