You are here

public function AcquiaSearchApiClient::searchRequest in Acquia Search 3.x

Create and send a request to search controller.

Parameters

string $path: Path to call.

string $query_string: Query string to call.

Return value

array|false Response array or FALSE.

1 call to AcquiaSearchApiClient::searchRequest()
AcquiaSearchApiClient::getSearchIndexes in src/AcquiaSearchApiClient.php
Helper function to fetch all search v3 indexes for given subscription.

File

src/AcquiaSearchApiClient.php, line 126

Class

AcquiaSearchApiClient
Acquia implementation of the Search API Client.

Namespace

Drupal\acquia_search

Code

public function searchRequest(string $path, string $query_string) {
  $host = $this->authInfo['host'];
  $req_time = \Drupal::time()
    ->getRequestTime();
  $authorization_string = $this
    ->calculateAuthString();
  $req_params = [
    'GET',
    preg_replace('/^https?:\\/\\//', '', $host),
    $path,
    $query_string,
    $authorization_string,
    $req_time,
  ];
  $authorization_header = $this
    ->calculateAuthHeader($req_params, $authorization_string);
  $data = [
    'host' => $host,
    'headers' => [
      'Authorization' => $authorization_header,
      'X-Authorization-Timestamp' => $req_time,
    ],
  ];
  $uri = $data['host'] . $path . '?' . $query_string;
  $options = [
    'headers' => $data['headers'],
    'timeout' => 5,
  ];
  try {
    $response = $this->client
      ->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. @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. @message", [
        '@message' => $e
          ->getMessage(),
      ]);
    }
    else {
      \Drupal::logger('acquia_search')
        ->error("Couldn't connect to search v3 API:\n          @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;
}