Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit d42d241

Browse files
committed
ENH: refs #256. New framework for web API index page
1 parent be544cb commit d42d241

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

modules/api/AppController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Api_AppController extends MIDAS_GlobalModule
44
{
5-
public $moduleName='api';
5+
public $moduleName = 'api';
66

77
/**completion eclipse*/
88
/**

modules/api/controllers/IndexController.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ class Api_IndexController extends Api_AppController
2828
public $_components = array('Upload', 'Search', 'Uuid', 'Sortdao');
2929

3030
var $kwWebApiCore = null;
31-
32-
// Use this parameter to map API methods to your protected or private controller methods
3331
var $apicallbacks = array();
32+
var $helpContent = array();
3433

3534
// Config parameters
3635
var $apiEnable = '';
@@ -48,7 +47,6 @@ function preDispatch()
4847
$modulesConfig = Zend_Registry::get('configsModules');
4948
$this->apiSetup['apiMethodPrefix'] = $modulesConfig['api']->methodprefix;
5049

51-
$this->_setApiCallbacks($this->apiSetup['apiMethodPrefix']);
5250
$this->action = $actionName = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
5351
switch($this->action)
5452
{
@@ -68,6 +66,7 @@ function preDispatch()
6866
function indexAction()
6967
{
7068
$this->view->header = 'Web API';
69+
$this->_computeApiHelp($this->apiSetup['apiMethodPrefix']);
7170

7271
// Prepare the data used by the view
7372
$data = array(
@@ -81,10 +80,42 @@ function indexAction()
8180
$this->view->serverURL = $this->getServerURL();
8281
}
8382

84-
/** Set the call back API */
85-
private function _setApiCallbacks($apiMethodPrefix)
83+
/** This is called when calling a web api method */
84+
private function _computeApiCallback($method_name, $apiMethodPrefix)
85+
{
86+
$tokens = explode('.', $method_name);
87+
if(array_shift($tokens) != $apiMethodPrefix) //pop off the method prefix token
88+
{
89+
return; //let the web API core write out its method doesn't exist message
90+
}
91+
92+
$method = implode($tokens);
93+
if(method_exists($this, $method))
94+
{
95+
$this->apicallbacks[$method_name] = array(&$this, $method);
96+
}
97+
else //it doesn't exist here, check in the module specified by the 2nd token
98+
{
99+
$moduleName = array_shift($tokens);
100+
$moduleMethod = implode('', $tokens);
101+
$retVal = Zend_Registry::get('notifier')->callback('CALLBACK_API_METHOD_'.strtoupper($moduleName), array());
102+
// print_r($retVal);
103+
// exit;
104+
/*foreach($additionalMethods as $module => $methods)
105+
{
106+
foreach($methods as $method)
107+
{
108+
$this->helpContent[$apiMethodPrefix.strtolower($module).'.'.$method['name']] = $method['help'];
109+
$this->apicallbacks[$apiMethodPrefix.strtolower($module).'.'.$method['name']] = array($method['callbackObject'], $method['callbackFunction']);
110+
}
111+
}*/
112+
}
113+
}
114+
115+
/** This index function uses this to display the list of web api methods */
116+
private function _computeApiHelp($apiMethodPrefix)
86117
{
87-
$apiMethodPrefix = KwWebApiCore::checkApiMethodPrefix($apiMethodPrefix);
118+
$apiMethodPrefix = KwWebApiCore::checkApiMethodPrefix($apiMethodPrefix); //append the . if needed
88119

89120
$help = array();
90121
$help['params'] = array();
@@ -375,8 +406,8 @@ private function _setApiCallbacks($apiMethodPrefix)
375406
$this->helpContent[$apiMethodPrefix.'item.getmetadata'] = $help;
376407
$this->apicallbacks[$apiMethodPrefix.'item.getmetadata'] = array(&$this, 'itemGetMetadata');
377408

378-
// Extend web API to other modules via CALLBACK_API_METHODS
379-
$additionalMethods = Zend_Registry::get('notifier')->callback('CALLBACK_API_METHODS', array());
409+
// Get the lists from other modules
410+
$additionalMethods = Zend_Registry::get('notifier')->callback('CALLBACK_API_HELP', array());
380411
foreach($additionalMethods as $module => $methods)
381412
{
382413
foreach($methods as $method)
@@ -423,6 +454,7 @@ function restAction()
423454
}
424455

425456
$request_data = $this->_getAllParams();
457+
$this->_computeApiCallback($method_name, $this->apiSetup['apiMethodPrefix']);
426458
// Handle XML-RPC request
427459
$this->kwWebApiCore = new KwWebApiRestCore($this->apiSetup, $this->apicallbacks, $request_data);
428460
}
@@ -443,6 +475,7 @@ function jsonAction()
443475
}
444476

445477
$request_data = $this->_getAllParams();
478+
$this->_computeApiCallback($method_name, $this->apiSetup['apiMethodPrefix']);
446479
// Handle XML-RPC request
447480
$this->kwWebApiCore = new KwWebApiRestCore($this->apiSetup, $this->apicallbacks, array_merge($request_data, array('format' => 'json')));
448481
}

modules/api/library/APIEnabledNotification.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ApiEnabled_Notification extends MIDAS_Notification
2525
* $this->enableWebAPI();
2626
*
2727
*/
28-
public function getWebApiMethods()
28+
public function getWebApiHelp()
2929
{
3030
$methods = array();
3131
$r = new ReflectionClass($this->ModuleComponent->Api);
@@ -79,12 +79,27 @@ public function getWebApiMethods()
7979
return $methods;
8080
}
8181

82+
/**
83+
* Returns the actual method in your module corresponding to the requested method,
84+
* or false if the method doesn't exist
85+
*/
86+
public function findWebApiMethod($params)
87+
{
88+
$methodName = $params['methodName'];
89+
if(method_exists($this->ModuleComponent->Api, $methodName))
90+
{
91+
return array('object' => &$this->ModuleComponent->Api, 'method' => $methodName);
92+
}
93+
return false;
94+
}
95+
8296
/**
8397
* Add to your init function to enable the web api for your module. This will
8498
* work provided you've created an ApiComponent.
8599
*/
86-
public function enableWebAPI()
100+
public function enableWebAPI($moduleName)
87101
{
88-
$this->addCallBack('CALLBACK_API_METHODS', 'getWebApiMethods');
102+
$this->addCallBack('CALLBACK_API_HELP', 'getWebApiHelp');
103+
$this->addCallBack('CALLBACK_API_METHOD_'.strtoupper($moduleName), 'findWebApiMethod');
89104
}
90105
}

0 commit comments

Comments
 (0)