API: Add format parameter to list-devices action

This commit is contained in:
Starbeamrainbowlabs 2019-02-24 17:11:36 +00:00
parent 09ab6f38d5
commit e29f7f4569
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 37 additions and 9 deletions

View file

@ -3,6 +3,7 @@
## v0.4 - 24th February 2019 (unreleased) ## v0.4 - 24th February 2019 (unreleased)
- [API] Added new `format` GET parameter to the following actions: - [API] Added new `format` GET parameter to the following actions:
- `device-data` - `device-data`
- `list-devices`
- `list-reading-types` - `list-reading-types`
## v0.3.3 - 20th February 2019 ## v0.3.3 - 20th February 2019

View file

@ -100,6 +100,7 @@ https://example.com/path/to/api.php?action=fetch-data&datetime=2019-01-03%2007:5
Parameter | Type | Meaning Parameter | Type | Meaning
--------------------|-----------|--------------------- --------------------|-----------|---------------------
`only-with-location`| bool | Optional. If present only devices with a defined location will be returned. Useful for getting a list of devices to place on a map. `only-with-location`| bool | Optional. If present only devices with a defined location will be returned. Useful for getting a list of devices to place on a map.
`format` | string | Optional. Specifies the format that the response will be returned in. Valid values: `json`, `csv`. Default: `json`.
Examples: Examples:

View file

@ -3,6 +3,7 @@
namespace AirQuality\Actions; namespace AirQuality\Actions;
use \SBRL\TomlConfig; use \SBRL\TomlConfig;
use \SBRL\ResponseEncoder;
use \AirQuality\Repositories\IDeviceRepository; use \AirQuality\Repositories\IDeviceRepository;
use \AirQuality\ApiResponseSender; use \AirQuality\ApiResponseSender;
@ -34,21 +35,44 @@ class ListDevices implements IAction {
// 1: Parse & validate parameters // 1: Parse & validate parameters
$only_with_location = !empty($_GET["only-with-location"]); $only_with_location = !empty($_GET["only-with-location"]);
// 1: Pull data from database $format = $_GET["format"] ?? "json";
if(!in_array($format, ["json", "csv"])) {
$this->sender->send_error_plain(406,
"Error: The format '$format' isn't recognised. Valid formats: " . implode(", ", $format) . "."
);
exit;
}
// 2: Pull data from database
$data = $this->device_repo->get_all_devices($only_with_location); $data = $this->device_repo->get_all_devices($only_with_location);
// 1.5: Validate data from database
// 2.5: Validate data from database
if(empty($data)) { if(empty($data)) {
http_response_code(404); $this->sender->send_error_plain(404,
header("content-type: text/plain"); "Error: No devices are currently present in the system.",
header("x-time-taken: " . PerfFormatter::format_perf_data($start_time, $start_handle, null)); [ "x-time-taken: " . PerfFormatter::format_perf_data($start_time, $start_handle, null) ]
echo("Error: No devices are currently present in the system."); );
return false; return false;
} }
// 3: Serialise data // 3: Serialise data
$start_encode = microtime(true); $start_encode = microtime(true);
$response = null;
$response_type = "application/octet-stream";
$response_suggested_filename = "data-" . date(\DateTime::ATOM) . "";
switch($format) {
case "json":
$response_type = "application/json";
$response = json_encode($data); $response = json_encode($data);
break;
case "csv":
$response_type = "text/csv";
$response = ResponseEncoder::encode_csv($data);
break;
}
// 4: Send response // 4: Send response
@ -56,7 +80,8 @@ class ListDevices implements IAction {
// TODO: Investigate adding a short-term (~10mins?) cache-control header here // TODO: Investigate adding a short-term (~10mins?) cache-control header here
header("content-length: " . strlen($response)); header("content-length: " . strlen($response));
header("content-type: application/json"); header("content-type: $response_type");
header("content-disposition: inline; filename=$response_suggested_filename");
header("x-time-taken: " . PerfFormatter::format_perf_data($start_time, $start_handle, $start_encode)); header("x-time-taken: " . PerfFormatter::format_perf_data($start_time, $start_handle, $start_encode));
echo($response); echo($response);
return true; return true;

View file

@ -34,12 +34,13 @@ class ListReadingTypes implements IAction {
// 1: Parse & validate parameters // 1: Parse & validate parameters
$device_id = !empty($_GET["device-id"]) ? intval($_GET["device-id"]) : null; $device_id = !empty($_GET["device-id"]) ? intval($_GET["device-id"]) : null;
$format = $_GET["format"] ?? "json"; $format = $_GET["format"] ?? "json";
if(!in_array($format, ["json", "csv"])) { if(!in_array($format, ["json", "csv"])) {
$this->sender->send_error_plain(406, $this->sender->send_error_plain(406,
"Error: The format '$format' isn't recognised. Valid formats: " . implode(", ", $format) . "." "Error: The format '$format' isn't recognised. Valid formats: " . implode(", ", $format) . "."
); );
exit; return false;
} }
// 1: Pull data from database // 1: Pull data from database