Wiki

Clone wiki

moodle_thrift_tools / Home

Use Moodle Web services with Apache Thrift

The purpose of this project is to provide the tools required to use Moodle Web services with Apache Thrift. This package contains the following applications:

  • Thrift file generator (thriftgenerator), used to generate .thrift files that match the Moodle Web services API.
  • Wrapper generator (wrappergenerator), used to generate PHP implementations of Thrift generated interfaces that redirect the calls to Moodle Web service handlers.
  • Documentation generator (docgenerator), used to generate Web service documentation in HTML format from Moodle source code.

Below is described how to use these applications to set up Moodle with Apache Thrift:

Prerequisites

  1. Make sure your environment is capable of running Moodle and Apache Thrift (please refer to the websites of these projects for details).
  2. Download and install Apache Thrift 0.9.0+: http://thrift.apache.org/ (Note: use 0.9.0-dev (or later) version of thrift, as 0.8.0 doesn't support JSON in PHP, which is required for JavaScript clients.
  3. Download and unpack Moodle 2.3.2+ sources: http://download.moodle.org/

Steps to set up Moodle

  1. Preparations: create directory "thrift" inside moodle/local and copy directory "Thrift" from thrift/lib/php/lib to that directory (or make symbolic link). Create directory "packages" inside moodle/local/thrift (you'll have "Thrift" and "packages" directories under moodle/local/thrift).
  2. Edit config.php (in this package) to specify the directories where moodle source is unpacked and where to put/find the generated .thrift and .php files.
  3. Generate .thrift files for Moodle API using thriftgenerator (go to thriftgenerator directory and run php -f generate.php).
  4. Generate thrift handlers for PHP, e.g.: for i in *.thrift; do thrift --gen php:server $i; done and copy the packages from gen-php to moodle/local/thrift/packages
  5. Generate API wrappers using wrappergenerator (go to wrappergenerator directory and run php -f generate.php) and place the .php files in moodle/local/thrift.
  6. That is all, Moodle part is ready.

Using the API on client side

  1. Preparations: include thrift library into client (for JavaScript, it can be found here: thrift/lib/js/thrift.js).
  2. Generate client classes for any language as required from the .thrift files e.g.: for i in *.thrift; do thrift --gen js:jquery $i; done
  3. Include the generated files to your client application.
  4. Setup client class to use JSON protocol with the following URL: http://<moodle>/local/thrift/<package_name>.php?token=<user_token> e.g.: var helloClient = new local_wstemplateClient(new Thrift.Protocol(new Thrift.Transport("http://localhost/mmoodle/local/thrift/local_wstemplate.php?token=" + token)))
  5. Client class is ready to use. E.g.: helloClient.hello_world("Howdy!")

Documentation

Optionally, you can generate the API documentation in HTML format, with type names used by Thrift. To do this, go to docgenerator directory and run php -f generate.php - the documentation will be in the output directory, under doc subdirectory.

Updated