Add support for JSON listing #1
No reviewers
Labels
No Label
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: infrastructure/blender-buildbot-www#1
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "json-view"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
By appending ?format=json&v=1 it is now possible to display the list
of builds as JSON object. This makes it easier for updaters or
download scripts to list the builds offered.
Notice that v=1 must be specified for the listing to appear, otherwise
we return code 400 with an error message.
@ -31,2 +31,3 @@
// Serve the directory listing.
// Check if we are requesting a JSON formatted view.
if (isset($_GET['format']) && $_GET['format'] == 'json') {
You do not need to check
isset($_GET['format'])
when you're comparing to an explicit constant. Doingif ($_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
@ -33,0 +32,4 @@
// 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
Move to
renderDownloadResponceAsJSON
or something similar.@ -33,0 +45,4 @@
return;
}
// Serve the directory listing as HTML.
Move to
function renderDownloadResponceAsHTML
.@ -11,0 +12,4 @@
/* Return a JSON formatted list of builds. */
foreach ($this->builds as $build) {
// Construct a full URL to the downloadable file
$build->url = $this->getFileNameURL($build);
I didn't realize how it worked before. We should not be modifying builds from the renderer. And we should never inject fields which do not exist in the class.
Unfortunately, PHP does not provide anything to ensure const-correctness. But once the build and list of builds were constructed they should remain non-mutable.
More verbose but more correct is to create an explicit associative array with the fields which you're interested in:
This hides the code flow, without any indication that it is what that will happen. An errorous output after rendering response should not be guarded with an
exit
somewhere deep in the call hierarchy.There might be other things happening after render, like performance or statistics gathering, logging, etc.
I guess that is because
renderDownloadResponseAsHTML ()
could have happened afterrenderDownloadResponseAsJSON()
, but the proper fix for it is to:I indeed missed the
return
afterrenderDownloadResponseAsJSON
.Thank you for your patience with this review.