You are here

protected function Client::request in Acquia Connector 3.x

Same name and namespace in other branches
  1. 8.2 src/Client.php \Drupal\acquia_connector\Client::request()
  2. 8 src/Client.php \Drupal\acquia_connector\Client::request()

Create and send a request.

Parameters

string $method: Method to call.

string $path: Path to call.

array $data: Data to send.

Return value

array|false Response array or FALSE.

Throws

ConnectorException

3 calls to Client::request()
Client::getDefinition in src/Client.php
Get SPI definition.
Client::getSubscriptionCredentials in src/Client.php
Get account settings to use for creating request authorizations.
Client::nspiCall in src/Client.php
Prepare and send a REST request to Acquia with an authenticator.

File

src/Client.php, line 315

Class

Client
Acquia connector client.

Namespace

Drupal\acquia_connector

Code

protected function request($method, $path, array $data) {
  $uri = $this->server . $path;
  $options = [
    'headers' => $this->headers,
    'body' => Json::encode($data),
  ];
  try {
    switch ($method) {
      case 'GET':
        $response = $this->client
          ->get($uri);
        $status_code = $response
          ->getStatusCode();
        $stream_size = $response
          ->getBody()
          ->getSize();
        $data = Json::decode($response
          ->getBody()
          ->read($stream_size));
        if ($status_code < 200 || $status_code > 299) {
          throw new ConnectorException($data['message'], $data['code'], $data);
        }
        return $data;
      case 'POST':
        $response = $this->client
          ->post($uri, $options);
        $status_code = $response
          ->getStatusCode();
        $stream_size = $response
          ->getBody()
          ->getSize();
        $data = Json::decode($response
          ->getBody()
          ->read($stream_size));
        if ($status_code < 200 || $status_code > 299) {
          throw new ConnectorException($data['message'], $data['code'], $data);
        }
        return $data;
    }
  } catch (RequestException $e) {
    throw new ConnectorException($e
      ->getMessage(), $e
      ->getCode());
  }
  return FALSE;
}