You are here

protected function Api::query in Fastly 8.3

Performs http queries to Fastly API server.

Parameters

string $uri: The uri to use for the request, appended to the host.

array $data: (optional) Data to send with the request.

string $method: (optional) The method to use for the request, defaults to GET.

array $headers: (optional) An array of headers to send with the request.

Return value

\Psr\Http\Message\ResponseInterface Response.

Throws

\GuzzleHttp\Exception\RequestException RequestException.

7 calls to Api::query()
Api::getCurrentUser in src/Api.php
Gets Fastly user associated with an API key.
Api::getDetails in src/Api.php
Get Service Details.
Api::getServices in src/Api.php
Gets a list of services for the current customer.
Api::getToken in src/Api.php
Get a single token based on the access_token used in the request..
Api::purgeAll in src/Api.php
Purge whole site/service.

... See full list

File

src/Api.php, line 462

Class

Api
Fastly API for Drupal.

Namespace

Drupal\fastly

Code

protected function query($uri, array $data = [], $method = 'GET', array $headers = []) {
  $data['connect_timeout'] = $this->connectTimeout;
  try {
    if (empty($data['headers'])) {
      $data['headers'] = $headers;
      $data['headers']['Accept'] = 'application/json';
      $data['headers']['Fastly-Key'] = $this->apiKey;

      // If the module is configured to use soft purging, we need to add
      // the appropriate header.
      if ($this->purgeMethod == PurgeOptionsForm::FASTLY_SOFT_PURGE) {
        $data['headers']['Fastly-Soft-Purge'] = 1;
      }
    }
    switch (strtoupper($method)) {
      case 'GET':
        $uri = ltrim($uri, "/");
        return $this->httpClient
          ->request($method, $this->host . $uri, $data);
      case 'POST':
        return $this->httpClient
          ->post($this->host . $uri, $data);
      case 'PURGE':
        return $this->httpClient
          ->request($method, $uri, $data);
      default:
        throw new \Exception('Method :method is not valid for Fastly service.', [
          ':method' => $method,
        ]);
    }
  } catch (\Exception $e) {
    $this->messenger
      ->addError($e
      ->getMessage());
    $this->logger
      ->critical($e
      ->getMessage());
  }
  return new Response();
}