From 140141edf2c5d89f083b4d254c0533e0209d517b Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Wed, 12 Dec 2012 16:50:25 +0000 Subject: [PATCH] API: Further tidying, implement OC_OCS_Result object for api results. --- lib/api.php | 69 ++++++---------------------------------------- lib/ocs/result.php | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 60 deletions(-) create mode 100644 lib/ocs/result.php diff --git a/lib/api.php b/lib/api.php index cc1d9fccaff..e119b878210 100644 --- a/lib/api.php +++ b/lib/api.php @@ -71,7 +71,7 @@ class OC_API { ->action('OC_API', 'call'); self::$actions[$name] = array(); } - self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authlevel); + self::$actions[$name] = array('app' => $app, 'action' => $action, 'authlevel' => $authlevel); } /** @@ -87,22 +87,11 @@ class OC_API { } $name = $parameters['_route']; // Loop through registered actions - foreach(self::$actions[$name] as $action){ - $app = $action['app']; - // Authorise this call - if(self::isAuthorised($action)){ - if(is_callable($action['action'])){ - $responses[] = array('app' => $app, 'response' => call_user_func($action['action'], $parameters)); - } else { - $responses[] = array('app' => $app, 'response' => 501); - } - } else { - $responses[] = array('app' => $app, 'response' => 401); - } - - } - // Merge the responses - $response = self::mergeResponses($responses); + if(is_callable(self::$actions[$name]['action'])){ + $response = call_user_func(self::$actions[$name]['action'], $parameters); + } else { + $response = new OC_OCS_Result(null, 998, 'Internal server error.'); + } // Send the response $formats = array('json', 'xml'); $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; @@ -168,53 +157,13 @@ class OC_API { return OC_User::login($authuser, $authpw) ? $authuser : false; } - /** - * intelligently merges the different responses - * @param array $responses - * @return array the final merged response - */ - private static function mergeResponses($responses){ - $finalresponse = array( - 'meta' => array( - 'statuscode' => '', - ), - 'data' => array(), - ); - $numresponses = count($responses); - - foreach($responses as $response){ - if(is_int($response['response']) && empty($finalresponse['meta']['statuscode'])){ - $finalresponse['meta']['statuscode'] = $response['response']; - continue; - } - if(is_array($response['response'])){ - // Shipped apps win - if(OC_App::isShipped($response['app'])){ - $finalresponse['data'] = array_merge_recursive($finalresponse['data'], $response['response']); - } else { - $finalresponse['data'] = array_merge_recursive($response['response'], $finalresponse['data']); - } - $finalresponse['meta']['statuscode'] = 100; - } - } - //Some tidying up - if($finalresponse['meta']['statuscode']=='100'){ - $finalresponse['meta']['status'] = 'ok'; - } else { - $finalresponse['meta']['status'] = 'failure'; - } - if(empty($finalresponse['data'])){ - unset($finalresponse['data']); - } - return array('ocs' => $finalresponse); - } - /** * respond to a call - * @param int|array $response the response + * @param int|array $result the result from the api method * @param string $format the format xml|json */ - private static function respond($response, $format='xml'){ + private static function respond($result, $format='xml'){ + $response = array('ocs' => $result->getResult()); if ($format == 'json') { OC_JSON::encodedPrint($response); } else if ($format == 'xml') { diff --git a/lib/ocs/result.php b/lib/ocs/result.php new file mode 100644 index 00000000000..a7199cb5ac7 --- /dev/null +++ b/lib/ocs/result.php @@ -0,0 +1,65 @@ +data = $data; + $this->statuscode = $code; + $this->message = $message; + } + + /** + * sets the statuscode + * @param $code int + */ + public function setCode(int $code){ + $this->statuscode = $code; + } + + /** + * optionally set the total number of items available + * @param $items int + */ + public function setItems(int $items){ + $this->items = $items; + } + + /** + * optionally set the the number of items per page + * @param $items int + */ + public function setItemsPerPage(int $items){ + $this->perpage = $items; + } + + /** + * set a custom message for the response + * @param $message string the message + */ + public function setMessage(string $message){ + $this->message = $message; + } + + /** + * returns the data associated with the api result + * @return array + */ + public function getResult(){ + $return = array(); + $return['meta'] = array(); + $return['meta']['status'] = ($this->statuscode === 100) ? 'ok' : 'failure'; + $return['meta']['statuscode'] = $this->statuscode; + $return['meta']['message'] = $this->message; + $return['data'] = $this->data; + // Return the result data. + return $return; + } + + +} \ No newline at end of file -- 2.39.5