Exemplo n.º 1
0
 /**
  * @param array|string $body
  *
  * @throws \Elasticsearch\Common\Exceptions\InvalidArgumentException
  * @return $this
  */
 public function setBody($body)
 {
     if (isset($body) !== true) {
         return $this;
     }
     if (is_array($body) === true) {
         $bulkBody = "";
         foreach ($body as $item) {
             $bulkBody .= $this->serializer->serialize($item) . "\n";
         }
         $body = $bulkBody;
     }
     $this->body = $body;
     return $this;
 }
 /**
  * @param $method
  * @param $uri
  * @param null $params
  * @param null $body
  * @param array $options
  * @param \Elasticsearch\Transport $transport
  * @return mixed
  */
 public function performRequest($method, $uri, $params = null, $body = null, $options = [], Transport $transport = null)
 {
     if (isset($body)) {
         $body = $this->serializer->serialize($body);
     }
     $request = ['http_method' => $method, 'scheme' => $this->transportSchema, 'uri' => $this->getURI($uri, $params), 'body' => $body, 'headers' => ['host' => [$this->host]]];
     $request = array_merge_recursive($request, $this->connectionParams, $options);
     $handler = $this->handler;
     $future = $handler($request, $this, $transport, $options);
     return $future;
 }
Exemplo n.º 3
0
 /**
  * Perform a request to the Cluster
  *
  * @param string $method     HTTP method to use
  * @param string $uri        HTTP URI to send request to
  * @param null   $params     Optional query parameters
  * @param null   $body       Optional query body
  *
  * @throws Common\Exceptions\NoNodesAvailableException|\Exception
  * @internal param null $maxRetries Optional number of retries
  *
  * @return array
  */
 public function performRequest($method, $uri, $params = null, $body = null)
 {
     try {
         $connection = $this->getConnection();
     } catch (Exceptions\NoNodesAvailableException $exception) {
         $this->log->critical('No alive nodes found in cluster');
         throw $exception;
     }
     $response = array();
     $caughtException = null;
     $this->lastConnection = $connection;
     try {
         if (isset($body) === true) {
             $body = $this->serializer->serialize($body);
         }
         $response = $connection->performRequest($method, $uri, $params, $body);
         $connection->markAlive();
         $this->retryAttempts = 0;
         $data = $this->serializer->deserialize($response['text'], $response['info']);
         return array('status' => $response['status'], 'data' => $data, 'info' => $response['info']);
     } catch (Exceptions\Curl\OperationTimeoutException $exception) {
         $this->connectionPool->scheduleCheck();
         $caughtException = $exception;
     } catch (Exceptions\ClientErrorResponseException $exception) {
         throw $exception;
         //We need 4xx errors to go straight to the user, no retries
     } catch (Exceptions\ServerErrorResponseException $exception) {
         throw $exception;
         //We need 5xx errors to go straight to the user, no retries
     } catch (TransportException $exception) {
         $connection->markDead();
         $this->connectionPool->scheduleCheck();
         $caughtException = $exception;
     }
     $shouldRetry = $this->shouldRetry($method, $uri, $params, $body);
     if ($shouldRetry === true) {
         return $this->performRequest($method, $uri, $params, $body);
     }
     if ($caughtException !== null) {
         throw $caughtException;
     }
     return $response;
 }