Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit fd40929

Browse files
committed
magento-engcom/bulk-api#7 Add extension point to WebAPI
- reverted back the logic for resolution of the processors - added sort order of the injected objects
1 parent bae7248 commit fd40929

File tree

6 files changed

+60
-42
lines changed

6 files changed

+60
-42
lines changed

app/code/Magento/Webapi/Controller/Rest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
182182
$path = $this->_pathProcessor->process($request->getPathInfo());
183183
$this->_request->setPathInfo($path);
184184
$this->areaList->getArea($this->_appState->getAreaCode())
185-
->load(\Magento\Framework\App\Area::PART_TRANSLATE);
185+
->load(\Magento\Framework\App\Area::PART_TRANSLATE);
186186
try {
187-
$this->requestProcessorPool->process($this->_request);
187+
$processor = $this->requestProcessorPool->getProcessor($this->_request);
188+
$processor->process($this->_request);
188189
} catch (\Exception $e) {
189190
$maskedException = $this->_errorProcessor->maskException($e);
190191
$this->_response->setException($maskedException);
@@ -208,7 +209,7 @@ protected function isSchemaRequest()
208209
*
209210
* @return Route
210211
* @deprecated 100.1.0
211-
* @see Magento\Webapi\Controller\Rest\InputParamsResolver::getRoute
212+
* @see \Magento\Webapi\Controller\Rest\InputParamsResolver::getRoute
212213
*/
213214
protected function getCurrentRoute()
214215
{
@@ -245,7 +246,7 @@ protected function checkPermissions()
245246
* @throws \Magento\Framework\Webapi\Exception
246247
* @return void
247248
* @deprecated 100.1.0
248-
* @see Magento\Webapi\Controller\Rest\RequestValidator::validate
249+
* @see \Magento\Webapi\Controller\Rest\RequestValidator::validate
249250
*/
250251
protected function validateRequest()
251252
{

app/code/Magento/Webapi/Controller/Rest/RequestProcessorInterface.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
*/
1212
interface RequestProcessorInterface
1313
{
14-
1514
/**
15+
* Executes the logic to process the request
16+
*
1617
* @param \Magento\Framework\Webapi\Rest\Request $request
1718
* @return void
1819
* @throws \Magento\Framework\Exception\AuthorizationException
@@ -22,7 +23,19 @@ interface RequestProcessorInterface
2223
public function process(\Magento\Framework\Webapi\Rest\Request $request);
2324

2425
/**
26+
* Return processor request prefix
27+
*
2528
* @return string
2629
*/
2730
public function getProcessorPath();
31+
32+
/**
33+
* Method should return true for all the request current processor can process.
34+
*
35+
* Invoked in the loop for all registered request processors. The first one wins.
36+
*
37+
* @param \Magento\Framework\Webapi\Rest\Request $request
38+
* @return bool
39+
*/
40+
public function canProcess(\Magento\Framework\Webapi\Rest\Request $request);
2841
}

app/code/Magento/Webapi/Controller/Rest/RequestProcessorPool.php

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Request Processor Pool
1111
*/
12-
class RequestProcessorPool implements RequestProcessorInterface
12+
class RequestProcessorPool
1313
{
1414

1515
/**
@@ -20,7 +20,7 @@ class RequestProcessorPool implements RequestProcessorInterface
2020
/**
2121
* Initial dependencies
2222
*
23-
* @param array $requestProcessors
23+
* @param RequestProcessorInterface[] $requestProcessors
2424
*/
2525
public function __construct($requestProcessors = [])
2626
{
@@ -30,46 +30,24 @@ public function __construct($requestProcessors = [])
3030
/**
3131
* {@inheritdoc}
3232
*
33-
* @throws \Magento\Framework\Exception\LocalizedException
33+
* @throws \Magento\Framework\Webapi\Exception
34+
* return RequestProcessorInterface
3435
*/
35-
public function process(\Magento\Framework\Webapi\Rest\Request $request)
36+
public function getProcessor(\Magento\Framework\Webapi\Rest\Request $request)
3637
{
37-
$processed = false;
38-
3938
/**
4039
* @var RequestProcessorInterface $processor
4140
*/
4241
foreach ($this->requestProcessors as $processor) {
43-
if (strpos(ltrim($request->getPathInfo(), '/'), $processor->getProcessorPath()) === 0) {
44-
$processor->process($request);
45-
$processed = true;
46-
break;
42+
if ($processor->canProcess($request)) {
43+
return $processor;
4744
}
4845
}
49-
if (!$processed) {
50-
throw new \Magento\Framework\Exception\LocalizedException(
51-
__('Specified request cannot be processed.'),
52-
null,
53-
400
54-
);
55-
}
56-
}
5746

58-
/**
59-
* Get array of rest processors from di.xml
60-
*
61-
* @return array
62-
*/
63-
public function getProcessors()
64-
{
65-
return $this->requestProcessors;
66-
}
67-
68-
/**
69-
* {@inheritdoc}
70-
*/
71-
public function getProcessorPath()
72-
{
73-
return null;
47+
throw new \Magento\Framework\Webapi\Exception(
48+
__('Specified request cannot be processed.'),
49+
0,
50+
\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST
51+
);
7452
}
7553
}

app/code/Magento/Webapi/Controller/Rest/SchemaRequestProcessor.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function process(\Magento\Framework\Webapi\Rest\Request $request)
5454
$responseBody = $this->swaggerGenerator->generate(
5555
$requestedServices,
5656
$request->getScheme(),
57-
$request->getHttpHost(),
57+
$request->getHttpHost(flase),
5858
$request->getRequestUri()
5959
);
6060
$this->response->setBody($responseBody)->setHeader('Content-Type', 'application/json');
@@ -67,4 +67,14 @@ public function getProcessorPath()
6767
{
6868
return self::PROCESSOR_PATH;
6969
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function canProcess(\Magento\Framework\Webapi\Rest\Request $request) {
75+
if (strpos(ltrim($request->getPathInfo(), '/'), $this->getProcessorPath()) === 0) {
76+
return true;
77+
}
78+
return false;
79+
}
7080
}

app/code/Magento/Webapi/Controller/Rest/SynchronousRequestProcessor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,14 @@ public function getProcessorPath()
114114
{
115115
return self::PROCESSOR_PATH;
116116
}
117+
118+
/**
119+
* {@inheritdoc}
120+
*/
121+
public function canProcess(\Magento\Framework\Webapi\Rest\Request $request) {
122+
if (strpos(ltrim($request->getPathInfo(), '/'), $this->getProcessorPath()) === 0) {
123+
return true;
124+
}
125+
return false;
126+
}
117127
}

app/code/Magento/Webapi/etc/webapi_rest/di.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,14 @@
7575
<type name="Magento\Webapi\Controller\Rest\RequestProcessorPool">
7676
<arguments>
7777
<argument name="requestProcessors" xsi:type="array">
78-
<item name="syncSchema" xsi:type="object">Magento\Webapi\Controller\Rest\SchemaRequestProcessor</item>
79-
<item name="sync" xsi:type="object">Magento\Webapi\Controller\Rest\SynchronousRequestProcessor</item>
78+
<item name="syncSchema" xsi:type="array">
79+
<item name="type" xsi:type="object">Magento\Webapi\Controller\Rest\SchemaRequestProcessor</item>
80+
<item name="sortOrder" xsi:type="string">50</item>
81+
</item>
82+
<item name="sync" xsi:type="array">
83+
<item name="type" xsi:type="object">Magento\Webapi\Controller\Rest\SynchronousRequestProcessor</item>
84+
<item name="sortOrder" xsi:type="string">100</item>
85+
</item>
8086
</argument>
8187
</arguments>
8288
</type>

0 commit comments

Comments
 (0)