You are here

function _restclient_request in RESTClient 7.2

Basic request with no body data.

Compatible with GET, DELETE, OPTION, and TRACE requests.

3 calls to _restclient_request()
restclient_delete in ./restclient.module
Make a DELETE request
restclient_get in ./restclient.module
Make a GET request
_restclient_request_with_body in ./restclient.module
Requests with body data.

File

./restclient.module, line 299
Defines a standard REST interface to RESTful services

Code

function _restclient_request(&$resource_path, &$variables = array(), $type = 'GET') {

  // List of cacheable requests
  $cacheable = array(
    'GET',
    'OPTION',
  );

  // Map variables
  _restclient_map_variables($variables);

  // Cache reset
  $reset = $variables['reset'];

  // Set the method
  $variables['method'] = $type;

  // Prepare the URL parameters
  if (!empty($variables['parameters'])) {
    _restclient_prepare_url_parameters($resource_path, $variables);
  }

  // Build the URL
  $url = _restclient_build_resource_path($resource_path, $variables);

  // We only cache certain requests
  if (in_array($type, $cacheable)) {

    // Generate the cache id
    $cid = _restclient_generate_cid($type, $url, $variables['headers']);

    // Check the cache
    if (!$reset) {
      $data = restclient_cache_get($variables, $cid);
      if (!empty($data) and REQUEST_TIME < $data->expire) {

        // Debug output
        if (variable_get("restclient_debug", FALSE)) {

          // Temporarily add the response to the variables for display
          $variables['from_cache'] = TRUE;
          $variables['response_from_cache'] = $data->data;
          $variables['resource_path'] = $resource_path;
          _restclient_debug($variables);

          // Cleanup
          unset($variables['from_cache']);
          unset($variables['response_from_cache']);
          unset($variables['resource_path']);
        }
        return $data->data;
      }
    }
  }

  // Authentication - done after checking the cache
  if (!_restclient_prepare_authentication($variables)) {

    // Already logged.
    return FALSE;
  }

  // Make the HTTP request
  switch (RESTCLIENT_ACTIVE_LIBRARY) {
    case RESTCLIENT_FILES_SYSTEM:
      module_load_include('inc', 'restclient', 'restclient.filemode');
      $response = restclient_filemode_fetch_response($url, $variables);
      break;
    case RESTCLIENT_LIBRARY_CURL:
      $response = chr_curl_http_request($url, $variables);
      break;
    case RESTCLIENT_LIBRARY_DRUPAL:
    default:
      $response = drupal_http_request($url, $variables);
  }

  // Debug output
  if (variable_get("restclient_debug", FALSE)) {

    // Temporarily add the response to the variables for display
    $variables['from_cache'] = FALSE;
    $variables['response'] = $response;
    $variables['resource_path'] = $resource_path;
    _restclient_debug($variables);

    // Cleanup
    unset($variables['from_cache']);
    unset($variables['response']);
    unset($variables['resource_path']);
  }
  if (variable_get("restclient_testing", FALSE)) {
    if (RESTCLIENT_ACTIVE_LIBRARY != RESTCLIENT_FILES_SYSTEM) {
      module_load_include('inc', 'restclient', 'restclient.filemode');
      restclient_filemode_save_response($response, $url, $variables);
    }
  }

  // Check the response
  if (!isset($response->error)) {
    if (in_array($type, $cacheable)) {

      // Add default overrides if not set
      if (!isset($variables['cache_default_time'])) {
        $variables['cache_default_time'] = 0;
      }
      if (!isset($variables['cache_default_override'])) {
        $variables['cache_default_override'] = FALSE;
      }
      restclient_cache_set($variables, $cid, $response);
    }

    // Log if the response code is Redirection
    if (RESTCLIENT_RESPONSE_REDIRECTION == restclient_response_code($response)) {
      _restclient_watchdog($type, $response->code, 'Redirection', $url, NULL, WATCHDOG_INFO);
    }

    // No error occured, return the response
    return $response;
  }

  // Log the error
  _restclient_watchdog($type, $response->code, $response->error, $url);

  // Handle authentication (oauth, hybridauth) related errors. The request
  // may have failed due to an expired token, in which case we can re-authenticate
  // and retry this request.
  $retry = _restclient_authentication_request_error($response, $variables);
  if ($retry and empty($variables['auth_retry'])) {
    $variables['auth_retry'] = TRUE;
    return _restclient_request($resource_path, $variables, $type);
  }

  // Try returning a stale cache entry.
  $stale = restclient_cache_get($variables, $cid, TRUE);
  if ($stale) {
    return $stale->data;
  }

  // If error handling is set, return the response anyway, otherwise return FALSE
  if (isset($variables['error_handling']) and $variables['error_handling']) {
    return $response;
  }
  return FALSE;
}