aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>2009-11-12 18:55:44 +0000
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>2009-11-12 18:55:44 +0000
commitf22a2760e794ad4d863d817543ecfdc22670809d (patch)
treec6ada531c3822bac6006ae9c1f954a3263f98cc9
parent146ac58cdb0c28b1ab25d5d93b1b753ddbd20c11 (diff)
downloadelgg-f22a2760e794ad4d863d817543ecfdc22670809d.tar.gz
elgg-f22a2760e794ad4d863d817543ecfdc22670809d.tar.bz2
rest api parameters can now have default values
git-svn-id: http://code.elgg.org/elgg/trunk@3674 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r--engine/lib/api.php24
1 files changed, 16 insertions, 8 deletions
diff --git a/engine/lib/api.php b/engine/lib/api.php
index b3da52c5a..4080a2ab1 100644
--- a/engine/lib/api.php
+++ b/engine/lib/api.php
@@ -310,11 +310,14 @@ $API_METHODS = array();
*
* @param string $method The api name to expose - for example "myapi.dosomething"
* @param string $function Your function callback.
- * @param array $parameters (optional) list of parameters in the same order as in your function, with optional parameters last.
+ * @param array $parameters (optional) List of parameters in the same order as in your function.
+ * Default values may be set for parameters which would allow REST api users flexibility in
+ * what parameters are passed. Generally, optional parameters should be after required parameters.
* This array should be in the format
* "variable" = array (
* type => 'int' | 'bool' | 'float' | 'string' | 'array'
* required => true (default) | false
+ * default => value (optional)
* )
* @param string $description (optional) human readable description of the function.
* @param string $call_method (optional) Define what http method must be used for this function. Default: GET
@@ -332,10 +335,12 @@ function expose_function($method, $function, array $parameters = NULL, $descript
// does not check whether this method has already been exposed - good idea?
$API_METHODS[$method] = array();
+ $API_METHODS[$method]["description"] = $description;
+
// does not check whether callable - done in execute_method()
$API_METHODS[$method]["function"] = $function;
- if ($parameters != NULL) {
+ if ($parameters != NULL) {
if (!is_array($parameters)) {
throw new InvalidParameterException(sprintf(elgg_echo('InvalidParameterException:APIParametersArrayStructure'), $method));
}
@@ -371,8 +376,6 @@ function expose_function($method, $function, array $parameters = NULL, $descript
throw new InvalidParameterException(sprintf(elgg_echo('InvalidParameterException:UnrecognisedHttpMethod'), $call_method, $method));
}
- $API_METHODS[$method]["description"] = $description;
-
$API_METHODS[$method]["require_api_auth"] = $require_api_auth;
$API_METHODS[$method]["require_user_auth"] = $require_user_auth;
@@ -510,9 +513,14 @@ function get_parameters_for_method($method) {
// if there are parameters, sanitize them
if (isset($API_METHODS[$method]['parameters'])) {
foreach ($API_METHODS[$method]['parameters'] as $k => $v) {
- $v = get_input($k); // Make things go through the sanitiser
- if ($v !== '') {
- $sanitised[$k] = $v;
+ $param = get_input($k); // Make things go through the sanitiser
+ if ($param !== '') {
+ $sanitised[$k] = $param;
+ } else {
+ // parameter wasn't passed so check for default
+ if (isset($v['default'])) {
+ $sanitised[$k] = $v['default'];
+ }
}
}
}
@@ -548,7 +556,7 @@ function include_post_data() {
if (is_array($query_arr)) {
foreach($query_arr as $name => $val) {
set_input($name, $val);
- }
+ }
}
}
}