isApiRequest() public static method

Detect if request is an API request. Meaning the module is 'API' and an API method having a valid format was specified.
public static isApiRequest ( array $request ) : boolean
$request array eg array('module' => 'API', 'method' => 'Test.getMethod')
return boolean
Example #1
0
 public function test_isApiRequest_shouldDetectIfItIsApiRequestOrNot()
 {
     $this->assertFalse(Request::isApiRequest(array()));
     $this->assertFalse(Request::isApiRequest(array('module' => '', 'method' => '')));
     $this->assertFalse(Request::isApiRequest(array('module' => 'API')));
     // no method
     $this->assertFalse(Request::isApiRequest(array('module' => 'CoreHome', 'method' => 'index.test')));
     // not api
     $this->assertFalse(Request::isApiRequest(array('module' => 'API', 'method' => 'testmethod')));
     // no valid action
     $this->assertTrue(Request::isApiRequest(array('module' => 'API', 'method' => 'test.method')));
 }
Example #2
0
 public function displayDbConnectionMessage($exception = null)
 {
     Common::sendResponseCode(500);
     $errorMessage = $exception->getMessage();
     if (Request::isApiRequest($_GET)) {
         $ex = new DatabaseConnectionFailedException($errorMessage);
         throw $ex;
     }
     $view = new PiwikView("@Installation/cannotConnectToDb");
     $view->exceptionMessage = $errorMessage;
     $ex = new DatabaseConnectionFailedException($view->render());
     $ex->setIsHtmlMessage();
     throw $ex;
 }
 private static function getErrorResponse(Exception $ex)
 {
     $debugTrace = $ex->getTraceAsString();
     $message = $ex->getMessage();
     $isHtmlMessage = method_exists($ex, 'isHtmlMessage') && $ex->isHtmlMessage();
     if (!$isHtmlMessage && Request::isApiRequest($_GET)) {
         $outputFormat = strtolower(Common::getRequestVar('format', 'xml', 'string', $_GET + $_POST));
         $response = new ResponseBuilder($outputFormat);
         return $response->getResponseException($ex);
     } elseif (!$isHtmlMessage) {
         $message = Common::sanitizeInputValue($message);
     }
     $logo = new CustomLogo();
     $logoHeaderUrl = false;
     $logoFaviconUrl = false;
     try {
         $logoHeaderUrl = $logo->getHeaderLogoUrl();
         $logoFaviconUrl = $logo->getPathUserFavicon();
     } catch (Exception $ex) {
         try {
             Log::debug($ex);
         } catch (\Exception $otherEx) {
             // DI container may not be setup at this point
         }
     }
     $result = Piwik_GetErrorMessagePage($message, $debugTrace, true, true, $logoHeaderUrl, $logoFaviconUrl);
     try {
         /**
          * Triggered before a Piwik error page is displayed to the user.
          *
          * This event can be used to modify the content of the error page that is displayed when
          * an exception is caught.
          *
          * @param string &$result The HTML of the error page.
          * @param Exception $ex The Exception displayed in the error page.
          */
         Piwik::postEvent('FrontController.modifyErrorPage', array(&$result, $ex));
     } catch (ContainerDoesNotExistException $ex) {
         // this can happen when an error occurs before the Piwik environment is created
     }
     return $result;
 }