phpdrupaldrupal-7drupal-services

Drupal 7 - cannot get services endpoints to show


I am at my wit's end trying to decipher why this isn't working. I'm attempting to set up a REST server from my Drupal 7 instance. Currently focusing on no authentication required (I'll set that up once I manage to get it talking).

Here's the relevant code bits:

mymodule.module

/**
 * Implements hook_ctools_plugin_api().
 * Declares the mymodule services endpoint (stored in
 * mymodule.services.inc).  Note that the referenced ctools
 * hook obviates creating this endpoint through the UI.
 */
function mymodule_ctools_plugin_api($owner, $api) {
  if ($owner == 'services' && $api == 'services') {
    return array(
      'version' => 3,
      'file' => 'mymodule.services.inc',
      'path' => drupal_get_path('module', 'mymodule'),
    );
  }
}
/**
 * Implements hook_services_resources
 * Defines the resources available via services module (REST, in this case).
 *
 * @return array The definition array
 */
function mymodule_services_resources() {
  watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link');
    return array(
      '#api_version' => 3001,
      'test' => array(
        'retrieve' => array(
          'help' => t("A test of the REST api"),
          'file' => array(
            'type' => 'inc',
            'module' => 'mymodule',
            'name' => 'resources/test_resource',
          ),
          'access arguments'=>array('access content'),
          'callback' => 'mymodule_services_test',
          'args' => array(
            array(
              'name' => 'id',
              'type' => 'int',
              'description' => t("a test of rest arguments"),
              'source' => array('path' => '0'),
              'optional' => FALSE,
            ),
          ),
        ),
        'index' => array(
          'help' => t('A test of the REST api index functionality'),
          'file' => array(
            'type' => 'inc',
            'module' => 'mymodule',
            'name' => 'resources/test_resource',
          ),
          'callback' => 'mymodule_services_test',
        ),

      ),
    );
}

resources/test_resources.inc

/**
 *
 */
function mymodule_services_test() {
  watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link');
  $result = array('foo' => 'bar');
  drupal_json_output($result);
}
/**
 * Access callback for test services (currently unused)
 * @param  string $op   The operation being performed: creat
 * @param  [type] $args [description]
 * @return [type]       [description]
 */
function mymodule_services_test_access($op, $args) {
  watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link');
  return TRUE;
}

mymodule.services.inc

/**
 * @file
 */

/**
 * Implements hook_default_services_endpoint().
 */
function mymodule_default_services_endpoint() {
  watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link');
  $endpoint = new stdClass();
  $endpoint->disabled = FALSE;  //Edit this to true to make a default endpoint disabled initially
  $endpoint->api_version = 3;
  $endpoint->name = 'mymodule_rest_api_v1';
  $endpoint->server = 'rest_server';
  $endpoint->path = 'api/mymodule/v1';
  $endpoint->authentication = array();
  $endpoint->server_settings = array(
    'formatters' => array(
      'json' => TRUE,
      'bencode' => FALSE,
      'jsonp' => FALSE,
      'php' => FALSE,
      'xml' => FALSE,
    ),
    'parsers' => array(
      'application/json' => TRUE,
      'text/xml' => TRUE,
      'application/vnd.php.serialized' => FALSE,
      'application/x-www-form-urlencoded' => FALSE,
      'application/xml' => FALSE,
      'multipart/form-data' => FALSE,
    ),
  );
  $endpoint->resources = array();
  $endpoint->debug = 0;
  return array('mymodule'=>$endpoint);
}

I removed the UI-built definition of this service, and can see that it was successfully recreated from code, plus, when I access 'api/mymodule/v1/', I see the message 'Services Endpoint "mymodule_rest_api_v1" has been setup successfully.', so I know that hook_default_services_endpoint and hook_ctools_plugin_api are working correctly.

For whatever reason, though, I can't get any of the paths defined in mymodule_services_resources to recognize as valid. I've removed all access restraints, cleared the cache multiple times - always, I end up with a 404 not found when I hit up any url ending with 'test' (e.g. https://[my-domain]/api/mymodule/v1/test and https://[my-domain]/api/mymodule/v1/test/1 both yield "Not found : Could not find resource test.").

Any advice would be appreciated.


Solution

  • Figured it out. Posting back why in case anyone else ever runs across this.

    hook_services_resources declares the resources provided by the module. What it does NOT do is enable those resources. Going back under the resources tab for my service endpoint, I discovered "test" was available as a new resource that can be exposed. More to the point, it was not enabled.

    After enabling it and exporting it to code, I was able to change the contents of mymodule_default_services_endpoint to this:

    function mymodule_default_services_endpoint() {
      watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link');
      $endpoint = new stdClass();
      $endpoint->disabled = FALSE; /* Edit this to true to make a default endpoint disabled initially */
      $endpoint->api_version = 3;
      $endpoint->name = 'mymodule_rest_api_v1';
      $endpoint->server = 'rest_server';
      $endpoint->path = 'api/mymodule/v1';
      $endpoint->authentication = array(
        'services' => 'services',
      );
      $endpoint->server_settings = array(
        'formatters' => array(
          'json' => TRUE,
          'bencode' => FALSE,
          'jsonp' => FALSE,
          'php' => FALSE,
          'xml' => FALSE,
        ),
        'parsers' => array(
          'application/json' => TRUE,
          'text/xml' => TRUE,
          'application/vnd.php.serialized' => FALSE,
          'application/x-www-form-urlencoded' => FALSE,
          'application/xml' => FALSE,
          'multipart/form-data' => FALSE,
        ),
      );
      $endpoint->resources = array(
        'test' => array(
          'operations' => array(
            'retrieve' => array(
              'enabled' => '1',
            ),
            'index' => array(
              'enabled' => '1',
            ),
          ),
        ),
      );
      $endpoint->debug = 0;
      return array('mymodule' => $endpoint);
    }
    

    Now I'm getting authentication errors, which I expected and can deal with. Hope this helps someone.