diff options
author | Lukas Fleischer <archlinux@cryptocrack.de> | 2014-06-25 12:10:15 +0200 |
---|---|---|
committer | Lukas Fleischer <archlinux@cryptocrack.de> | 2014-06-25 12:10:15 +0200 |
commit | 65129ce9c3e951d10d8558d68e7b831db2726515 (patch) | |
tree | 07c688bf241360402706e89344001023b36a03f8 /web/lib | |
parent | 91e6b5cb2f3d531ce40cd0331e2261d30766de47 (diff) | |
download | aur-65129ce9c3e951d10d8558d68e7b831db2726515.tar.gz aur-65129ce9c3e951d10d8558d68e7b831db2726515.tar.xz |
Make results consistent in RPC replies
Add RPC v3 which always returns a list of objects in RPC responses,
regardless of the request type. An empty list is returned when an error
occurs. The error message is moved to a separate "error" field.
Fixes FS#40963.
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
Diffstat (limited to 'web/lib')
-rw-r--r-- | web/lib/aurjson.class.php | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index 9abb2d65..8187bef5 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -61,7 +61,7 @@ class AurJSON { if (isset($http_data['v'])) { $this->version = intval($http_data['v']); } - if ($this->version < 1 || $this->version > 2) { + if ($this->version < 1 || $this->version > 3) { return $this->json_error('Invalid version specified.'); } @@ -109,7 +109,11 @@ class AurJSON { */ private function json_error($msg) { header('content-type: application/json'); - return $this->json_results('error', 0, $msg); + if ($this->version < 3) { + return $this->json_results('error', 0, $msg, NULL); + } elseif ($this->version >= 3) { + return $this->json_results('error', 0, array(), $msg); + } } /* @@ -117,16 +121,23 @@ class AurJSON { * * @param $type The response method type. * @param $data The result data to return + * @param $error An error message to include in the response * * @return mixed A json formatted result response. */ - private function json_results($type, $count, $data) { - return json_encode(array( + private function json_results($type, $count, $data, $error) { + $json_array = array( 'version' => $this->version, 'type' => $type, 'resultcount' => $count, 'results' => $data - )); + ); + + if ($error) { + $json_array['error'] = $error; + } + + return json_encode($json_array); } private function get_extended_fields($pkgid) { @@ -195,7 +206,7 @@ class AurJSON { "WHERE ${where_condition} " . "GROUP BY Packages.ID " . "LIMIT $MAX_RPC_RESULTS"; - } elseif ($this->version == 2) { + } elseif ($this->version >= 2) { $fields = implode(',', self::$fields_v2); $query = "SELECT {$fields} " . "FROM Packages LEFT JOIN PackageBases " . @@ -225,14 +236,18 @@ class AurJSON { $row[$field] = intval($row[$field]); } - if ($this->version == 2 && ($type == 'info' || $type == 'multiinfo')) { + if ($this->version >= 2 && ($type == 'info' || $type == 'multiinfo')) { $row = array_merge($row, $this->get_extended_fields($row['ID'])); } - if ($type == 'info') { - $search_data = $row; - break; - } else { + if ($this->version < 3) { + if ($type == 'info') { + $search_data = $row; + break; + } else { + array_push($search_data, $row); + } + } elseif ($this->version >= 3) { array_push($search_data, $row); } } @@ -241,9 +256,9 @@ class AurJSON { return $this->json_error('Too many package results.'); } - return $this->json_results($type, $resultcount, $search_data); + return $this->json_results($type, $resultcount, $search_data, NULL); } else { - return $this->json_results($type, 0, array()); + return $this->json_results($type, 0, array(), NULL); } } |