External services description: Difference between revisions
Line 4: | Line 4: | ||
This document explains how we describe and where we store the web service functions in Moodle 2.0. | This document explains how we describe and where we store the web service functions in Moodle 2.0. | ||
We store function descriptions in the | We store function descriptions in the component/db/services.php file. Web services functions will be located in servicelib.php files - this file name is not mandatory, but is strongly recommended. | ||
Descriptions will be parsed by a service discovery function that will fill the database (extend the current discovery functionalities looking parsing the DB folders). | |||
Descriptions will be | |||
Revision as of 09:44, 12 September 2009
Moodle 2.0
Purpose
This document explains how we describe and where we store the web service functions in Moodle 2.0.
We store function descriptions in the component/db/services.php file. Web services functions will be located in servicelib.php files - this file name is not mandatory, but is strongly recommended.
Descriptions will be parsed by a service discovery function that will fill the database (extend the current discovery functionalities looking parsing the DB folders).
Administration pages
Moodle administrators will be able to manage services . By default all services will be disabled.
List of administration functionalities:
- enable a service (all the functions into this service will be available)
- associate a web service user (the user has web service capability) to some services => a user can only call a service that he is associated with
- create a custom service (add and remove functions from this service)
- search easily function by component in order to create a custom service
Administrators will also be able to create a custom service, selecting any existing functions.
external_methods table
List of web service functions.
Field | Type | Default | Description |
---|---|---|---|
id | int(10) | auto-incrementing | |
classname | varchar(255) | name of the class containing the function | |
method | varchar(150) | function name: get_users, create_users,... | |
classpath | varchar(255) | location where function defined - Needed for identifying | |
component | varchar(100) | Component where class method defined, needed for automatic updates. Service description file is found in the component db folder. | |
requiredlogin | int(1) | extra information - some external functions may not require $USER |
Detailed method description is stored as a complex PHP array in db/services.php file.
external_services table
Service is defined as a group of web service functions.
Field | Type | Default | Description |
---|---|---|---|
id | int(10) | auto-incrementing | |
name | varchar(150) | Name of service (gradeexport_xml_export, mod_chat_export) - appears on the admin page | |
enabled | int(1) | 0 | service enabled, for security reasons some services may be disabled - The only editable field by the admin |
custom | int(1) | 0 | Custom local service, created manually by admin or some other way. |
customname | varchar(150) | As a custom service name cannot be found into lang files, we need a field for that | |
requiredcapabilities | varchar(255) | capability ids (separate by comma) | |
(version) | varchar(10) | version string - it should be implemented later |
Note about Petr's Proposal:
- requiredCapability, we will test if a user has the right to use the service checking that an association exists.
external_services_functions table
Specifies functions used by services.
Field | Type | Default | Description |
---|---|---|---|
id | int(10) | auto-incrementing | |
externalserviceid | int(10) | foreign key, reference external_services.id | |
externalfunctionid | int(10) | foreign key, reference external_functions.id |
external_services_users table
Specifies services used by users.
Field | Type | Default | Description |
---|---|---|---|
id | int(10) | auto-incrementing | |
externalserviceid | int(10) | foreign key, reference external_services.id | |
userid | int(10) | foreign key, reference user.id |
PHP array description
why a web service description
- in order to generate the WSDL for SOAP
- in order to generate documentation for protocol as REST or XMLRPC
- in order to clean the params automatically
code template
$object = new object();
$object->property1 = PARAM_NUMBER;
$object->property2 = PARAM_ALPHA;
$params = new object();
$params->objects = array($object);
$params->object = $object;
$params->primarytype = PARAM_ALPHANUM;
$return = new object();
$return->values = array(PARAM_NUMBER);
$return->value = PARAM_NUMBER;
$functions = array(
'classname_external:functionname' => array(
'path' => 'servicelibpath',
'requiredlogin' => 0,
'params' => $params,
'optionalinformation' => 'Human readable text: "object" and "primarytype" parameters are optionals',
'return' => $return,
'capabilities' => array()
),
'classname_external:secondfunctionname' => array(
'path' => 'servicelibpath',
'requiredlogin' => 1,
'params' => $params,
'optionalinformation' => 'Human readable text: "object" and "primarytype" parameters are optionals',
'return' => $return,
'capabilities' => array()
)
);
$services = array(
'servicename' => array(
'functions' => array ('functioname' => 'componentname', 'functionname2' => 'componentname2'),
'capabilities' => array()
)
);
Note about the code:
- component: the component is not defined into the function description cause it will get the name of the plugin (if it's not a plugin, so the name will be 'core')
- service: the services can reference any functions (do not depend of this plugin) => the web services dev needs to know the component name format!
Function implementation
The function are store into servicelib.php classes. These classes can be found anywhere and are referenced by the servicelibpath into the function description. However it would be a common practice to place them in obvious places like a plugin lib folder.
final class componentname_service {
static public function functioname ($params) {
clean_function_params('functioname', $params);
/// specific function code
/// ...
}
}
Way to fill the database tables
The Moodle upgrade will take care of the updates. We will need to add a function into lib/upgradelib.php/upgrade_plugins() that will check all web service description.
Return values are filtered by the servers
Web service function should be able to return whatever they want. Each server should be looking at the web services description and returns the expected values.
Petr's Proposal
It can be found here.