You are here

function ClientsConnectionDrupalRESTTestGeneric::test in Web Service Clients 7.3

Execute the test.

Connection test handlers should return the raw data they got back from the connection for display to the user.

Parameters

$connection: The connection handler.

$button_form_values: The form values for the test form element. The values for elements added to the form are in $button_form_values['params'].

Return value

Data from the remote connection. This is output to the form as raw data.

Overrides ClientsConnectionTestingInterface::test

File

connections/clients_drupal_rest/clients_drupal_rest.testing.inc, line 383
Contains classes for Client connection testing.

Class

ClientsConnectionDrupalRESTTestGeneric
Test a generic REST request.

Code

function test($connection, &$button_form_values) {
  if (!empty($button_form_values['params']['entity_text'])) {
    $entity_type = $button_form_values['params']['entity_text'];
  }
  else {
    $entity_type = $button_form_values['params']['entity'];
  }
  if ($button_form_values['params']['entity_prefix']) {
    $resource = 'entity_' . $entity_type;
  }
  else {
    $resource = $entity_type;
  }
  if (!empty($button_form_values['params']['path'])) {
    $path = $resource . '/' . $button_form_values['params']['path'];
  }
  else {
    $path = $resource;
  }
  $method = $button_form_values['params']['method'];
  if (!empty($button_form_values['params']['data'])) {
    if ($button_form_values['params']['data_type'] == 'json') {
      $data = drupal_json_decode($button_form_values['params']['data']);
    }
    else {
      eval('$data = ' . $button_form_values['params']['data'] . ';');
    }
  }
  else {
    $data = array();
  }
  drupal_set_message(t("Making @method request to @path.", array(
    '@method' => $method,
    '@path' => $path,
  )));
  if (module_exists('devel')) {
    dpm($data, 'Data');
    $json_error = json_last_error();
    if ($json_error != JSON_ERROR_NONE) {
      dpm($json_error, 'JSON error');
    }
  }
  try {
    $result = $connection
      ->makeRequest($path, $method, $data);
  } catch (Exception $e) {
    drupal_set_message(t('Could not access the remote site, got error message "@message".', array(
      '@message' => $e
        ->getMessage(),
    )), 'warning');
    return;
  }
  return $result;
}