API: Add format parameter to fetch-data action.

This commit is contained in:
Starbeamrainbowlabs 2019-02-24 17:17:11 +00:00
parent e29f7f4569
commit 47bc5874cc
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
6 changed files with 36 additions and 8 deletions

View File

@ -5,6 +5,7 @@
- `device-data` - `device-data`
- `list-devices` - `list-devices`
- `list-reading-types` - `list-reading-types`
- `fetch-data`
## v0.3.3 - 20th February 2019 ## v0.3.3 - 20th February 2019
- Updated to use new database structure - Updated to use new database structure

View File

@ -87,6 +87,7 @@ Parameter | Type | Meaning
--------------------|-----------|--------------------- --------------------|-----------|---------------------
`datetime` | date/time | Required. Specifies the date and time for which readings are desired. `datetime` | date/time | Required. Specifies the date and time for which readings are desired.
`reading_type` | string | Required. Specifies the type of reading desired. `reading_type` | string | Required. Specifies the type of reading desired.
`format` | string | Optional. Specifies the format that the response will be returned in. Valid values: `json`, `csv`. Default: `json`.
Examples: Examples:

View File

@ -119,9 +119,8 @@ class DeviceData implements IAction {
break; break;
case "csv": case "csv":
$response_type = "text/csv"; $response_type = "text/csv";
$response_suggested_filename .= ".json"; $response_suggested_filename .= ".csv";
$response = ResponseEncoder::encode_csv($data); $response = ResponseEncoder::encode_csv($data);
break; break;
} }

View File

@ -3,6 +3,7 @@
namespace AirQuality\Actions; namespace AirQuality\Actions;
use \SBRL\TomlConfig; use \SBRL\TomlConfig;
use \SBRL\ResponseEncoder;
use \AirQuality\Repositories\IMeasurementDataRepository; use \AirQuality\Repositories\IMeasurementDataRepository;
use \AirQuality\Repositories\IMeasurementTypeRepository; use \AirQuality\Repositories\IMeasurementTypeRepository;
use \AirQuality\ApiResponseSender; use \AirQuality\ApiResponseSender;
@ -47,8 +48,11 @@ class FetchData implements IAction {
$this->validator->is_datetime("datetime"); $this->validator->is_datetime("datetime");
$this->validator->exists("reading_type"); $this->validator->exists("reading_type");
$this->validator->is_max_length("reading_type", 256); $this->validator->is_max_length("reading_type", 256);
if(!empty($_GET["format"]))
$this->validator->is_preset_value("format", ["json", "csv"], 406);
$this->validator->run(); $this->validator->run();
$format = $_GET["format"] ?? "json";
$measurement_type_id = $this->type_repo->get_id($_GET["reading_type"]); $measurement_type_id = $this->type_repo->get_id($_GET["reading_type"]);
if($measurement_type_id == null) { if($measurement_type_id == null) {
@ -60,24 +64,42 @@ class FetchData implements IAction {
return false; return false;
} }
// 2: Pull data from database // 2: Pull data from database
$data = $this->measurement_repo->get_readings_by_date( $data = $this->measurement_repo->get_readings_by_date(
new \DateTime($_GET["datetime"]), new \DateTime($_GET["datetime"]),
$measurement_type_id $measurement_type_id
); );
// 2.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 data could be found for that timestamp.",
header("x-time-taken: " . PerfFormatter::format_perf_data($start_time, $start_handle, null)); [ PerfFormatter::format_perf_data($start_time, $start_handle, null) ]
echo("Error: No data could be found for that timestamp."); );
return false; return false;
} }
// 3: Serialise data // 3: Serialise data
$start_encode = microtime(true); $start_encode = microtime(true);
$response = json_encode($data); $response_type = "application/octet-stream";
$response_suggested_filename = "data-" . date(\DateTime::ATOM) . "";
$response = null;
switch($format) {
case "json":
$response_type = "application/json";
$response_suggested_filename .= ".json";
$response = json_encode($data);
break;
case "csv":
$response_type = "text/csv";
$response_suggested_filename .= ".csv";
$response = ResponseEncoder::encode_csv($data);
break;
}
// 4: Send response // 4: Send response
@ -86,8 +108,9 @@ class FetchData implements IAction {
header("cache-control: public, max-age=" . $this->settings->get("cache.max-age")); header("cache-control: public, max-age=" . $this->settings->get("cache.max-age"));
} }
header("content-type: $response_type");
header("content-length: " . strlen($response)); header("content-length: " . strlen($response));
header("content-type: application/json"); 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

@ -66,10 +66,12 @@ class ListDevices implements IAction {
switch($format) { switch($format) {
case "json": case "json":
$response_type = "application/json"; $response_type = "application/json";
$response_suggested_filename .= ".json";
$response = json_encode($data); $response = json_encode($data);
break; break;
case "csv": case "csv":
$response_type = "text/csv"; $response_type = "text/csv";
$response_suggested_filename .= ".csv";
$response = ResponseEncoder::encode_csv($data); $response = ResponseEncoder::encode_csv($data);
break; break;
} }

View File

@ -67,10 +67,12 @@ class ListReadingTypes implements IAction {
switch($format) { switch($format) {
case "json": case "json":
$response_type = "application/json"; $response_type = "application/json";
$response_suggested_filename .= ".json";
$response = json_encode($data); $response = json_encode($data);
break; break;
case "csv": case "csv":
$response_type = "text/csv"; $response_type = "text/csv";
$response_suggested_filename .= ".csv";
$response = ResponseEncoder::encode_csv($data); $response = ResponseEncoder::encode_csv($data);
break; break;
} }