Add support for JSON listing #1

Merged
Francesco Siddi merged 7 commits from json-view into develop 2023-11-01 12:08:09 +01:00
Showing only changes of commit 16b3ccec59 - Show all commits

View File

@ -13,6 +13,29 @@ function handleNotFoundRequest() {
return;
}
function renderDownloadResponseAsJSON($lister) {
header('Content-Type: application/json; charset=utf-8');
// Require v=1 to be specified
if (!isset($_GET['v']) || $_GET['v'] != '1') {
$data = [ 'error' => 'Missing supported format version, for example v=1'];
echo json_encode( $data );
http_response_code(400);
return;
}
$builds = $lister->getBuilds();
$renderer = new BuildsRenderer($builds);
$renderer->renderJSON();
return;
}
function renderDownloadResponseAsHTML($lister) {
// Serve the directory listing as HTML.
require 'templates/header.php';

You do not need to check isset($_GET['format']) when you're comparing to an explicit constant. Doing if ($_GET['format'] == 'json') { should suffice.

You do not need to check `isset($_GET['format'])` when you're comparing to an explicit constant. Doing `if ($_GET['format'] == 'json') {` should suffice.

I tried, but removing the check raises:

Warning: Undefined array key "format" in /var/www/html/source/main.php on line 56

I tried, but removing the check raises: `Warning: Undefined array key "format" in /var/www/html/source/main.php on line 56`
// Logic inside content_build_lister.php will render the list of builds
require 'templates/content_build_lister.php';
fsiddi marked this conversation as resolved Outdated

Move to renderDownloadResponceAsJSON or something similar.

Move to `renderDownloadResponceAsJSON` or something similar.
require 'templates/footer.php';
}
function handleDownloadRequest() {
$lister = createListerForCurrentRequest();
$is_valid = $lister->isValid();
@ -30,26 +53,12 @@ function handleDownloadRequest() {
}
// Check if we are requesting a JSON formatted view.
if (isset($_GET['format']) && $_GET['format'] == 'json') {
header('Content-Type: application/json; charset=utf-8');
// Require v=1 to be specified
if (!isset($_GET['v']) || $_GET['v'] != '1') {
$data = [ 'error' => 'Missing supported format version, for example v=1'];
echo json_encode( $data );
http_response_code(400);
return;
}
$builds = $lister->getBuilds();
$renderer = new BuildsRenderer($builds);
$renderer->renderJSON();
return;
if ($_GET['format'] == 'json') {
renderDownloadResponseAsJSON($lister);
}
// Serve the directory listing as HTML.
require 'templates/header.php';
// Logic inside content_build_lister.php will render the list of builds
require 'templates/content_build_lister.php';
require 'templates/footer.php';
renderDownloadResponseAsHTML($lister);
}
function handleDashboardRequest() {