You are here

protected function SolrConnectorPluginBase::restRequest in Search API Solr 8

Same name and namespace in other branches
  1. 8.3 src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase::restRequest()
  2. 8.2 src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase::restRequest()
  3. 4.x src/SolrConnector/SolrConnectorPluginBase.php \Drupal\search_api_solr\SolrConnector\SolrConnectorPluginBase::restRequest()

Sends a REST request to the Solr server endpoint and returns the result.

Parameters

string $endpoint_key: The endpoint that refelcts the base URI.

string $path: The path to append to the base URI.

string $method: The HTTP request method.

string $command_json: The command to send encoded as JSON.

Return value

string The decoded response.

4 calls to SolrConnectorPluginBase::restRequest()
SolrConnectorPluginBase::coreRestGet in src/SolrConnector/SolrConnectorPluginBase.php
Sends a REST GET request to the Solr core and returns the result.
SolrConnectorPluginBase::coreRestPost in src/SolrConnector/SolrConnectorPluginBase.php
Sends a REST POST request to the Solr core and returns the result.
SolrConnectorPluginBase::serverRestGet in src/SolrConnector/SolrConnectorPluginBase.php
Sends a REST GET request to the Solr server and returns the result.
SolrConnectorPluginBase::serverRestPost in src/SolrConnector/SolrConnectorPluginBase.php
Sends a REST POST request to the Solr server and returns the result.

File

src/SolrConnector/SolrConnectorPluginBase.php, line 644

Class

SolrConnectorPluginBase
Defines a base class for Solr connector plugins.

Namespace

Drupal\search_api_solr\SolrConnector

Code

protected function restRequest($endpoint_key, $path, $method = Request::METHOD_GET, $command_json = '') {
  $this
    ->connect();
  $request = new Request();
  $request
    ->setMethod($method);
  $request
    ->addHeader('Accept: application/json');
  if (Request::METHOD_POST == $method) {
    $request
      ->addHeader('Content-type: application/json');
    $request
      ->setRawData($command_json);
  }
  $request
    ->setHandler($path);
  $endpoint = $this->solr
    ->getEndpoint($endpoint_key);
  $response = $this
    ->executeRequest($request, $endpoint);
  $output = Json::decode($response
    ->getBody());

  // \Drupal::logger('search_api_solr')->info(print_r($output, true));.
  if (!empty($output['errors'])) {
    throw new SearchApiSolrException('Error trying to send a REST request.' . "\nError message(s):" . print_r($output['errors'], TRUE));
  }
  return $output;
}