/** * Logs failed requests data * * @param Request $request API request data * @param Response $response API response data */ public function logError(Request $request, Response $response) { if ($this->enabled && !empty($this->writer)) { try { $time = time(); $status = $response->getStatus(); $data = ["tag" => $this->defaultTag, "dateTime" => $time, "message" => $status, "extra" => ['request' => ['remote_ip' => $request->getIp(), 'method' => $request->getMethod(), 'url' => $request->getUrl() . $request->getPath(), 'headers' => $request->headers(), 'body' => $request->getBody()], 'response' => $response->finalize(), 'tags' => [$this->defaultTag, $status], 'time' => $time], "type" => "ApiLog"]; $this->writer->send($data); } catch (Exception $e) { \Scalr::logException(new Exception(sprintf("Api logger could not save the record: %s", $e->getMessage()), $e->getCode(), $e)); } } }
/** * Asserts describe response has all properties and deprecated paths response are identical current response * * @param string $basePath * @param array $listPaths * @param bool $environment optional if variable is true replace envId for test environment */ public function assertApiListResponse($basePath, $listPaths, $environment = false) { $this->assertNotEmpty($listPaths); foreach ($listPaths as $path => $property) { $pathInfo = $basePath . $path; if ($environment) { $pathInfo = str_replace('{envId}', static::$testEnvId, $pathInfo); } $resp = $this->request($pathInfo, Request::METHOD_GET); $envelope = $resp->getBody(); $this->assertDescribeResponseNotEmpty($resp); $pathInfoDeprecated = preg_replace("#/(v\\d.*?)/(user|admin|account)/#", '/$2/$1/', $pathInfo); $resDepPath = $this->request($pathInfoDeprecated, Request::METHOD_GET); $this->assertDescribeResponseNotEmpty($resDepPath); $envelopeDep = $resDepPath->getBody(); $this->assertObjectHasAttribute('warnings', $envelopeDep); $this->assertNotEmpty($envelopeDep->warnings); $code = []; $message = []; /* @var $warning ApiMessage */ foreach ($envelopeDep->warnings as $warning) { $code[] = $warning->code; $message[] = $warning->message; } $this->assertContains(Response::getCodeMessage(301), $code); $this->assertContains(sprintf('Location %s', $pathInfo), $message); $this->assertEquals($envelope->data, $envelopeDep->data); } }
/** * {@inheritdoc} * @see \Scalr\Api\Rest\Application::__construct() */ public function __construct(array $settings = []) { parent::__construct($settings); $cont = $this->getContainer(); $cont->api->setShared('meta', function ($cont) { return new Meta(); }); $cont->api->setShared('warnings', function () { return new Warnings(); }); $this->pathPreprocessor = function ($method, $pathInfo) { if (preg_match("#^/api/(user|admin|account)/#", $pathInfo)) { $pathInfo = preg_replace("#/(user|admin|account)/(v\\d.*?)/#", '/$2/$1/', $pathInfo); $this->warnings->appendWarnings(Response::getCodeMessage(301), sprintf('Location %s', $pathInfo)); } return [$method, $pathInfo]; }; $this->limiter = new Limiter(\Scalr::getContainer()->config->{'scalr.system.api.limits'}); }
/** * Logs failed requests data * * @param Request $request API request data * @param Response $response API response data * * @return array Returns array of the fields to log */ protected function handlerApiError(Request $request, Response $response) { $data = ['.request.method' => $request->getMethod(), '.request.url' => $request->getUrl() . $request->getPath(), '.request.headers' => $request->headers(), '.request.body' => $request->getBody(), '.response.status' => $response->getStatus(), '.response.headers' => $response->getHeaders(), '.response.body' => $response->getBody()]; return $data; }
/** * Runs application */ public function run() { $this->response->setHeader('X-Scalr-Inittime', microtime(true) - $this->startTime); $this->call(); $this->response->setHeader('X-Scalr-Actiontime', microtime(true) - $this->startTime); //Fetch status, header, and body list($status, $headers, $body) = $this->response->finalize(); if (headers_sent() === false) { if (strpos(PHP_SAPI, 'cgi') === 0) { header(sprintf('Status: %s', Response::getCodeMessage($status))); } else { header(sprintf('HTTP/%s %s', '1.1', Response::getCodeMessage($status))); } @header_remove('X-Powered-By'); if (isset($this->settings[static::SETTING_API_VERSION])) { $headers['X-Powered-By'] = sprintf("Scalr API/%s", $this->settings[static::SETTING_API_VERSION]); } //Send headers foreach ($headers as $name => $value) { //Normalizes the header name $name = implode('-', array_map(function ($v) { return ucfirst(strtolower($v)); }, preg_split('/[_-]/', $name))); $a = explode("\n", $value); if ($a) { foreach ($a as $v) { header("{$name}: {$v}", false); } } } } if ($this->request->getMethod() !== Request::METHOD_HEAD) { echo $body; } }
/** * Gets json decoded response * * @return object */ public function getBody() { return $this->response->getBody() == '' ? null : json_decode($this->response->getBody()); }