I am wondering if 'actions', 'targeted actions' and 'relationships' are simple logical divisions of services or are they treated differently at the code level. I have been working through some services tutorials and none of them mention how to create 'actions', 'targeted actions' and 'relationships'. The only info I have found specifically thus far is from docs/services.services.api.php :

* Actions are performed directly on the resource type, not a individual
* resource. The following example is hypothetical (but plausible). Say
* that you want to expose a API for the apachesolr module. One of the
* things that could be exposed is the functionality to reindex the whole
* site at apachesolr/reindex.
*
* Targeted actions acts on a individual resource. A good, but again -
* hypothetical, example would be the publishing and unpublishing of nodes
* at node/123/publish.
*
* Relationship requests are convenience methods (sugar) to get something
* thats related to a individual resource. A real example would be the
* ability to get the files for a node at node/123/files.

I am wondering if these different service types are used more as a convention for categorize services rather than being treated differently at coding level. Would a service defined under 'target_action' behave any differently if I move it to 'relationship'?

Comments

kylebrowning’s picture

Status: Active » Closed (fixed)

IN rest serve module they are handled differently.

Targeted actions are always POST and required a 'context' in between the resource the ID and the action.

relationships are always GET's

monta’s picture

Hi,

I defined a REST endpoint with targeted_actions to be called via a POST request, but this didn't work for me.
When I call http://mysite.local/api/lead I got "Services Endpoint "lead" has been setup successfully." all the time because the canonical path is empty.

My endpoint path is: api/lead (please see the export below)

In my resource.inc I have the following

function _ea_services_resource_definition() {
  $definition = array(
    'lead' => array(
      'operations' => array(),
      'targeted_actions' => array(
        'create' => array(
          'help' => 'Create a lead',
          'file' => array('type' => 'inc', 'module' => 'ea_services', 'name' => 'resources/resource'),
          'callback' => '_ea_services_lead_create',
          'access callback' => 'user_access',
          'access arguments'=> array('access content'),
          'access arguments append' => TRUE,
          'args' => array(
            array(
              'name' => 'op',
              'type' => 'string',
              'description' => 'Lead opeartion type',
              'optional' => FALSE,
              'source' => array('path' => 0),
            ),
            array(
              'name' => 'lead',
              'type' => 'string',
              'description' => 'JSON array representation of a lead',
              'optional' => FALSE,
              'source' => array('data' => 'lead'),
            ),
            array(
              'name' => 'product_type',
              'type' => 'string',
              'description' => 'Product type',
              'source' => 'product_type',
              'optional' => FALSE,
              'source' => array('data' => 'product_type'),
            ),
            array(
              'name' => 'service_type',
              'type' => 'string',
              'description' => "Service type as 'tariff', 'devis' or 'souscription'",
              'source' => 'service_type',
              'optional' => FALSE,
              'source' => array('data' => 'service_type'),
            ),
          ),
        ),
      ),
    ),
  );

  return $definition;
}

function _ea_services_lead_create($op, $lead, $product_type, $service_type) {
  // stuff ...
  // ..
  return $lead;
}

// resource export

$endpoint = new stdClass();
$endpoint->disabled = FALSE; /* Edit this to true to make a default endpoint disabled initially */
$endpoint->api_version = 3;
$endpoint->name = 'lead';
$endpoint->server = 'rest_server';
$endpoint->path = 'api/lead';
$endpoint->authentication = array();
$endpoint->server_settings = array(
  'formatters' => array(
    'bencode' => TRUE,
    'json' => TRUE,
    'jsonp' => TRUE,
    'php' => TRUE,
    'xml' => TRUE,
    'yaml' => TRUE,
  ),
  'parsers' => array(
    'application/json' => TRUE,
    'application/vnd.php.serialized' => TRUE,
    'application/x-yaml' => TRUE,
    'application/xml' => TRUE,
    'multipart/form-data' => TRUE,
    'text/xml' => TRUE,
    'application/x-www-form-urlencoded' => FALSE,
  ),
);
$endpoint->resources = array(
  'lead' => array(
    'targeted_actions' => array(
      'create' => array(
        'enabled' => '1',
      ),
    ),
  ),
);
$endpoint->debug = 0;

Thank you for your help,