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

Commit

Permalink
ENH: refs #256. New framework for web API index page
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmullen committed Sep 19, 2011
1 parent be544cb commit d42d241
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion modules/api/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Api_AppController extends MIDAS_GlobalModule
{
public $moduleName='api';
public $moduleName = 'api';

/**completion eclipse*/
/**
Expand Down
49 changes: 41 additions & 8 deletions modules/api/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ class Api_IndexController extends Api_AppController
public $_components = array('Upload', 'Search', 'Uuid', 'Sortdao');

var $kwWebApiCore = null;

// Use this parameter to map API methods to your protected or private controller methods
var $apicallbacks = array();
var $helpContent = array();

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

$this->_setApiCallbacks($this->apiSetup['apiMethodPrefix']);
$this->action = $actionName = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
switch($this->action)
{
Expand All @@ -68,6 +66,7 @@ function preDispatch()
function indexAction()
{
$this->view->header = 'Web API';
$this->_computeApiHelp($this->apiSetup['apiMethodPrefix']);

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

/** Set the call back API */
private function _setApiCallbacks($apiMethodPrefix)
/** This is called when calling a web api method */
private function _computeApiCallback($method_name, $apiMethodPrefix)
{
$tokens = explode('.', $method_name);
if(array_shift($tokens) != $apiMethodPrefix) //pop off the method prefix token
{
return; //let the web API core write out its method doesn't exist message
}

$method = implode($tokens);
if(method_exists($this, $method))
{
$this->apicallbacks[$method_name] = array(&$this, $method);
}
else //it doesn't exist here, check in the module specified by the 2nd token
{
$moduleName = array_shift($tokens);
$moduleMethod = implode('', $tokens);
$retVal = Zend_Registry::get('notifier')->callback('CALLBACK_API_METHOD_'.strtoupper($moduleName), array());
// print_r($retVal);
// exit;
/*foreach($additionalMethods as $module => $methods)
{
foreach($methods as $method)
{
$this->helpContent[$apiMethodPrefix.strtolower($module).'.'.$method['name']] = $method['help'];
$this->apicallbacks[$apiMethodPrefix.strtolower($module).'.'.$method['name']] = array($method['callbackObject'], $method['callbackFunction']);
}
}*/
}
}

/** This index function uses this to display the list of web api methods */
private function _computeApiHelp($apiMethodPrefix)
{
$apiMethodPrefix = KwWebApiCore::checkApiMethodPrefix($apiMethodPrefix);
$apiMethodPrefix = KwWebApiCore::checkApiMethodPrefix($apiMethodPrefix); //append the . if needed

$help = array();
$help['params'] = array();
Expand Down Expand Up @@ -375,8 +406,8 @@ private function _setApiCallbacks($apiMethodPrefix)
$this->helpContent[$apiMethodPrefix.'item.getmetadata'] = $help;
$this->apicallbacks[$apiMethodPrefix.'item.getmetadata'] = array(&$this, 'itemGetMetadata');

// Extend web API to other modules via CALLBACK_API_METHODS
$additionalMethods = Zend_Registry::get('notifier')->callback('CALLBACK_API_METHODS', array());
// Get the lists from other modules
$additionalMethods = Zend_Registry::get('notifier')->callback('CALLBACK_API_HELP', array());
foreach($additionalMethods as $module => $methods)
{
foreach($methods as $method)
Expand Down Expand Up @@ -423,6 +454,7 @@ function restAction()
}

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

$request_data = $this->_getAllParams();
$this->_computeApiCallback($method_name, $this->apiSetup['apiMethodPrefix']);
// Handle XML-RPC request
$this->kwWebApiCore = new KwWebApiRestCore($this->apiSetup, $this->apicallbacks, array_merge($request_data, array('format' => 'json')));
}
Expand Down
21 changes: 18 additions & 3 deletions modules/api/library/APIEnabledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ApiEnabled_Notification extends MIDAS_Notification
* $this->enableWebAPI();
*
*/
public function getWebApiMethods()
public function getWebApiHelp()
{
$methods = array();
$r = new ReflectionClass($this->ModuleComponent->Api);
Expand Down Expand Up @@ -79,12 +79,27 @@ public function getWebApiMethods()
return $methods;
}

/**
* Returns the actual method in your module corresponding to the requested method,
* or false if the method doesn't exist
*/
public function findWebApiMethod($params)
{
$methodName = $params['methodName'];
if(method_exists($this->ModuleComponent->Api, $methodName))
{
return array('object' => &$this->ModuleComponent->Api, 'method' => $methodName);
}
return false;
}

/**
* Add to your init function to enable the web api for your module. This will
* work provided you've created an ApiComponent.
*/
public function enableWebAPI()
public function enableWebAPI($moduleName)
{
$this->addCallBack('CALLBACK_API_METHODS', 'getWebApiMethods');
$this->addCallBack('CALLBACK_API_HELP', 'getWebApiHelp');
$this->addCallBack('CALLBACK_API_METHOD_'.strtoupper($moduleName), 'findWebApiMethod');
}
}

0 comments on commit d42d241

Please sign in to comment.