Merge pull request #14 from halkyon/json-output

Output check result and details as JSON if requested
This commit is contained in:
Stig Lindqvist 2015-04-09 10:02:58 +12:00
commit 20f0a2bf88
3 changed files with 29 additions and 3 deletions

View File

@ -12,4 +12,4 @@ class DevHealthController extends Controller {
$e->setErrorCode(404);
return $e;
}
}
}

View File

@ -192,6 +192,22 @@ class EnvironmentCheckSuiteResult extends ViewableData {
return $this->details;
}
/**
* Convert the final result status and details to JSON.
* @return string
*/
function toJSON() {
$result = array(
'Status' => $this->Status(),
'ShouldPass' => $this->ShouldPass(),
'Checks' => array()
);
foreach($this->details as $detail) {
$result['Checks'][] = $detail->toMap();
}
return json_encode($result);
}
/**
* Return a text version of a status code
*/

View File

@ -88,11 +88,11 @@ class EnvironmentChecker extends RequestHandler {
function index() {
$response = new SS_HTTPResponse;
$result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run();
if(!$result->ShouldPass()) {
$response->setStatusCode($this->errorCode);
}
$resultText = $result->customise(array(
"URL" => Director::absoluteBaseURL(),
"Title" => $this->title,
@ -105,6 +105,16 @@ class EnvironmentChecker extends RequestHandler {
$email->send();
}
// output the result as JSON if requested
if(
$this->getRequest()->getExtension() == 'json'
|| strpos($this->getRequest()->getHeader('Accept'), 'application/json') !== false
) {
$response->setBody($result->toJSON());
$response->addHeader('Content-Type', 'application/json');
return $response;
}
$response->setBody($resultText);
return $response;