Ejemplo n.º 1
0
 /**
  * @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.");
 }
 /**
  * @expectedException \GuzzleHttp\Command\Exception\CommandException
  * @expectedExceptionMessage Validation errors: [bar] must be of type string
  */
 public function testValidatesAdditionalParameters()
 {
     $description = new Description(['operations' => ['foo' => ['uri' => 'http://httpbin.org', 'httpMethod' => 'GET', 'responseModel' => 'j', 'additionalParameters' => ['type' => 'string']]]]);
     $client = new GuzzleClient(new Client(), $description);
     $val = new ValidateInput($description);
     $event = new InitEvent(new CommandTransaction($client, $client->getCommand('foo', ['bar' => new \stdClass()])));
     $val->onInit($event);
 }
 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();
 }
Ejemplo n.º 5
0
 /**
  * @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;
 }
Ejemplo n.º 6
0
 public function testExecutesCommandsInParallel()
 {
     $client = $this->getMockBuilder('GuzzleHttp\\Client')->setMethods(['sendAll'])->getMock();
     $description = new Description(['operations' => ['Foo' => []]]);
     $guzzle = new GuzzleClient($client, $description);
     $command = $guzzle->getCommand('foo');
     $request = $client->createRequest('GET', 'http://httbin.org');
     $command->getEmitter()->on('prepare', function (PrepareEvent $e) use($request) {
         $e->setRequest($request);
     }, 1);
     $client->expects($this->once())->method('sendAll')->will($this->returnCallback(function ($requests, $options) use($request) {
         $this->assertEquals(10, $options['parallel']);
         $this->assertTrue($requests->valid());
         $this->assertSame($request, $requests->current());
     }));
     $guzzle->executeAll([$command], ['parallel' => 10]);
 }
Ejemplo n.º 7
0
 /**
  * @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']);
 }