Web Services Helper

Categories

Component ID

2293981

Component name

Web Services Helper

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Component created

Component changed

Component body

Classes to ease the web services creation using OOP.

In short

If you are using an IDE like Netbeans this set of classes and interfaces will help you a lot in creating a web service and its resources. Netbeans, like other IDEs, will alert you if you forget to implement some necessary method and, with the code completion feature, in defining your resources. With the Drupal standard way, with arrays, you must keep in your head, or in a browser page, all the necessary array keys for each option you need to specify.

Example

This is the way to define a web service with one resource, MyArticles:

/**
 * Definition of the Example Web Service
 */
class ExampleService implements WebServiceInterface {
  public static function getResourceClasses() {
    return array('MyArticles');
  }

  public static function getServiceName() {
    return 'example';
  }

  public static function getEndpointPath() {
    return 'api/example/v1';
  }
}

This is the way to define the MyArticles resource:

class MyArticles extends WebResourceBase {
  const CONTENT_TYPE = 'article';

  /**
   * Gets the resource name.
   * @return string Resource name.
   */
  public static function getResourceName() {
    return 'articles';
  }


  /**
   * Defines the two operations available to retrieve articles.
   * Operations: index & retrieve
   * @return array
   */
  public static function getCrudOperations() {
    $index = new WebResourceOperation('index', t('Retrieves a listing of all article nodes.'));
    $index->setParamArgument('start', 'int', TRUE, 0);
    $index->setParamArgument('limit', 'int', TRUE, self::MAX_NODES);

    $retrieve = new WebResourceOperation('retrieve', t('Retrieves an article details.'));
    $retrieve->setPathArgument('nid', 0, 'int');  // The ID of the node to retrieve

    return array($index, $retrieve);
  }

  /**
   * Returns a list of articles
   * @param int $start Node index in which start the query.
   * @param int $limit Quantity of nodes to retrieve.
   * @return array
   */
  protected function index($start, $limit) {
    $query = db_select('node', 'n');
    $query->fields('n');

    $query->condition('n.type', self::CONTENT_TYPE);
    $query->condition('n.status', 1);
...

Notes

See the attached example module for usage instructions.

PS: As you can imagine at this time, English is not my native language :)