Edit in GitHubLog an issue
Thanks to Comwrap GmbH for contributing this topic!

Request processors pool

The request processors pool routes web API requests. It is located in the Magento_WebApi module: Magento\Webapi\Controller\Rest\RequestProcessorPool

Adobe Commerce and Magento Open Source define the following processors:

Processors#

sync#

Executes the corresponding service contract.

  • Class: Magento\Webapi\Controller\Rest\SynchronousRequestProcessor
  • URL pattern: /^\\/V\\d+/

syncSchema#

Delivers the schema needed for generating Swagger documentation.

  • Class: Magento\Webapi\Controller\Rest\SchemaRequestProcessor
  • URL pattern: schema

async#

  • Class: Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor
  • URL pattern: /^\\/async(\\/V.+)/

Performs an asynchronous API request. It executes Magento\AsynchronousOperations\Model\MassSchedule::publishMass, which places a single message in the queue.

asyncSchema#

Delivers the schema needed for generating Swagger documentation for asynchronous endpoints.

  • Class: Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor
  • URL pattern: async/schema

asyncBulk#

  • Class: Magento\WebapiAsync\Controller\Rest\VirtualType\AsynchronousBulkRequestProcessor
  • URL pattern: /^\\/async\/bulk(\\/V.+)/

Performs a bulk API request by executing Magento\AsynchronousOperations\Model\MassSchedule::publishMass, which places multiple messages in the queue.

asyncBulkSchema#

Currently not used. Reserved for future use.

  • Class: Magento\WebapiAsync\Controller\Rest\VirtualType\AsynchronousBulkSchemaRequestProcessor
  • URL pattern: async/bulk/schema

Create a new processor#

To create a custom processor, you must perform the following tasks:

  • Define the custom processor in webapi_rest/di.xml.
  • Create a processor class and implement the Magento\Webapi\Controller\Rest\RequestProcessorInterface interface.
  • Define the processing rules in the canProcess method.
  • Create the processor logic in the process method.

Define the custom processor#

Processors must be defined in a module's webapi_rest/di.xml file. The following example shows the definition of the default sync processor:

Copied to your clipboard
1<type name="Magento\Webapi\Controller\Rest\RequestProcessorPool">
2 <arguments>
3 <argument name="requestProcessors" xsi:type="array">
4 <item name="sync" xsi:type="object" sortOrder="100">Magento\Webapi\Controller\Rest\SynchronousRequestProcessor</item>
5 </argument>
6 </arguments>
7</type>

Create the processor class#

A custom processor must implement the Magento\Webapi\Controller\Rest\RequestProcessorInterface interface, as shown below:

Copied to your clipboard
1<?php
2/**
3 * Copyright © Magento, Inc. All rights reserved.
4 * See COPYING.txt for license details.
5 */
6declare(strict_types=1);
7
8namespace Magento\Webapi\Controller\Rest;
9
10/**
11 * Request processor interface
12 */
13interface RequestProcessorInterface
14{
15 /**
16 * Executes the logic to process the request
17 *
18 * @param \Magento\Framework\Webapi\Rest\Request $request
19 * @return void
20 * @throws \Magento\Framework\Exception\AuthorizationException
21 * @throws \Magento\Framework\Exception\InputException
22 * @throws \Magento\Framework\Webapi\Exception
23 */
24 public function process(\Magento\Framework\Webapi\Rest\Request $request);
25
26 /**
27 * Method should return true for all the requests the current processor can process.
28 *
29 * Invoked in the loop for all registered request processors. The first one wins.
30 *
31 * @param \Magento\Framework\Webapi\Rest\Request $request
32 * @return bool
33 */
34 public function canProcess(\Magento\Framework\Webapi\Rest\Request $request);
35}

The canProcess(\Magento\Framework\Webapi\Rest\Request $request) method defines whether the current request can be processed. Currently, all implemented processors match current request URLs with the defined patterns.

For example, Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor processes asynchronous calls, such as <host>/rest/async/V1/products.

Copied to your clipboard
1const PROCESSOR_PATH = "/^\\/async(\\/V.+)/";
2
3.....
4
5public function canProcess(\Magento\Framework\Webapi\Rest\Request $request)
6{
7 if ($request->getHttpMethod() === \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET) {
8 return false;
9 }
10
11 if (preg_match($this->processorPath, $request->getPathInfo()) === 1) {
12 return true;
13 }
14 return false;
15}
16
17.....

The process(\Magento\Framework\Webapi\Rest\Request $request) method executes processor logic.

Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.