Posted by -enzo- on February 22, 2012 at 1:37am
Hello Folks
I'm trying to code a new service in Drupal 7 using the following code.
function exchange_rate_services_services_resources() {
return array(
'exchange_rate' => array(
'retrieve' => array( 'help' => 'Retrieves the exchange rate',
'callback' => '_exchange_rate_services_get_exchange_rate',
'args' => array(
array( 'name' => 'country',
'type' => 'string',
'description' => 'Country Id',
'source' => array( 'path' => '0' ),
'optional' => FALSE,
),
array( 'name' => 'currency',
'type' => 'string',
'description' => 'Currency Id',
'source' => array( 'path' => '0' ),
'optional' => FALSE,
),
array( 'name' => 'date',
'type' => 'int',
'description' => 'Date in Timestamp',
'source' => array( 'path' => '0' ),
'optional' => FALSE,
)
),
)
)
);
}
function _exchange_rate_services_get_exchange_rate( $country, $currency, $date ) {
$ret = new stdClass();
$ret->bank = 'BCCR';
$ret->buy = '500';
$ret->sell = '524';
break;
return $ret;
}
unction exchange_rate_services_default_services_endpoint() {
$endpoints = array( );
$endpoint = new stdClass;
$endpoint->disabled = FALSE; /* Edit this to true to make a default endpoint disabled initially */
$endpoint->api_version = 3;
$endpoint->name = 'exchange_rate';
$endpoint->server = 'xmlrpc_server';
$endpoint->path = 'exchange_rate/services';
$endpoint->authentication = array( );
$endpoint->server_settings = array( );
$endpoint->resources = array(
'exchange_rate' => array(
'operations' => array(
'retrieve' => array(
'enabled' => 1,
),
),
),
);
$endpoint->debug = 1;
$endpoints[ ] = $endpoint;
return $endpoints;
}
function exchange_rate_services_ctools_plugin_api($owner, $api) {
if ($owner == 'services' && $api == 'services') {
return array(
'version' => 3
);
}
}
The service is detected by ctools interface for end points, but when I try to call the service the method is not available.
You can test at http://pb.anexusit.com/exchange_rate/services
I will really appreciate any suggestion.
Regards,
enzo

Comments
Update
I did some changes in me resources definition, to set the source to data instead of path. but I'm getting the same error.
function exchange_rate_services_services_resources() { return array( 'exchange_rate' => array( 'retrieve' => array( 'help' => 'Retrieves the exchange rate', 'callback' => '_exchange_rate_services_get_exchange_rate', 'args' => array( array( 'name' => 'country', 'type' => 'string', 'description' => 'Country Id', 'source' => 'data', 'optional' => TRUE, ), array( 'name' => 'currency', 'type' => 'string', 'description' => 'Currency Id', 'source' => 'data', 'optional' => TRUE, ), array( 'name' => 'date', 'type' => 'int', 'description' => 'Date in Timestamp', 'source' => 'data', 'optional' => TRUE, ) ), ) ) ); }I run the test using XML-RPC client
more info: http://yfrog.com/5hctnp
enzo - Eduardo Garcia
Change to rest
Hello folks
I update my code to use REST server instead of XML-RPC , but the problem is the same.
Here my new resources definition without parameters
function exchange_rate_services_services_resources() { return array( 'rate' => array( 'retrieve' => array( 'help' => 'Retrieves the exchange rate', 'callback' => '_exchange_rate_services_get_exchange_rate', /* 'args' => array( array( 'name' => 'country', 'type' => 'string', 'description' => 'Country Id', 'source' => array( 'path' => '0' ), 'optional' => TRUE, ), array( 'name' => 'currency', 'type' => 'string', 'description' => 'Currency Id', 'source' => array( 'path' => '1' ), 'optional' => TRUE, ), array( 'name' => 'date', 'type' => 'int', 'description' => 'Date in Timestamp', 'source' => array( 'path' => '2' ), 'optional' => TRUE, ) ), */ ) ) ); }Here new new endpoint definition using REST
/** * Implements hook_default_services_endpoint(). */ function exchange_rate_services_default_services_endpoint() { $endpoints = array( ); $endpoint = new stdClass; $endpoint->disabled = FALSE; /* Edit this to true to make a default endpoint disabled initially */ $endpoint->api_version = 3; $endpoint->name = 'rest_api_exchange_rate'; $endpoint->server = 'rest_server'; $endpoint->path = 'exchange/api/rest'; $endpoint->authentication = array( ); $endpoint->server_settings = array( 'rest_server' => array( 'formatters' => array( 'bencode' => TRUE, 'json' => TRUE, 'php' => TRUE, 'rss' => TRUE, 'xml' => TRUE, 'yaml' => TRUE, 'jsonp' => FALSE, ), 'parsers' => array( 'application/json' => TRUE, 'application/vnd.php.serialized' => TRUE, 'application/x-www-form-urlencoded' => TRUE, 'application/x-yaml' => TRUE, 'multipart/form-data' => TRUE, ), ), ); $endpoint->resources = array( 'rate' => array( 'operations' => array( 'retrieve' => array( 'enabled' => 1, ), ), ), ); $endpoints[ ] = $endpoint; return $endpoints; }You can check my new service at http://drupanium.anexusit.com/exchange/api/rest/rate/retreive
If you access the endpoint return empty, but don't execute my function and if you try to pass parameters doesn't work
Any suggestion ?
Regards,
enzo
enzo - Eduardo Garcia