/** * @param string $name * @param array $args * * @return FutureInterface|mixed|null */ public function request($name, array $args = []) { $args = array_merge($this->getDefaultArgs(), $args); $command = $this->guzzleClient->getCommand($name, $args); $response = $this->guzzleClient->execute($command); return $response; }
/** * @param string $commandName * @param array $params * * @return array */ public function executeCommand($commandName, array $params = array()) { try { $command = empty($params) ? $this->client->getCommand($commandName) : $this->client->getCommand($commandName, $params); $command->getEmitter()->on('process', function (ProcessEvent $event) { $event->setResult($event->getResponse()); }); $result = $this->client->execute($command); if ($result->getStatusCode() != 200) { throw new \Exception($result->getReasonPhrase()); } } catch (\Exception $e) { return array("status" => "error", "message" => $e->getMessage()); } return $result ? array("status" => "success", "message" => $result) : array("status" => "error", "message" => "Empty response after API call."); }
function it_should_return_recommendations(GuzzleClient $client, ResponseParser $parser, RecommendationFactory $factory, Command $command) { $parameters = ['id' => '1', 'limit' => 30]; $client->getCommand('recommendations', $parameters)->willReturn($command); $results = [['id' => '1', 'uuid' => 'abc', ['score' => ['totalScore' => 0, 'scoreParts' => []]]]]; $client->execute($command)->willReturn($results); $parser->parse($results)->willReturn($results); $factory->getRecommendation(['id' => '1', 'uuid' => 'abc', ['score' => ['totalScore' => 0, 'scoreParts' => []]]])->willReturn(new Recommendation('1', 'abc', new Score())); $recommendationResult = $this->getRecommendations($parameters); $recommendationResult->shouldBeArray(); }
/** * @param array $parameters * @return \GraphAwareReco\Domain\Model\Recommendation[] * @throws ServerErrorException * @throws UnFoundItemException */ public function getRecommendations(array $parameters) { $command = $this->client->getCommand('recommendations', $parameters); try { $result = $this->client->execute($command); } catch (CommandClientException $ex) { if ($ex->getResponse()->getStatusCode() == 404) { throw new UnFoundItemException($parameters, $ex->getResponse()->getBody()); } elseif ($ex->getResponse()->getStatusCode() == 500) { throw new ServerErrorException($parameters, $ex->getResponse()->getBody()); } else { throw new ServerErrorException($parameters, $ex->getResponse()->getBody()); } } $recommendationResults = $this->parser->parse($result); $recommendations = []; foreach ($recommendationResults as $recommendation) { $recommendations[] = $this->factory->getRecommendation($recommendation); } return $recommendations; }
/** * @param CommandInterface $command * * @return \GuzzleHttp\Ring\Future\FutureInterface|mixed|null * @throws \Exception */ public function execute(CommandInterface $command) { try { $result = parent::execute($command); return $result['result']; } catch (RequestException $e) { if ($e->hasResponse() !== null) { throw new \Exception($e->getResponse()->getBody()->__toString()); } throw $e; } }
public function testReturnsProcessedResponse() { $client = new Client(); $client->getEmitter()->on('before', function (BeforeEvent $event) { $event->intercept(new Response(201)); }); $description = new Description(['operations' => ['Foo' => ['responseModel' => 'Bar']], 'models' => ['Bar' => ['type' => 'object', 'properties' => ['code' => ['location' => 'statusCode']]]]]); $guzzle = new GuzzleClient($client, $description); $command = $guzzle->getCommand('foo'); $command->getEmitter()->on('prepare', function (PrepareEvent $event) { $event->setRequest($event->getClient()->getHttpClient()->createRequest('GET', 'http://httbin.org')); }, 1); $result = $guzzle->execute($command); $this->assertInstanceOf('GuzzleHttp\\Command\\Model', $result); $this->assertEquals(201, $result['code']); }
/** * @param CommandInterface $command * @return GeoCoderResponse */ public function execute(CommandInterface $command) { $result = parent::execute($command); return new GeoCoderResponse($result ?: []); }
/** * @dataProvider xmlProvider */ public function testSerializesXml(array $operation, array $input, $xml) { $operation['uri'] = 'http://httpbin.org'; $client = new GuzzleClient(new Client(), new Description(['operations' => ['foo' => $operation]])); $request = null; $command = $client->getCommand('foo', $input); $command->getEmitter()->on('prepare', function (PrepareEvent $event) use(&$request) { $request = $event->getRequest(); $event->getRequest()->getEmitter()->on('before', function (BeforeEvent $e) { $e->intercept(new Response(200)); }); }); $client->execute($command); if (!empty($input)) { $this->assertEquals('application/xml', $request->getHeader('Content-Type')); } else { $this->assertEquals('', (string) $request->getHeader('Content-Type')); } $body = str_replace(array("\n", "<?xml version=\"1.0\"?>"), '', (string) $request->getBody()); $this->assertEquals($xml, $body); }
public function testReturnsProcessedResponse() { $client = new Client(); $client->getEmitter()->on('before', function (BeforeEvent $event) { $event->intercept(new Response(201)); }); $description = new Description(['operations' => ['Foo' => ['responseModel' => 'Bar']], 'models' => ['Bar' => ['type' => 'object', 'properties' => ['code' => ['location' => 'statusCode']]]]]); $guzzle = new GuzzleClient($client, $description); $command = $guzzle->getCommand('foo'); $result = $guzzle->execute($command); $this->assertInternalType('array', $result); $this->assertEquals(201, $result['code']); }