web service MDL-12886 better looking documentation, use a lower integer for the mock token

This commit is contained in:
jerome
2009-03-04 03:18:14 +00:00
parent bb0e7937ec
commit fa0797ecf4
4 changed files with 131 additions and 69 deletions
+1 -36
View File
@@ -32,47 +32,12 @@ require_once('lib.php');
$protocol = optional_param('protocol',"soap",PARAM_ALPHA);
print_header(get_string('wspagetitle','webservice'), get_string('wspagetitle','webservice').":", true);
check_webservices($protocol);
webservice_lib::display_webservices_availability($protocol);
generate_documentation($protocol);
generate_functionlist();
print_footer();
/**
* Check if the Moodle site has the web service protocol enable
* @global object $CFG
* @param string $protocol
*/
function check_webservices($protocol){
global $CFG;
echo get_string('webservicesenable','webservice').": ";
if (empty($CFG->enablewebservices)) {
echo "<strong style=\"color:red\">".get_string('fail','webservice')."</strong>";
} else {
echo "<strong style=\"color:green\">".get_string('ok','webservice')."</strong>";
}
echo "<br/>";
foreach(webservice_lib::get_list_protocols() as $wsprotocol) {
if (strtolower($wsprotocol->get_protocolname()) == strtolower($protocol)) {
echo get_string('protocolenable','webservice',array($wsprotocol->get_protocolname())).": ";
if ( get_config($wsprotocol-> get_protocolname(), "enable")) {
echo "<strong style=\"color:green\">".get_string('ok','webservice')."</strong>";
} else {
echo "<strong style=\"color:red\">".get_string('fail','webservice')."</strong>";
}
echo "<br/>";
continue;
}
}
//check debugging
if ($CFG->debugdisplay) {
echo "<strong style=\"color:red\">".get_string('debugdisplayon','webservice')."</strong>";
}
}
/**
* Generate documentation specific to a protocol
+66 -24
View File
@@ -70,7 +70,7 @@ final class webservice_lib {
*/
public static function mock_check_token($token) {
//fake test
if ($token == 465465465468468464) {
if ($token == 456) {
///retrieve the user
global $DB;
$user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
@@ -134,7 +134,7 @@ final class webservice_lib {
-------
* Docnlock: @ subparam string $params:searches->search - the string to search
* $params is considered as the first element, searches the second, and search the terminal
* Except the terminal element, all other will be generated as an array
* Except the terminal element, all other will be generated as an array
* => left element are generated as an associative array.
* If the following character is ':' so the right element is a key named 'multiple:element_name'
* If the following character is '->' so the right element will be named 'element_name'
@@ -150,7 +150,7 @@ final class webservice_lib {
@ return array users
@ subreturn integer $users:user->id
@ subreturn integer $users:user->auth
Generated description array
---------------------------
description["mock_function"]=>
@@ -221,10 +221,10 @@ final class webservice_lib {
//retrieve the subparam and subreturn
preg_match_all('/\s*\*\s*@(subparam|subreturn)\s+(\w+)\s+(\$\w+(?::\w+|->\w+)+)((?:\s+(?:optional|required|multiple))*)/', $docBlock, $matches);
/// process every @subparam and @subreturn line of the docblock
/// process every @subparam and @subreturn line of the docblock
for($i=0;$i<sizeof($matches[1]);$i++){
/// identify the description type of the docblock line: is it params, optional or return (first key of a description method array)
/// identify the description type of the docblock line: is it params, optional or return (first key of a description method array)
switch ($matches[1][$i]) {
case "subparam":
if (strpos($matches[4][$i], "optional")!==false) {
@@ -238,12 +238,12 @@ final class webservice_lib {
break;
}
/// init description[method]
/// init description[method]
if (empty($description[$method->getName()])) {
$description[$method->getName()] = array();
}
/// directly set description[method][return] if the return value is a primary type
/// directly set description[method][return] if the return value is a primary type
if (strpos($returnmatches[1][0] ,"object")===false && strpos($returnmatches[1][0],"array")===false) {
$description[$method->getName()]['return'] = array($returnmatches[2][0] => $returnmatches[1][0]);
}
@@ -258,8 +258,8 @@ final class webservice_lib {
///Part 1.
/// extract the first part into $param (has to be $params in the case of @subparam, or anything in the case of $subreturn)
/// extract the second part
/// extract the first part into $param (has to be $params in the case of @subparam, or anything in the case of $subreturn)
/// extract the second part
if (strpos($matches[3][$i], "->")===false || (strpos($matches[3][$i], ":")!==false && (strpos($matches[3][$i], ":") < strpos($matches[3][$i], "->")))) {
$separator = ":";
$separatorsize=1;
@@ -270,27 +270,27 @@ final class webservice_lib {
}
$param = substr($matches[3][$i],1,strpos($matches[3][$i], $separator)-1); //first element/part/array
//for example for the line @subparam string $params:students->student->name
// @params is the first element/part/array of this docnlock line
// students is the second element/part/array
// ...
// name is the terminal element, this element will be generated as String here
//for example for the line @subparam string $params:students->student->name
// @params is the first element/part/array of this docnlock line
// students is the second element/part/array
// ...
// name is the terminal element, this element will be generated as String here
$otherparam = substr($matches[3][$i],strpos($matches[3][$i], $separator)+$separatorsize); //rest of the line
$parsingdesc = $description[$method->getName()]; //$pasingdesc is the current position of the algorythm into the description array
//it is used to check if a element already exist into the description array
//it is used to check if a element already exist into the description array
if (!empty($parsingdesc) && array_key_exists($descriptiontype, $parsingdesc)){
$parsingdesc = $parsingdesc[$descriptiontype];
}
$descriptionpath=array(); //we save in this variable the description path (e.g all keys to go deep into the description array)
//it will be used to know where to add a new part the description array
//it will be used to know where to add a new part the description array
$creationfinished = false; //it's used to stop the algorythm when we find a new element that we can add to the descripitoin
unset($type);
/// try to extract the other elements and add them to the descripition id there are not already in the description
/// try to extract the other elements and add them to the descripition id there are not already in the description
while(!$creationfinished && (strpos($otherparam, ":") || strpos($otherparam, "->"))) {
if (strpos($otherparam, "->")===false || (strpos($otherparam, ":")!==false && (strpos($otherparam, ":") < strpos($otherparam, "->")))) {
$type = $separator;
@@ -325,7 +325,7 @@ final class webservice_lib {
}
webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
$creationfinished = true; // we do not want to keep going to parse this line,
// neither add again the terminal element of the line to the descripiton
// neither add again the terminal element of the line to the descripiton
} else {
if(empty($descriptionpath)) {
$descriptionpath[] = $descriptiontype;
@@ -353,7 +353,7 @@ final class webservice_lib {
webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath);
$creationfinished = true; // we do not want to keep going to parse this line,
// neither add again the terminal element of the line to the descripiton
// neither add again the terminal element of the line to the descripiton
} else {
if(empty($descriptionpath)) {
$descriptionpath[] = $descriptiontype;
@@ -364,7 +364,7 @@ final class webservice_lib {
}
}
/// Add the "terminal" element of the line to the description array
/// Add the "terminal" element of the line to the description array
if (!$creationfinished) {
if (!empty($type) && $type==":") {
@@ -400,9 +400,9 @@ final class webservice_lib {
}
}
// echo "<pre>";
// var_dump($description);
// echo "</pre>";
// echo "<pre>";
// var_dump($description);
// echo "</pre>";
return $description;
}
@@ -481,6 +481,48 @@ final class webservice_lib {
}
/**
* Check if the Moodle site has the web service protocol enable
* @global object $CFG
* @param string $protocol
*/
function display_webservices_availability($protocol){
global $CFG;
$available = true;
echo get_string('webservicesenable','webservice').": ";
if (empty($CFG->enablewebservices)) {
echo "<strong style=\"color:red\">".get_string('fail','webservice')."</strong>";
$available = false;
} else {
echo "<strong style=\"color:green\">".get_string('ok','webservice')."</strong>";
}
echo "<br/>";
foreach(webservice_lib::get_list_protocols() as $wsprotocol) {
if (strtolower($wsprotocol->get_protocolname()) == strtolower($protocol)) {
echo get_string('protocolenable','webservice',array($wsprotocol->get_protocolname())).": ";
if ( get_config($wsprotocol-> get_protocolname(), "enable")) {
echo "<strong style=\"color:green\">".get_string('ok','webservice')."</strong>";
} else {
echo "<strong style=\"color:red\">".get_string('fail','webservice')."</strong>";
$available = false;
}
echo "<br/>";
continue;
}
}
//check debugging
if ($CFG->debugdisplay) {
echo "<strong style=\"color:red\">".get_string('debugdisplayon','webservice')."</strong>";
$available = false;
}
return $available;
}
}
/**
@@ -528,7 +570,7 @@ class ws_authentication {
*/
function tmp_get_token($params) {
if ($params['username'] == 'wsuser' && $params['password'] == 'wspassword') {
return '465465465468468464';
return '456';
} else {
throw new moodle_exception('wrongusernamepassword');
}
+2 -2
View File
@@ -57,7 +57,7 @@ function call_moodle_function ($rest_arguments) {
} else {
/// TODO: authentication + token generation need to be implemented
if (optional_param('username',null,PARAM_ALPHANUM) == 'wsuser' && optional_param('password',null,PARAM_ALPHANUM) == 'wspassword') {
return '465465465468468464';
return '456';
} else {
throw new moodle_exception('wrongusernamepassword');
}
@@ -113,7 +113,7 @@ function call_moodle_function ($rest_arguments) {
*/
function mock_check_token($token) {
//fake test
if ($token == 465465465468468464) {
if ($token == 456) {
///retrieve the user
global $DB;
$user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1));
@@ -29,31 +29,86 @@
*/
require_once('../../../config.php');
require_once('../lib.php');
include "Zend/Loader.php";
Zend_Loader::registerAutoload();
print_header('Soap test client', 'Soap test client'.":", true);
if (!webservice_lib::display_webservices_availability("soap")) {
echo "<br/><br/>";
echo "Please fix the previous problem(s), the testing session has been interupted.";
print_footer();
exit();
}
//1. authentication
$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?wsdl");
try {
$token = $client->tmp_get_token(array('username' => "wsuser", 'password' => "wspassword"));
printLastRequestResponse($client);
print "<pre>\n";
var_dump($token);
print "</pre>";
} catch (moodle_exception $exception) {
echo $exception;
}
//2. test functions
//$client = new Zend_Http_Client($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl", array(
// 'maxredirects' => 0,
// 'timeout' => 30));
//$response = $client->request();
//$wsdl = $response->getBody();
//varlog($wsdl,"user.wsdl", "w");
$client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl");
var_dump($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl");
print "<pre>\n";
var_dump($client->tmp_get_users(array('search' => "admin")));
printLastRequestResponse($client);
print "</pre>";
//$param = array('search' => "admin");
//$expectedresult = array(array( 'id' => 2,
// 'auth' => 'manual',
// 'confirmed' => '1',
// 'username' => 'admin',
// 'idnumber' => '',
// 'firstname' => 'Admin',
// 'lastname' => 'HEAD',
// 'email' => '[email protected]',
// 'emailstop' => '0',
// 'lang' => 'en_utf8',
// 'theme' => '',
// 'timezone' => '99',
// 'mailformat' => '1'));
//$functionname = tmp_get_users;
//call_soap_function($client,$functionname,$param,$expectedresult);
print "<pre>\n";
var_dump($client->tmp_create_user(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "[email protected]",'password' => "password6")));
printLastRequestResponse($client);
var_dump($client->tmp_update_user(array('username' => "mockuser66",'mnethostid' => 1,'newusername' => "mockuser6b",'firstname' => "firstname6b")));
printLastRequestResponse($client);
print "</pre>";
print "<pre>\n";
var_dump($client->tmp_update_user(array('mnethostid' => 1,'username' => "mockuser66",'newusername' => "mockuser6b",'firstname' => "firstname6b")));
print "</pre>";
print "<pre>\n";
var_dump($client->tmp_delete_user(array('username' => "mockuser6b",'mnethostid' => 1)));
printLastRequestResponse($client);
print "</pre>";
print "<pre>\n";
var_dump($client->tmp_do_multiple_user_searches(array(array('search' => "jerome"),array('search' => "mock"))));
printLastRequestResponse($client);
print "</pre>";
print_footer();
//function call_soap_function($client,$functionname,$param,$expectedresult) {
// print "<pre>\n";
// var_dump($client->$functionname($param));
// print "</pre>";
//}
function printLastRequestResponse($client) {
print "<pre>\n";