You are here

public function AcquiaSearchV3ApiClient::searchRequest in Acquia Search 2.x

Create and send a request to search controller.

Parameters

string $path: Path to call.

Return value

array|false Response array or FALSE.

2 calls to AcquiaSearchV3ApiClient::searchRequest()
AcquiaSearchV3ApiClient::getKeys in src/AcquiaSearchV3ApiClient.php
Fetch the search v3 index keys for given core_id and network_id.
AcquiaSearchV3ApiClient::getSearchV3Indexes in src/AcquiaSearchV3ApiClient.php
Helper function to fetch all search v3 indexes for given network_id.

File

src/AcquiaSearchV3ApiClient.php, line 165

Class

AcquiaSearchV3ApiClient
Search v3 API Client. (Deprecated, use Acquia Search Solr Module instead)

Namespace

Drupal\acquia_search

Code

public function searchRequest($path) {
  $data = [
    'host' => $this->searchV3Host,
    'headers' => [
      'x-api-key' => $this->searchV3ApiKey,
    ],
  ];
  $uri = $data['host'] . $path;
  $options = [
    'headers' => $data['headers'],
    'body' => Json::encode($data),
  ];
  try {
    $response = $this->httpClient
      ->get($uri, $options);
    if (!$response) {
      throw new \Exception('Empty Response');
    }
    $stream_size = $response
      ->getBody()
      ->getSize();
    $data = Json::decode($response
      ->getBody()
      ->read($stream_size));
    $status_code = $response
      ->getStatusCode();
    if ($status_code < 200 || $status_code > 299) {
      \Drupal::logger('acquia search')
        ->error("Couldn't connect to search v3 API: @message", [
        '@message' => $response
          ->getReasonPhrase(),
      ]);
      return FALSE;
    }
    return $data;
  } catch (RequestException $e) {
    if ($e
      ->getCode() == 401) {
      \Drupal::logger('acquia search')
        ->error("Couldn't connect to search v3 API:\n          Received a 401 response from the API indicating that credentials are incorrect.\n          Please validate your credentials. @message", [
        '@message' => $e
          ->getMessage(),
      ]);
    }
    elseif ($e
      ->getCode() == 404) {
      \Drupal::logger('acquia search')
        ->error("Couldn't connect to search v3 API:\n          Received a 404 response from the API indicating that the api host is incorrect.\n          Please validate your host. @message", [
        '@message' => $e
          ->getMessage(),
      ]);
    }
    else {
      \Drupal::logger('acquia search')
        ->error("Couldn't connect to search v3 API: Please\n        validate your api host and credentials. @message", [
        '@message' => $e
          ->getMessage(),
      ]);
    }
  } catch (\Exception $e) {
    \Drupal::logger('acquia search')
      ->error("Couldn't connect to search v3 API: @message", [
      '@message' => $e
        ->getMessage(),
    ]);
  }
  return FALSE;
}