/** * Handle REST request * * @param \Magento\Framework\App\RequestInterface $request * @return \Magento\Framework\App\ResponseInterface */ public function dispatch(\Magento\Framework\App\RequestInterface $request) { $path = $this->_pathProcessor->process($request->getPathInfo()); $this->_request->setPathInfo($path); $this->areaList->getArea($this->_appState->getAreaCode())->load(\Magento\Framework\App\Area::PART_TRANSLATE); try { $this->checkPermissions(); $route = $this->getCurrentRoute(); if ($route->isSecure() && !$this->_request->isSecure()) { throw new \Magento\Framework\Webapi\Exception(__('Operation allowed only in HTTPS')); } /** @var array $inputData */ $inputData = $this->_request->getRequestData(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); $inputData = $this->paramsOverrider->override($inputData, $route->getParameters()); $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData); $service = $this->_objectManager->get($serviceClassName); /** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */ $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams); $outputData = $this->serviceOutputProcessor->process($outputData, $serviceClassName, $serviceMethodName); if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) { $outputData = $this->fieldsFilter->filter($outputData); } $this->_response->prepareResponse($outputData); } catch (\Exception $e) { $maskedException = $this->_errorProcessor->maskException($e); $this->_response->setException($maskedException); } return $this->_response; }
/** * Process and resolve input parameters * * @return array * @throws \Magento\Framework\Webapi\Exception */ public function resolve() { $this->requestValidator->validate(); $route = $this->getRoute(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); /* * Valid only for updates using PUT when passing id value both in URL and body */ if ($this->request->getHttpMethod() == RestRequest::HTTP_METHOD_PUT) { $inputData = $this->paramsOverrider->overrideRequestBodyIdWithPathParam($this->request->getParams(), $this->request->getBodyParams(), $serviceClassName, $serviceMethodName); $inputData = array_merge($inputData, $this->request->getParams()); } else { $inputData = $this->request->getRequestData(); } $inputData = $this->paramsOverrider->override($inputData, $route->getParameters()); $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData); return $inputParams; }
/** * Execute API request * * @return void * @throws AuthorizationException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Webapi\Exception */ protected function processApiRequest() { $this->validateRequest(); /** @var array $inputData */ $inputData = $this->_request->getRequestData(); $route = $this->getCurrentRoute(); $serviceMethodName = $route->getServiceMethod(); $serviceClassName = $route->getServiceClass(); $inputData = $this->paramsOverrider->override($inputData, $route->getParameters()); $inputParams = $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData); $service = $this->_objectManager->get($serviceClassName); /** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */ $outputData = call_user_func_array([$service, $serviceMethodName], $inputParams); $outputData = $this->serviceOutputProcessor->process($outputData, $serviceClassName, $serviceMethodName); if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) { $outputData = $this->fieldsFilter->filter($outputData); } $this->_response->prepareResponse($outputData); }
protected function setUp() { $this->mockArguments(); $layoutMock = $this->getMockBuilder('Magento\\Framework\\View\\LayoutInterface')->disableOriginalConstructor()->getMock(); $errorProcessorMock = $this->getMock('Magento\\Framework\\Webapi\\ErrorProcessor', [], [], '', false); $errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnArgument(0)); $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->serviceInputProcessorMock = $this->getMockBuilder('\\Magento\\Framework\\Webapi\\ServiceInputProcessor')->disableOriginalConstructor()->setMethods(['process'])->getMock(); $this->areaListMock = $this->getMock('\\Magento\\Framework\\App\\AreaList', [], [], '', false); $this->areaMock = $this->getMock('Magento\\Framework\\App\\AreaInterface'); $this->areaListMock->expects($this->any())->method('getArea')->will($this->returnValue($this->areaMock)); /** Init SUT. */ $this->_restController = $objectManager->getObject('Magento\\Webapi\\Controller\\Rest', ['request' => $this->_requestMock, 'response' => $this->_responseMock, 'router' => $this->_routerMock, 'objectManager' => $this->_objectManagerMock, 'appState' => $this->_appStateMock, 'layout' => $layoutMock, 'oauthService' => $this->_oauthServiceMock, 'authorization' => $this->_authorizationMock, 'serviceInputProcessor' => $this->serviceInputProcessorMock, 'errorProcessor' => $errorProcessorMock, 'areaList' => $this->areaListMock, 'paramsOverrider' => $this->paramsOverriderMock, 'dataObjectProcessor' => $this->dataObjectProcessorMock]); // Set default expectations used by all tests $this->_routeMock->expects($this->any())->method('getServiceClass')->will($this->returnValue(self::SERVICE_ID)); $this->_routeMock->expects($this->any())->method('getServiceMethod')->will($this->returnValue(self::SERVICE_METHOD)); $this->_routerMock->expects($this->any())->method('match')->will($this->returnValue($this->_routeMock)); $this->_objectManagerMock->expects($this->any())->method('get')->will($this->returnValue($this->_serviceMock)); $this->_responseMock->expects($this->any())->method('prepareResponse')->will($this->returnValue([])); $this->_serviceMock->expects($this->any())->method(self::SERVICE_METHOD)->will($this->returnValue(null)); $this->dataObjectProcessorMock->expects($this->any())->method('getMethodReturnType')->with(self::SERVICE_ID, self::SERVICE_METHOD)->will($this->returnValue('null')); $this->paramsOverriderMock->expects($this->any())->method('overrideParams')->will($this->returnValue([])); parent::setUp(); }