/** * Run server */ public function run() { // can not use response object case try { /** @var $response Mage_Api2_Model_Response */ $response = Mage::getSingleton('api2/response'); } catch (Exception $e) { Mage::logException($e); if (!headers_sent()) { header('HTTP/1.1 ' . self::HTTP_INTERNAL_ERROR); } echo 'Service temporary unavailable'; return; } // can not render errors case try { /** @var $request Mage_Api2_Model_Request */ $request = Mage::getSingleton('api2/request'); /** @var $renderer Mage_Api2_Model_Renderer_Interface */ $renderer = Mage_Api2_Model_Renderer::factory($request->getAcceptTypes()); } catch (Exception $e) { Mage::logException($e); $response->setHttpResponseCode(self::HTTP_INTERNAL_ERROR)->setBody('Service temporary unavailable')->sendResponse(); return; } // default case try { /** @var $apiUser Mage_Api2_Model_Auth_User_Abstract */ $apiUser = $this->_authenticate($request); $this->_route($request)->_allow($request, $apiUser)->_dispatch($request, $response, $apiUser); if ($response->getHttpResponseCode() == self::HTTP_CREATED) { // TODO: Re-factor this after _renderException refactoring throw new Mage_Api2_Exception('Resource was partially created', self::HTTP_CREATED); } //NOTE: At this moment Renderer already could have some content rendered, so we should replace it if ($response->isException()) { throw new Mage_Api2_Exception('Unhandled simple errors.', self::HTTP_INTERNAL_ERROR); } } catch (Exception $e) { Mage::logException($e); $this->_renderException($e, $renderer, $response); } $response->sendResponse(); }
/** * Get renderer if not exists create * * @return Mage_Api2_Model_Renderer_Interface */ public function getRenderer() { if (!$this->_renderer) { $renderer = Mage_Api2_Model_Renderer::factory($this->getRequest()->getAcceptTypes()); $this->setRenderer($renderer); } return $this->_renderer; }
protected function getRenderer(array $acceptTypes) { /** @var $renderer Mage_Api2_Model_Renderer_Interface */ $renderer = Mage_Api2_Model_Renderer::factory($acceptTypes); return $renderer; }