diff --git a/Changelog.md b/Changelog.md index e04d9fe..645822d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,7 +1,9 @@ # Changelog ## v0.4 - 24th February 2019 (unreleased) - - [API] Added new `format` GET parameter to `device-data` action. + - [API] Added new `format` GET parameter to the following actions: + - `device-data` + - `list-reading-types` ## v0.3.3 - 20th February 2019 - Updated to use new database structure diff --git a/README.md b/README.md index 9af005e..a0ae652 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,7 @@ Examples: Parameter | Type | Meaning --------------------|-----------|--------------------- `device-id` | int | Optional. If specified, this filters the list of measurement types to list only those reported by the device with the specified id. +`format` | string | Optional. Specifies the format that the response will be returned in. Valid values: `json`, `csv`. Default: `json`. ``` https://example.com/path/to/api.php?action=list-reading-types @@ -153,7 +154,7 @@ Parameter | Type | Meaning `start` | datetime | The starting datetime. `end` | datetime | The ending datetime. `average-seconds` | int | Optional. If specified, readings will be grouped into lumps of this many seconds and averaged. For example a value of 3600 (1 hour) will return 1 data point per hour, with the value of each point an average of all the readings for that hour. -`format` | string | Optional. Specifies the format that the response will be returned in. Valid values: `json`, `csv`. Default: `json` +`format` | string | Optional. Specifies the format that the response will be returned in. Valid values: `json`, `csv`. Default: `json`. ### changelog diff --git a/lib/SBRL/ResponseEncoder.php b/lib/SBRL/ResponseEncoder.php new file mode 100644 index 0000000..31df56f --- /dev/null +++ b/lib/SBRL/ResponseEncoder.php @@ -0,0 +1,31 @@ +sender->send_error_plain(406, + "Error: The format '$format' isn't recognised. Valid formats: " . implode(", ", $format) . "." + ); + exit; + } // 1: Pull data from database $data = null; @@ -52,7 +60,19 @@ class ListReadingTypes implements IAction { // 3: Serialise data $start_encode = microtime(true); - $response = json_encode($data); + $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); + break; + case "csv": + $response_type = "text/csv"; + $response = ResponseEncoder::encode_csv($data); + break; + } // 4: Send response @@ -60,7 +80,8 @@ class ListReadingTypes implements IAction { // TODO: Investigate adding a short-term (~10mins?) cache-control header here 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)); echo($response); return true;