public function testInterceptsWithEvent()
 {
     $client = new Client();
     $request = new Request('GET', '/');
     $response = new Response(404);
     $transaction = new Transaction($client, $request);
     $except = new RequestException('foo', $request, $response);
     $event = new ErrorEvent($transaction, $except);
     $event->throwImmediately(true);
     $this->assertTrue($except->getThrowImmediately());
     $event->throwImmediately(false);
     $this->assertFalse($except->getThrowImmediately());
     $this->assertSame($except, $event->getException());
     $this->assertSame($response, $event->getResponse());
     $this->assertSame($request, $event->getRequest());
     $res = null;
     $request->getEmitter()->on('complete', function ($e) use(&$res) {
         $res = $e;
     });
     $good = new Response(200);
     $event->intercept($good);
     $this->assertTrue($event->isPropagationStopped());
     $this->assertSame($res->getClient(), $event->getClient());
     $this->assertSame($good, $res->getResponse());
 }
Esempio n. 2
0
 public function onError(ErrorEvent $event, $name)
 {
     $params = Base::parse($event->getResponse());
     $response = new \GuzzleHttp\Message\Response(200);
     $event->intercept($response);
     $event->stopPropagation();
 }
Esempio n. 3
0
 public function onError(ErrorEvent $event)
 {
     //If an error occurs see if data can be served from cache
     $request = $event->getRequest();
     if ($response = $this->cache->fetch($request->__toString())) {
         $event->intercept($response);
     }
 }
 public function onError(ErrorEvent $event, $name)
 {
     if ($event->getResponse()->getStatusCode() == 401) {
         $params = ['userName' => $this->credentials->getUsername(), 'password' => $this->credentials->getPassword(), 'createPersistentCookie' => 'false'];
         $this->commandClient->getCookie($params, ['http' => ['cookies' => true]]);
         $newResponse = $event->getClient()->send($event->getRequest());
         // Intercept the original transaction with the new response
         $event->intercept($newResponse);
     }
 }
Esempio n. 5
0
 public function onError(ErrorEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->canCacheRequest($request)) {
         return;
     }
     $response = $this->storage->fetch($request);
     if ($response) {
         $request->getConfig()->set('cache_hit', 'ERROR');
         $event->intercept($response);
     }
 }
 /**
  * @inheritdoc
  */
 public function onError(ErrorEvent $event)
 {
     $response = $event->getResponse();
     if ($response && 401 == $response->getStatusCode()) {
         $request = $event->getRequest();
         if ($request->getConfig()->get('auth') == 'oauth2' && !$request->getConfig()->get('retried')) {
             if ($token = $this->acquireAccessToken()) {
                 $this->accessToken = $token;
                 $request->getConfig()->set('retried', true);
                 $event->intercept($event->getClient()->send($request));
             }
         }
     }
 }
Esempio n. 7
0
 public function onError(ErrorEvent $event)
 {
     $params = Base::parse($event->getResponse());
     if (isset($params['error']['code'])) {
         //invalid session possibly, let's reset the token
         if ($params['error']['code'] == 100) {
             $this->service->resetAccessToken();
         }
     }
     \Log::info('Facebook Error: ' . (string) $event->getResponse()->getBody());
     $response = new \GuzzleHttp\Message\Response(200);
     $event->intercept($response);
     $event->stopPropagation();
 }
Esempio n. 8
0
 /**
  * Prevent a request from sending and intercept it's complete event.
  *
  * This method is required when a request fails before sending to prevent
  * adapters from still transferring the request over the wire.
  */
 private static function stopRequestError(ErrorEvent $e)
 {
     $fn = function ($ev) {
         $ev->stopPropagation();
     };
     $e->getRequest()->getEmitter()->once('complete', $fn, 'first');
     $e->intercept(new CanceledResponse());
 }
 /**
  * If the request failed, then check if a cached response would suffice.
  *
  * @param ErrorEvent $event
  */
 public function onError(ErrorEvent $event)
 {
     $request = $event->getRequest();
     if (!call_user_func($this->canCache, $request)) {
         return;
     }
     $response = $this->storage->fetch($request);
     // Intercept the failed response if possible
     if ($response && $this->validateFailed($request, $response)) {
         $request->getConfig()->set('cache_hit', 'error');
         $response->setHeader('Age', Utils::getResponseAge($response));
         $event->intercept($response);
     }
 }
 /**
  * Request error event handler.
  *
  * Handles unauthorized errors by acquiring a new access token and
  * retrying the request.
  *
  * @param ErrorEvent $event Event received
  */
 public function onError(ErrorEvent $event)
 {
     if (!$event->getResponse() || $event->getResponse()->getStatusCode() == 401) {
         if ($event->getRequest()->getHeader('X-Guzzle-Retry')) {
             // We already retried once, give up.
             return;
         }
         // Acquire a new access token, and retry the request.
         $this->acquireAccessToken();
         if ($this->tokenData->accessToken) {
             $newRequest = clone $event->getRequest();
             $this->accessTokenSigner->sign($newRequest, $this->tokenData->accessToken);
             $newRequest->setHeader('X-Guzzle-Retry', '1');
             $event->intercept($event->getClient()->send($newRequest));
         }
     }
 }
Esempio n. 11
0
 /**
  * Prevent a request from sending and intercept it's complete event.
  *
  * This method is required when a request fails before sending to prevent
  * adapters from still transferring the request over the wire.
  */
 private static function stopRequestError(ErrorEvent $e)
 {
     $fn = function ($ev) {
         /** @noinspection PhpUndefinedMethodInspection */
         $ev->stopPropagation();
     };
     $e->getRequest()->getEmitter()->once('complete', $fn, 'first');
     $e->intercept(new CanceledResponse());
 }