You are here

class ServicesClientConnectionRestServer in Services Client 7.2

Same name and namespace in other branches
  1. 7 services_client_connection/plugins/ServicesClientConnectionRestServer.inc \ServicesClientConnectionRestServer

@file

Hierarchy

Expanded class hierarchy of ServicesClientConnectionRestServer

3 string references to 'ServicesClientConnectionRestServer'
ServicesClientBaseWebTestCase::createSCConnection in tests/services_client.test
Creates services client connection object.
ServicesClientConnection::httpRequest in services_client_connection/include/connection.inc
Make low level HTTP request which allows to make calls to non-services REST only endpoints.
_services_client_connection_server in services_client_connection/include/plugin_definition.inc
List of server plugins provided by module

File

services_client_connection/plugins/ServicesClientConnectionRestServer.inc, line 8

View source
class ServicesClientConnectionRestServer extends ServicesClientConnectionServer {

  /**
   * Response parser
   *
   * @var ServicesClientConnectionRestServerParser
   */
  protected $parser;

  /**
   * Request formatter
   *
   * @var ServicesClientConnectionRestServerFormatter
   */
  protected $formatter;

  /**
   * Specify REST actions
   */
  protected $rest_actions = array(
    'retrieve' => 'GET',
    'update' => 'PUT',
    'create' => 'POST',
    'delete' => 'DELETE',
  );

  /**
   * Constructor
   */
  public function __construct($connection, $config, $client = NULL) {
    parent::__construct($connection, $config, $client);

    // Init parser
    $parser = isset($this->config['response_parser']) ? $this->config['response_parser'] : NULL;
    $this->parser = new ServicesClientConnectionRestServerParser($parser);

    // Init formatter
    $formatter = isset($this->config['request_formatter']) ? $this->config['request_formatter'] : NULL;
    $this->formatter = new ServicesClientConnectionRestServerFormatter($formatter);
  }

  /**
   * Implements configForm().
   */
  public function configForm(&$form, &$form_state) {
    $form['request_formatter'] = array(
      '#type' => 'select',
      '#title' => t('Request formatter'),
      '#options' => ServicesClientConnectionRestServerFormatter::getFormatters(),
      '#default_value' => isset($this->config['request_formatter']) ? $this->config['request_formatter'] : '',
    );
    $form['response_parser'] = array(
      '#type' => 'select',
      '#title' => t('Response parser'),
      '#options' => ServicesClientConnectionRestServerParser::getParsers(),
      '#default_value' => isset($this->config['response_parser']) ? $this->config['response_parser'] : '',
    );
  }

  /**
   * Implements configFormSubmit().
   */
  public function configFormSubmit(&$form, &$form_state) {
    $form_state['config'] = array(
      'request_formatter' => $form_state['values']['request_formatter'],
      'response_parser' => $form_state['values']['response_parser'],
    );
  }

  /**
   * Implements prepareRequest().
   */
  public function prepareRequest(ServicesClientConnectionHttpRequest &$request) {
    parent::prepareRequest($request);

    // Normalize endpoint URL
    $endpoint = rtrim($this->connection->endpoint, '/');

    // No special changes to URL
    $url_parts = array(
      $endpoint,
      $request->resource,
    );

    // Add resource ID if present
    if ($request->id) {
      $url_parts[] = urlencode($request->id);
    }

    // For special REST actions - CRUD don't add action to URL and
    // add HTTP method.
    if (in_array($request->action, array_keys($this->rest_actions))) {
      $request->http_method = $this->rest_actions[$request->action];
    }
    elseif (!empty($request->relation)) {
      $url_parts[] = $request->relation;
      $request->http_method = 'GET';
    }
    elseif (!empty($request->action)) {
      $url_parts[] = $request->action;
      $request->http_method = 'POST';
    }
    elseif (empty($request->http_method)) {
      $request->http_method = 'POST';
    }

    // Build URL if not set in Request object
    if (empty($request->url)) {
      $request->url = implode('/', $url_parts);
    }
    else {
      $request->url = $endpoint . '/' . ltrim($request->url, '/');
    }

    // Allow parser to prepare request
    $this->parser
      ->prepareRequest($request);

    // Format request
    $this->formatter
      ->format($request);
  }

  /**
   * Implements processResponse().
   */
  public function processResponse(ServicesClientConnectionResponse &$response) {
    parent::processResponse($response);

    // Try to get error code from response
    if (ServicesClientConnectionHttp::isError($response->response_code)) {
      $response->error_code = $response->response_code;
      $response->error_message = ServicesClientConnectionHttp::getHttpMessage($response->response_code);
    }
    else {

      // Process response and parse data
      $response->data = $this->parser
        ->parse($response->raw_response);
    }
  }

}

Members