/** * * @throws \Exception * @return array */ public function performRequest() { $result = array(); try { $result = $this->transport->performRequest($this->getMethod(), $this->getURI(), $this->params, $this->getBody()); } catch (\Exception $exception) { $code = $exception->getCode(); if ($this->ignore === null) { throw $exception; } else { if (array_search($code, $this->ignore) === false) { throw $exception; } else { //TODO return null or dedicated object here instead? return array('data' => $exception->getMessage()); } } } return $result; }
/** * Perform Request * * @param AbstractEndpoint $endpoint The Endpoint to perform this request against * * @throws Missing404Exception * @throws RoutingMissingException */ public static function performRequest(AbstractEndpoint $endpoint, Transport $transport) { try { $response = $transport->performRequest($endpoint->getMethod(), $endpoint->getURI(), $endpoint->getParams(), $endpoint->getBody(), $endpoint->getOptions()); $response = $transport->resultOrFuture($response, $endpoint->getOptions()); if (!$response instanceof FutureArrayInterface) { if ($response['status'] === 200) { return true; } else { return false; } } else { // async mode, can't easily resolve this...punt to user return $response; } } catch (Missing404Exception $exception) { return false; } catch (RoutingMissingException $exception) { return false; } }
/** * @throws \Exception * @return array */ public function performRequest() { $promise = $this->transport->performRequest($this->getMethod(), $this->getURI(), $this->params, $this->getBody(), $this->options); return $promise; }
public function testPerformRequestTransportException() { $log = $this->getMockBuilder('\\Monolog\\Logger')->disableOriginalConstructor()->getMock(); $hosts = array(array('host' => 'localhost')); $method = 'GET'; $uri = '/'; $params = null; $body = null; $response = array('text' => 'texty text', 'status' => '200', 'info' => array()); $mockConnection = m::mock('\\Elasticsearch\\Connections\\AbstractConnection')->shouldReceive('performRequest')->once()->with($method, $uri, $params, $body)->andThrow(new Elasticsearch\Common\Exceptions\TransportException())->getMock()->shouldReceive('performRequest')->once()->with($method, $uri, $params, $body)->andReturn($response)->getMock()->shouldReceive('markDead')->once()->getMock()->shouldReceive('markAlive')->once()->getMock(); $mockConnectionFxn = function ($host, $port = null) use($mockConnection) { return $mockConnection; }; $mockConnectionPool = m::mock('\\Elasticsearch\\ConnectionPool\\StaticConnectionPool')->shouldReceive('scheduleCheck')->once()->getMock()->shouldReceive('nextConnection')->twice()->andReturn($mockConnection)->getMock(); $mockConnectionPoolFxn = function ($connections) use($mockConnectionPool) { return $mockConnectionPool; }; $mockSerializer = m::mock('\\Elasticsearch\\Serializers\\SerializerInterface')->shouldReceive('deserialize')->with($response['text'], array())->andReturn('out')->getMock(); // Eww... $pimple = m::mock('\\Pimple\\Container')->shouldReceive('offsetGet')->with('connectionPool')->andReturn($mockConnectionPoolFxn)->getMock()->shouldReceive('offsetGet')->with('serializer')->andReturn($mockSerializer)->getMock()->shouldReceive('offsetGet')->with('connection')->andReturn($mockConnectionFxn)->getMock()->shouldReceive('offsetGet')->with('sniffOnStart')->andReturn(false)->getMock()->shouldReceive('offsetExists')->with('retries')->andReturn(false)->getMock()->shouldReceive('offsetSet')->with('retries', 0)->getMock()->shouldReceive('offsetGet')->with('retries')->andReturn(1)->getMock(); $transport = new Elasticsearch\Transport($hosts, $pimple, $log); $ret = $transport->performRequest($method, $uri, $params, $body); $expected = array('status' => $response['status'], 'data' => 'out', 'info' => $response['info']); $this->assertEquals($expected, $ret); }
/** * @param $endpoint AbstractEndpoint * * @throws \Exception * @return array */ private function performRequest(AbstractEndpoint $endpoint) { $promise = $this->transport->performRequest($endpoint->getMethod(), $endpoint->getURI(), $endpoint->getParams(), $endpoint->getBody(), $endpoint->getOptions()); return $this->transport->resultOrFuture($promise, $endpoint->getOptions()); }