2015-09-14 16:23:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Core;
|
|
|
|
|
2016-02-01 14:57:34 +01:00
|
|
|
use Icinga\Exception\IcingaException;
|
2017-07-13 08:03:16 +02:00
|
|
|
use Icinga\Exception\NotFoundError;
|
2016-02-01 14:57:34 +01:00
|
|
|
|
2015-09-14 16:23:43 +02:00
|
|
|
class RestApiResponse
|
|
|
|
{
|
|
|
|
protected $errorMessage;
|
|
|
|
|
|
|
|
protected $results;
|
|
|
|
|
|
|
|
protected function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromJsonResult($json)
|
|
|
|
{
|
|
|
|
$response = new static;
|
|
|
|
return $response->parseJsonResult($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromErrorMessage($error)
|
|
|
|
{
|
|
|
|
$response = new static;
|
|
|
|
$response->errorMessage = $error;
|
2018-11-14 08:19:25 +01:00
|
|
|
|
2015-09-14 16:23:43 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResult($desiredKey, $filter = array())
|
2015-12-02 02:40:53 +01:00
|
|
|
{
|
|
|
|
return $this->extractResult($this->results, $desiredKey, $filter);
|
|
|
|
}
|
|
|
|
|
2018-11-14 08:19:25 +01:00
|
|
|
public function getRaw($key = null, $default = null)
|
|
|
|
{
|
|
|
|
if ($key === null) {
|
|
|
|
return $this->results;
|
|
|
|
} elseif (isset($this->results[0]) && property_exists($this->results[0], $key)) {
|
|
|
|
return $this->results[0]->$key;
|
|
|
|
} else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 02:40:53 +01:00
|
|
|
public function getSingleResult()
|
|
|
|
{
|
2016-03-17 01:07:04 +01:00
|
|
|
if ($this->isErrorCode($this->results[0]->code)) {
|
|
|
|
throw new IcingaException(
|
|
|
|
$this->results[0]->status
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $this->results[0]->result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isErrorCode($code)
|
|
|
|
{
|
|
|
|
$code = (int) ceil($code);
|
|
|
|
return $code >= 400;
|
2015-12-02 02:40:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function extractResult($results, $desiredKey, $filter = array())
|
2015-09-14 16:23:43 +02:00
|
|
|
{
|
|
|
|
$response = array();
|
2015-12-02 02:40:53 +01:00
|
|
|
foreach ($results as $result) {
|
2015-09-14 16:23:43 +02:00
|
|
|
foreach ($filter as $key => $val) {
|
|
|
|
if (! property_exists($result, $key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($result->$key !== $val) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! property_exists($result, $desiredKey)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response[$result->$desiredKey] = $result;
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getErrorMessage()
|
|
|
|
{
|
|
|
|
return $this->errorMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function succeeded()
|
|
|
|
{
|
|
|
|
return $this->errorMessage === null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseJsonResult($json)
|
|
|
|
{
|
|
|
|
$result = @json_decode($json);
|
2016-02-01 14:57:34 +01:00
|
|
|
if ($result === null) {
|
|
|
|
$this->setJsonError();
|
2019-09-22 17:07:22 +02:00
|
|
|
// <h1>Bad Request</h1><p><pre>bad version</pre></p>
|
|
|
|
throw new IcingaException(
|
|
|
|
'Parsing JSON result failed: '
|
|
|
|
. $this->errorMessage
|
|
|
|
. ' (Got: ' . substr($json, 0, 60) . ')'
|
|
|
|
);
|
2015-09-14 16:23:43 +02:00
|
|
|
}
|
2016-03-18 13:49:42 +01:00
|
|
|
if (property_exists($result, 'error')) {
|
|
|
|
if (property_exists($result, 'status')) {
|
2017-07-13 08:03:16 +02:00
|
|
|
if ((int) $result->error === 404) {
|
|
|
|
throw new NotFoundError($result->status);
|
|
|
|
} else {
|
|
|
|
throw new IcingaException('API request failed: ' . $result->status);
|
|
|
|
}
|
2016-03-18 13:49:42 +01:00
|
|
|
} else {
|
|
|
|
throw new IcingaException('API request failed: ' . var_export($result, 1));
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 16:23:43 +02:00
|
|
|
|
|
|
|
$this->results = $result->results; // TODO: Check if set
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-02-01 14:57:34 +01:00
|
|
|
// TODO: just return json_last_error_msg() for PHP >= 5.5.0
|
2015-09-14 16:23:43 +02:00
|
|
|
protected function setJsonError()
|
|
|
|
{
|
|
|
|
switch (json_last_error()) {
|
2016-02-01 14:57:34 +01:00
|
|
|
case JSON_ERROR_DEPTH:
|
|
|
|
$this->errorMessage = 'The maximum stack depth has been exceeded';
|
|
|
|
break;
|
|
|
|
case JSON_ERROR_CTRL_CHAR:
|
|
|
|
$this->errorMessage = 'Control character error, possibly incorrectly encoded';
|
|
|
|
break;
|
|
|
|
case JSON_ERROR_STATE_MISMATCH:
|
|
|
|
$this->errorMessage = 'Invalid or malformed JSON';
|
|
|
|
break;
|
|
|
|
case JSON_ERROR_SYNTAX:
|
|
|
|
$this->errorMessage = 'Syntax error';
|
|
|
|
break;
|
|
|
|
case JSON_ERROR_UTF8:
|
|
|
|
$this->errorMessage = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
|
|
|
break;
|
2015-09-14 16:23:43 +02:00
|
|
|
default:
|
2016-02-01 14:57:34 +01:00
|
|
|
$this->errorMessage = 'An error occured when parsing a JSON string';
|
2015-09-14 16:23:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|