You are here

protected function Client::send in Little helpers 7.2

Same name and namespace in other branches
  1. 7 src/Rest/Client.php \Drupal\little_helpers\Rest\Client::send()

This method does the actual hard-work in this class.

4 calls to Client::send()
Client::delete in src/Rest/Client.php
Send a DELETE request to the API.
Client::get in src/Rest/Client.php
Send a GET request to the API.
Client::post in src/Rest/Client.php
Send a POST request to the API.
Client::put in src/Rest/Client.php
Send a PUT request to the API.

File

src/Rest/Client.php, line 105

Class

Client
This a simple JSON REST Client based on drupal_http_request().

Namespace

Drupal\little_helpers\Rest

Code

protected function send($path, array $query = [], $data = NULL, array $options = []) {
  if (!$path || $path[0] != '/') {
    $path = '/' . $path;
  }
  if ($query) {
    $path .= '?' . http_build_query($query);
  }

  // Encode data if needed.
  if ($data) {
    $options['headers']['Content-Type'] = 'application/json';
    $options['headers']['Accept'] = 'application/json';
    if (!is_string($data)) {
      $data = drupal_json_encode($data);
    }
    $options['data'] = $data;
  }
  $url = $this->endpoint . $path;
  $options += $this->options;
  $result = $this
    ->sendRequest($url, $options);

  // Turn errors into exceptions.
  if (!empty($result->error)) {
    throw new HttpError($result);
  }
  return drupal_json_decode($result->data);
}