You are here

public function WSClientRESTEndpoint::call in Web service client 7

Calls the REST service.

Parameters

string $operation_name: The name of the operation to execute.

array $arguments: Arguments to pass to the service with this operation.

Overrides WSClientEndpoint::call

File

wsclient_rest/wsclient_rest.inc, line 99
Web service client REST - include file.

Class

WSClientRESTEndpoint
A remote endpoint type for invoking REST services.

Code

public function call($operation_name, $arguments) {
  $operation = $this->service->operations[$operation_name];
  $operation_url = isset($operation['url']) ? $operation['url'] : '';

  // Replace argument patterns in the operation URL.
  foreach ($arguments as $key => $value) {
    if (strpos($operation_url, '@' . $key) !== FALSE) {
      $operation_url = str_replace('@' . $key, $value, $operation_url);
      unset($arguments[$key]);
    }
  }
  $client = $this
    ->client();
  $type = isset($operation['type']) ? $operation['type'] : 'GET';
  $data = NULL;
  if (isset($operation['data'])) {
    $data = $arguments[$operation['data']];
    unset($arguments[$operation['data']]);
  }

  // Services module compliance: post fields should be in the $data array
  // instead of $arguments.
  if ($type == 'POST' || $type == 'PUT') {
    $data = array_merge((array) $data, $arguments);
    if (empty($data)) {
      unset($data);
    }
    unset($arguments);
  }
  try {
    $response = $client
      ->execute(new HttpClientRequest($this->service->url . $operation_url, array(
      'method' => $type,
      'parameters' => $arguments,
      'data' => $data,
    )));
    return $response;
  } catch (HttpClientException $e) {
    throw new WSClientException('Error invoking the REST service %name, operation %operation: %error', array(
      '%name' => $this->service->label,
      '%operation' => $operation_name,
      '%error' => $e
        ->getMessage(),
    ));
  }
}