It seems that this should generate an exception. the amfserver_service_sleep method requires an argument of type int. no default is set.
yet when I call the method it doesn't generate the exception.

I get the output from the function (in flash): result: !!!!! amfservice.sleep should fail since no required argument was sent. **data** amfservice says: argument to 'sleep' should be between 1 to 10 , yours was:

function amfserver_services_resources() {  
   return array(
    'amfservice' => array(
      'retrieve' => array(
        'help' => 'retrieves the version of the amfserver',
        'callback' => '_amfserver_service_retrieve',
        'access arguments' => array(AMFSERVER_PERM_SERVICE_TEST),
      ),
      'actions' => array(
        'ping' => array(
          'help' => 'pings the amfserver and sends back the optional message you send to it',
          'callback' => '_amfserver_service_ping',
          'access arguments' => array(AMFSERVER_PERM_SERVICE_TEST),
          'args' => array(
            array(
              'name' => 'message',
              'optional' => TRUE,
              'source' => array('path' => 0),
              'type' => 'string',
              'description' => 'a message you want to give to the server',
              'default value' => 'nothing'
            ),
          ),
        ),
         'sleep' => array(
          'help' => 'makes the php script sleep for the number of seconds provided',
          'callback' => '_amfserver_service_sleep',
          'access arguments' => array(AMFSERVER_PERM_SERVICE_TEST),
          'args' => array(
            array(
              'name' => 'seconds',
              'optional' => FALSE,
              'source' => array('path' => 0),
              'type' => 'int',
              'description' => 'an int between 1 and 10',
            ),
          ),
        ),
      ),
    ),
  );
}


/**
 * service method for the amfserver
 * it's a testing method for the amfserver, provided as an implementation example
 */
function _amfserver_service_retrieve() {
  return "version: " . variable_get( "amfserver_version" );
  
}

/**
 * service method for the amfserver
 * it's a testing method for the amfserver, provided as an implementation example
 */
function _amfserver_service_sleep( $seconds ) {
  if($seconds >= 1 && $seconds <= 10){
    sleep($seconds);
  }
  return "amfservice says: argument to 'sleep' should be between 1 to 10 , yours was: " . $seconds;
}

Comments

rolf vreijdenberger’s picture

It seems like it has to be implemented in the function services_controller_execute() in services.runtime.inc

working on it if I have the time.

rolf vreijdenberger’s picture

Status: Active » Patch (to be ported)
StatusFileSize
new2.05 KB

I implemented the folowing and provided a patch for it.

- check for any recquired arguments
- check if there are not too many arguments provided
- set the default values if no default values are provided and the default values are defined.

in my testing, only for the development of the amfserver, it works as expected.

I'm not sure how all the services are implemented, but in case the tests are not written properly they might fail..
an example: a test sends no arguments where one is required, but it gets a default result, so the test passed.
In the new situation, the test will fail because the recquired argument is missing. etc.

the patch is made agains the 7.x dev version of april 16th and is attached.

rolf vreijdenberger’s picture

Oh, and I'm not sure about the status this issue needs.
Is there a help page somewhere where I can find info on the statuses??

kylebrowning’s picture

Status: Patch (to be ported) » Needs review

Not sure, but the correct status is needs review, once this patch is committed it goes to be ported, for 6.x

kylebrowning’s picture

Status: Needs review » Needs work

Were already setting the default values for arguments here,
private function getControllerArguments($controller, $path, $method, &$sources=array()) { line 182.

It looks like the question is, Should services handle throwing errors if defaults are not supplied, and should services added the default arguments.

Right now, the REST server is throwing errors, so in essence, Servers are required to catch this.

Also, marking this as needs work, Too much arguments should be Too many arguments.

kylebrowning’s picture

Status: Needs work » Postponed

Gonna fix this in 3.1

kylebrowning’s picture

Category: bug » feature
kylebrowning’s picture

Project: Services » amfserver
Version: 7.x-3.x-dev » 7.x-3.0
Category: feature » bug
Status: Postponed » Needs work

Its hard for us to determine when to help developers out and when to not.
Servers ARE required to provide default arguments(and throw exceptions on them).
The reason behind not doing this for you is because different servers handle arguments differently. For Example, we have no way of testing if the node object for node_create is there, because in REST we accept an object of ?name=test as opposed to ?node[name] = test.
REST server excepts both, but you can't check for the former, especially because its resource specific.

That being said, its really easy to check for required arguments and AMServer should just copy and paste this. I havn't fully tested it, but it should open your eyes to how to do it.

  $endpoint = services_get_server_info('endpoint', '');
  $controller = services_controller_get($method_name, $endpoint);
  
  // Add in default arguments if arguments still dont exist.
  if(is_array($controller['args'])) {
    foreach($controller['args'] as $index => $arg) {
      if($arg['optional'] && isset($arg['default value']) && !isset($args[$index])) {
        $args[$index] = $arg['default value'];
      } else {
        if($arg['optional'] == FALSE) {
          return services_error(t('Missing required argument @arg', array(
              '@arg' => $arg['name'],
            )), 401);
        }
      }
    }
  }
rolf vreijdenberger’s picture

Hi Kyle,

I have an implementation of this in the amfserver pretty much from the beginning, so in that case, all is well :)

thanks

rolf vreijdenberger’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.