1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\WebapiAsync\Controller\Rest;
use Magento\Webapi\Model\Rest\Swagger\Generator;
use Magento\Framework\Webapi\Rest\Response as RestResponse;
use Magento\Framework\Webapi\Request;
use Magento\Webapi\Controller\Rest\RequestProcessorInterface;
class AsynchronousSchemaRequestProcessor implements RequestProcessorInterface
{
/**
* Path for accessing Async Rest API schema
*/
const PROCESSOR_PATH = 'async/schema';
const BULK_PROCESSOR_PATH = 'async/bulk/schema';
/**
* @var \Magento\Webapi\Model\Rest\Swagger\Generator
*/
private $swaggerGenerator;
/**
* @var \Magento\Framework\Webapi\Rest\Response
*/
private $response;
/**
* @var string
*/
private $processorPath;
/**
* Initial dependencies
*
* @param \Magento\Webapi\Model\Rest\Swagger\Generator $swaggerGenerator
* @param \Magento\Framework\Webapi\Rest\Response $response
* @param string $processorPath
*/
public function __construct(
Generator $swaggerGenerator,
RestResponse $response,
$processorPath = self::PROCESSOR_PATH
) {
$this->swaggerGenerator = $swaggerGenerator;
$this->response = $response;
$this->processorPath = $processorPath;
}
/**
* {@inheritdoc}
*/
public function process(\Magento\Framework\Webapi\Rest\Request $request)
{
$requestedServices = $request->getRequestedServices('all');
$requestedServices = $requestedServices == Request::ALL_SERVICES
? $this->swaggerGenerator->getListOfServices()
: $requestedServices;
$responseBody = $this->swaggerGenerator->generate(
$requestedServices,
$request->getScheme(),
$request->getHttpHost(),
$request->getRequestUri()
);
$this->response->setBody($responseBody)->setHeader('Content-Type', 'application/json');
}
/**
* {@inheritdoc}
*/
public function canProcess(\Magento\Framework\Webapi\Rest\Request $request)
{
if (strpos(ltrim($request->getPathInfo(), '/'), $this->processorPath) === 0) {
return true;
}
return false;
}
/**
* @param \Magento\Framework\Webapi\Rest\Request $request
* @return bool
*/
public function isBulk(\Magento\Framework\Webapi\Rest\Request $request)
{
if (strpos(ltrim($request->getPathInfo(), '/'), self::BULK_PROCESSOR_PATH) === 0) {
return true;
}
return false;
}
}