Example #1
0
 /**
  * {@inheritDoc}
  */
 public function __construct(ClientInterface $client, ApiInterface $api)
 {
     $this->client = $client;
     $this->api = $api;
     $this->errorBuilder = new Builder();
     $this->client->getEmitter()->attach($this->errorBuilder);
 }
Example #2
0
 protected function setUp()
 {
     parent::setUp();
     $this->client = new Guzzle\Client();
     $this->mock = new Guzzle\Subscriber\Mock();
     $this->client->getEmitter()->attach($this->mock);
     $this->service = new ServiceMock();
     $this->service->setClient($this->client);
 }
 /**
  * @param string             $accessToken
  * @param ClientInterface    $client      (optional)
  * @param ExceptionInterface $exception   (optional)
  */
 public function __construct($accessToken, ClientInterface $client = null, ExceptionInterface $exception = null)
 {
     $that = $this;
     $this->client = $client ?: new \GuzzleHttp\Client();
     $this->exception = $exception;
     $this->client->setDefaultOption('headers/Authorization', sprintf('Bearer %s', $accessToken));
     $this->client->getEmitter()->on('complete', function (CompleteEvent $e) use($that) {
         $that->handleResponse($e);
         $e->stopPropagation();
     });
 }
Example #4
0
 /**
  * @param $apiKey
  * @param array $httpOptions
  * @param HttpClientInterface $httpClient
  */
 public function __construct($apiKey, array $httpOptions = [], HttpClientInterface $httpClient = null)
 {
     $this->httpOptions = array_merge($this->httpOptions, $httpOptions);
     if (is_null($httpClient)) {
         $httpClient = new \GuzzleHttp\Client($this->httpOptions);
     }
     $this->apiKey = $apiKey;
     $this->httpClient = $httpClient;
     $emitter = $this->httpClient->getEmitter();
     $emitter->attach(new AuthHeaderSubscriber($apiKey));
 }
 /**
  * @param string|ClientInterface $client Guzzle Client or api base url
  * @param MediawikiSession|null $session Inject a custom session here
  *
  * @throws InvalidArgumentException
  */
 public function __construct($client, $session = null)
 {
     if (is_string($client)) {
         $client = new Client(array('base_url' => $client, 'defaults' => array('headers' => array('User-Agent' => 'addwiki-guzzle-mediawiki-client'))));
     } elseif (!$client instanceof ClientInterface) {
         throw new InvalidArgumentException('$client must either be a string or ClientInterface instance');
     }
     if ($session === null) {
         $session = new MediawikiSession($this);
     } elseif (!$session instanceof MediawikiSession) {
         throw new InvalidArgumentException('$session must either me null or MediawikiSession instance');
     }
     $this->client = $client;
     $this->client->getEmitter()->attach(new Cookie(new CookieJar()));
     $this->session = $session;
 }
 /**
  * {@inheritDoc}
  */
 public function registerSubscribers(EventDispatcherInterface $eventDispatcher)
 {
     $filter = RetrySubscriber::createChainFilter([RetrySubscriber::createIdempotentFilter(), RetrySubscriber::createStatusFilter([429, 500, 503])]);
     $retry = new RetrySubscriber(['filter' => $filter, 'delay' => function ($number, $event) {
         /** @var \GuzzleHttp\Message\Response $response */
         if (null !== $event->getResponse() && $event->getResponse()->getStatusCode() === 429) {
             // Adding 20% of the waiting time as it seems to be the best result without getting two blocking reqs.
             $sleep = (int) $event->getResponse()->getHeader('retry-after') * 1.2;
             if ($sleep >= 0) {
                 return $sleep * 1000;
             }
         }
         return 0;
     }, 'max' => 3]);
     $this->client->getEmitter()->attach($retry);
 }
 public function attachKey(ClientInterface $http, $key)
 {
     $subscriber = new SimpleSubscriber(['key' => $key]);
     $http->setDefaultOption('auth', 'simple');
     $http->getEmitter()->attach($subscriber);
     return $http;
 }
 /**
  * Returns a configured HTTP client.
  *
  * The client is instantiated lazily to allow the event loop to be injected
  * into the instance of this class so that it can be used here by the
  * client adapter.
  *
  * @return \GuzzleHttp\ClientInterface
  */
 protected function getHttpClient()
 {
     if (!$this->httpClient) {
         $this->httpClient = new Client(array('base_url' => 'https://api.twitter.com/1.1/', 'adapter' => new HttpClientAdapter($this->eventLoop)));
         $this->httpClient->getEmitter()->attach($this->oauth);
     }
     return $this->httpClient;
 }
 private function attachLoggingRetrySubscribersToClient()
 {
     foreach ($this->getRetryFilters() as $filter) {
         $subscriber = new RetrySubscriber(array('filter' => RetrySubscriber::createLoggingDelay($filter, $this->logger)));
         $this->clientSubscribers[] = $subscriber;
         $this->client->getEmitter()->attach($subscriber);
     }
 }
 private function mockResponse($status, $body = null)
 {
     $mock = new Mock();
     if ($status === 200) {
         $mock->addResponse(new Response($status, array(), $body === null ? null : Stream::factory($body)));
     } else {
         $mock->addException(new RequestException('Exception', new Request('GET', 'http://graph.facebook.com/xyz')));
     }
     $this->guzzleClient->getEmitter()->attach($mock);
 }
 public function batch($callable)
 {
     //enable batching in the config
     $this->setOption('batch', true);
     //gather requests
     call_user_func_array($callable, array($this));
     $requests = $this->requests;
     $emitter = $this->client->getEmitter();
     $emitter->emit('requests.batched', new RequestsBatchedEvent($requests));
     //reset the requests for the next batch
     $this->requests = [];
     return $requests;
 }
 function let(SiteConfigBuilder $siteConfigBuilder, SiteConfig $siteConfig, Factory $authenticatorFactory, ClientInterface $guzzle, Emitter $emitter, BeforeEvent $beforeEvent, CompleteEvent $completeEvent, RequestInterface $request, ResponseInterface $response, Factory $authenticatorFactory)
 {
     $siteConfig->getHost()->willReturn('example.com');
     $siteConfigBuilder->buildForHost('example.com')->willReturn($siteConfig);
     $guzzle->getEmitter()->willReturn($emitter);
     $request->getHost()->willReturn('example.com');
     $beforeEvent->getRequest()->willReturn($request);
     $beforeEvent->getClient()->willReturn($guzzle);
     $response->getBody()->willReturn('<html></html>');
     $completeEvent->getResponse()->willReturn($response);
     $completeEvent->getRequest()->willReturn($request);
     $completeEvent->getClient()->willReturn($guzzle);
     $this->beConstructedWith($siteConfigBuilder, $authenticatorFactory);
 }
 protected function mockResponses(\GuzzleHttp\ClientInterface &$client, array $responses)
 {
     $client->getEmitter()->attach(new Mock($responses));
 }
Example #14
0
 /**
  * @param ClientInterface $client
  * @param string $token
  * @return JwtAuth
  */
 protected function attachAuthenticationSubscriber(ClientInterface $client, $token)
 {
     // create and attach JWT auth subscriber
     $auth = new JwtAuth($token);
     $client->getEmitter()->attach($auth);
     return $auth;
 }
Example #15
0
 protected function registerAuthSubscriber($credentials)
 {
     $subscriber = new Oauth1($credentials);
     $this->client->getEmitter()->attach($subscriber);
 }
Example #16
0
 /**
  * Attach a subscriber to the client
  *
  * @param SubscriberInterface $subscriber
  */
 protected function attachSubscriber(SubscriberInterface $subscriber)
 {
     $this->client->getEmitter()->attach($subscriber);
     return $this;
 }
Example #17
0
 /**
  * @inheritdoc
  */
 public function attachSubscriber(SubscriberInterface $subscriber)
 {
     $this->guzzleClient->getEmitter()->attach($subscriber);
 }
 public function getMockResponse($path)
 {
     $mock = new Mock([file_get_contents(__DIR__ . "/mock-http-responses/" . $path)]);
     $this->client->getEmitter()->attach($mock);
     return $this->client->get('/');
 }
Example #19
0
 public function attachToClient(ClientInterface &$client)
 {
     $client->getEmitter()->attach($this);
 }
 /**
  * @param string $username
  * @param string $password
  * @param ClientInterface $client
  */
 private static function mockE2ETestingData($username, $password, ClientInterface $client)
 {
     if ($username == self::TEST_MEMBER_USERNAME) {
         $mock = new Mock([new Response(404)]);
         $client->getEmitter()->attach($mock);
     }
     if ($username == self::TEST_SR_USERNAME) {
         if ($password == self::TEST_SR_PASSWORD) {
             $body = Stream::factory('[{"identifier": "mock-id1", "name": "mock-name1", "repository":' . ' "http://public.languagedepot.org", "role": "manager", "isLinked": false}, ' . '{"identifier": "mock-id2", "name": "mock-name2", "repository": ' . '"http://public.languagedepot.org", "role": "contributor", "isLinked": false}, ' . '{"identifier": "mock-id3", "name": "mock-name3", "repository": ' . '"http://public.languagedepot.org", "role": "contributor", "isLinked": false}, ' . '{"identifier": "mock-id4", "name": "mock-name4", "repository": ' . '"http://private.languagedepot.org", "role": "manager", "isLinked": false}]');
             $response = new Response(200, ['Content-Type' => 'application/json'], $body);
             $mock = new Mock([$response]);
             $client->getEmitter()->attach($mock);
         } else {
             $mock = new Mock([new Response(403)]);
             $client->getEmitter()->attach($mock);
         }
     }
 }