public function __construct()
 {
     $this->client = new \Guzzle\Service\Client();
     $this->client->getEventDispatcher()->addListener('request.error', function (\Guzzle\Common\Event $event) {
         $event->stopPropagation();
     });
 }
Ejemplo n.º 2
0
 /**
  * Constructor
  *
  * @param string $accessToken
  */
 public function __construct($accessToken)
 {
     $this->accessToken = $accessToken;
     $this->client = new Client();
     $this->client->setDescription(ServiceDescription::factory(__DIR__ . '/Resources/config/service.json'));
     $this->client->getEventDispatcher()->addListener('client.create_request', [$this, 'onClientCreateRequest']);
     $this->client->getEventDispatcher()->addListener('request.error', [$this, 'onRequestError']);
 }
Ejemplo n.º 3
0
 /**
  * @covers Geocoder\HttpAdapter\GuzzleHttpAdapter::__construct
  * @covers Geocoder\HttpAdapter\GuzzleHttpAdapter::getContent
  */
 public function testRetrievesResponse()
 {
     $historyPlugin = new HistoryPlugin();
     $mockPlugin = new MockPlugin(array(new Response(200, null, 'body')));
     $client = new Client();
     $client->getEventDispatcher()->addSubscriber($mockPlugin);
     $client->getEventDispatcher()->addSubscriber($historyPlugin);
     $adapter = new GuzzleHttpAdapter($client);
     $this->assertEquals('body', $adapter->getContent('http://test.com/'));
     $this->assertEquals('http://test.com/', $historyPlugin->getLastRequest()->getUrl());
 }
 /**
  * @param array $options API options
  */
 public function __construct($authToken, $options = array())
 {
     $apiBase = isset($options['api_base']) ? $options['api_base'] : 'https://api.webpay.jp/v1';
     $this->client = new GuzzleClient($apiBase);
     $this->client->setDefaultOption('headers/Authorization', 'Bearer ' . $authToken);
     $this->client->setDefaultOption('headers/Content-Type', "application/json");
     $this->client->setDefaultOption('headers/Accept', "application/json");
     $this->client->setDefaultOption('headers/User-Agent', "Apipa-webpay/2.2.2 php");
     $this->client->setDefaultOption('headers/Accept-Language', "en");
     $this->client->getEventDispatcher()->addListener('request.error', array($this, 'onRequestError'));
     $this->client->getEventDispatcher()->addListener('request.exception', array($this, 'onRequestException'));
     $this->charge = new Charge($this);
     $this->customer = new Customer($this);
     $this->token = new Token($this);
     $this->event = new Event($this);
     $this->shop = new Shop($this);
     $this->recursion = new Recursion($this);
     $this->account = new Account($this);
 }
 public function testExecutesCommandsWithArray()
 {
     $client = new Client('http://www.test.com/');
     $client->getEventDispatcher()->addSubscriber(new MockPlugin(array(new Response(200), new Response(200))));
     // Create a command set and a command
     $set = array(new MockCommand(), new MockCommand());
     $client->execute($set);
     // Make sure it sent
     $this->assertTrue($set[0]->isExecuted());
     $this->assertTrue($set[1]->isExecuted());
 }
Ejemplo n.º 6
0
 public function getInfo($key, $default = null)
 {
     syslog(LOG_INFO, "getting info for key {$key} on request " . $_SERVER['REQUEST_URI']);
     if (empty($this->info)) {
         $this->info = array();
         syslog(LOG_INFO, 'guzzle start for url ' . $this->url);
         $guzzle = new GuzzleClient($this->url);
         $guzzle->getEventDispatcher()->addListener('request.error', function (Event $event) {
             $event->stopPropagation();
         });
         $response = $guzzle->head()->send();
         if (!$response->isSuccessful()) {
             $response = $guzzle->get()->send();
         }
         if ($response->isSuccessful()) {
             $this->info = $response->getInfo();
         }
         syslog(LOG_INFO, 'guzzle stop for url ' . $this->url);
     }
     return isset($this->info[$key]) ? $this->info[$key] : $default;
 }
Ejemplo n.º 7
0
 /**
  * Set a mock response from a mock file on the next client request.
  *
  * This method assumes that mock response files are located under the
  * Command/Mock/ directory of the Service being tested
  * (e.g. Unfuddle/Command/Mock/).  A mock response is added to the next
  * request sent by the client.
  *
  * @param Client $client Client object to modify
  * @param string $paths  Path to files within the Mock folder of the service
  *
  * @return MockPlugin returns the created mock plugin
  */
 public function setMockResponse(Client $client, $paths)
 {
     $this->requests = array();
     $that = $this;
     $mock = new MockPlugin(null, true);
     $client->getEventDispatcher()->removeSubscriber($mock);
     $mock->getEventDispatcher()->addListener('mock.request', function (Event $event) use($that) {
         $that->addMockedRequest($event['request']);
     });
     foreach ((array) $paths as $path) {
         $mock->addResponse($this->getMockResponse($path));
     }
     $client->getEventDispatcher()->addSubscriber($mock);
     return $mock;
 }
 public function testCanAddListenerToParseDomainObjects()
 {
     $client = new Client();
     $client->setDescription(ServiceDescription::factory(array('operations' => array('test' => array('responseClass' => 'FooBazBar')))));
     $foo = new \stdClass();
     $client->getEventDispatcher()->addListener('command.parse_response', function ($e) use($foo) {
         $e['result'] = $foo;
     });
     $command = $client->getCommand('test');
     $command->prepare()->setResponse(new Response(200), true);
     $result = $command->execute();
     $this->assertSame($result, $foo);
 }
Ejemplo n.º 9
0
 /**
  * @covers Guzzle\Service\Client::execute
  */
 public function testExecutesCommandSets()
 {
     $client = new Client('http://www.test.com/');
     $client->getEventDispatcher()->addSubscriber(new MockPlugin(array(new Response(200))));
     // Create a command set and a command
     $set = new CommandSet();
     $cmd = new MockCommand();
     $set->addCommand($cmd);
     $this->assertSame($set, $client->execute($set));
     // Make sure it sent
     $this->assertTrue($cmd->isExecuted());
     $this->assertTrue($cmd->isPrepared());
     $this->assertEquals(200, $cmd->getResponse()->getStatusCode());
 }
Ejemplo n.º 10
0
 /**
  * Set a mock response from a mock file on the next client request.
  *
  * This method assumes that mock response files are located under the
  * Command/Mock/ directory of the Service being tested
  * (e.g. Unfuddle/Command/Mock/).  A mock response is added to the next
  * request sent by the client.
  *
  * @param Client $client Client object to modify
  * @param string $paths  Path to files within the Mock folder of the service
  *
  * @return MockPlugin returns the created mock plugin
  */
 public function setMockResponse(Client $client, $paths)
 {
     $this->requests = array();
     $that = $this;
     $mock = new MockPlugin(null, true);
     $client->getEventDispatcher()->removeSubscriber($mock);
     $mock->getEventDispatcher()->addListener('mock.request', function (Event $event) use($that) {
         $that->addMockedRequest($event['request']);
     });
     if ($paths instanceof Response) {
         // A single response instance has been specified, create an array with that instance
         // as the only element for the following loop to work as expected
         $paths = array($paths);
     }
     foreach ((array) $paths as $path) {
         $mock->addResponse($this->getMockResponse($path));
     }
     $client->getEventDispatcher()->addSubscriber($mock);
     return $mock;
 }