示例#1
0
 public function onRequestBeforeSend(Event $event)
 {
     if ($this->queue) {
         $request = $event['request'];
         $this->received[] = $request;
         // Detach the filter from the client so it's a one-time use
         if ($this->temporary && count($this->queue) == 1 && $request->getClient()) {
             $request->getClient()->getEventDispatcher()->removeSubscriber($this);
         }
         $this->dequeue($request);
         $event->stopPropagation();
     }
 }
 /**
  * Map Laravel's soft delete action to Kinvey's user suspend
  * endpoint.
  *
  * @param  Guzzle\Common\Event
  * @return void
  */
 public function beforePrepare(Event $event)
 {
     $command = $event['command'];
     $operation = $command->getOperation();
     $client = $command->getClient();
     if ($command->getName() !== 'updateEntity') {
         return;
     }
     if ($operation->getParam('collection')->getDefault() !== 'user') {
         return;
     }
     // Attempt to get the model's deleted at value from the array of values passed
     // to the command.
     $statusValue = false;
     if ($command->offsetExists(Model::DELETED_AT)) {
         $statusValue = $command->offsetGet(Model::DELETED_AT);
     } elseif ($command->offsetExists('_kmd')) {
         $_kmd = array_dot($command->offsetGet('_kmd'));
         $statusField = MODEL::DELETED_AT;
         $statusField = str_replace('_kmd.', '', MODEL::DELETED_AT);
         if (array_key_exists($statusField, $_kmd)) {
             $statusValue = $_kmd[$statusField];
         }
     }
     // Could not determine the status field.
     if ($statusValue === false) {
         return;
     } elseif (!is_null($statusValue)) {
         $event->stopPropagation();
         $suspendCommand = $client->getCommand('deleteEntity', array('collection' => 'user', '_id' => $command->offsetGet('_id')));
         $suspendCommand->execute();
     } else {
         $event->stopPropagation();
         $restoreCommand = $client->getCommand('restore', array('_id' => $command->offsetGet('_id')));
         $restoreCommand->execute();
     }
 }
 public function onRequestError(Event $event)
 {
     if ($event['response']->getStatusCode() == 401) {
         if ($event['request']->getHeader('X-Retry-Count')) {
             // We already retried once, give up.
             return;
         }
         // Acquire a new access token, and retry the request.
         $newAccessToken = $this->acquireAccessToken();
         if ($newAccessToken) {
             $newRequest = clone $event['request'];
             $newRequest->setHeader('Authorization', 'Bearer ' . $newAccessToken->getToken());
             $newRequest->setHeader('X-Retry-Count', '1');
             $event['response'] = $newRequest->send();
             $event->stopPropagation();
         }
     }
 }
示例#4
0
 /**
  * Throws a more meaningful request exception if available
  *
  * @param Event $event Event emitted
  */
 public function onRequestError(Event $event)
 {
     $e = $this->factory->fromResponse($event['request'], $event['response']);
     $event->stopPropagation();
     throw $e;
 }
示例#5
0
 /**
  * If possible, set a cache response on a cURL exception
  *
  * @param Event $event
  *
  * @return null
  */
 public function onRequestException(Event $event)
 {
     if (!$event['exception'] instanceof CurlException) {
         return;
     }
     $request = $event['request'];
     if (!$this->canCache->canCacheRequest($request)) {
         return;
     }
     if ($response = $this->storage->fetch($request)) {
         $response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now'));
         if (!$this->canResponseSatisfyFailedRequest($request, $response)) {
             return;
         }
         $request->getParams()->set('cache.hit', 'error');
         $request->setResponse($response);
         $this->addResponseHeaders($request, $response);
         $event->stopPropagation();
     }
 }
 /**
  * Function: refreshToken()
  * Parameters:
  *    $event = Guzzle\Common\Event
  * Description: Attempts to reconnect with new token on 401
  * Returns: VOID
  */
 public function refreshToken(Event $event)
 {
     if ($event['response']->getStatusCode() === 401) {
         $this->setToken($this->getNewAuthToken());
         $event['response'] = $event['request']->send();
         $event->stopPropagation();
     }
 }
示例#7
0
 public function onRequestError(Event $event)
 {
     if ($event['response']->getStatusCode() == 401) {
         // The access token has expired.  We need to get a new token using the refresh token,
         // and then sign a new request with the new access token
         if ($storage = self::$config['storage']['getRefresh']) {
             $client = new Client(self::URL_STUB);
             if ($id = self::getConfig('userID')) {
                 $params = array('refresh_token' => $storage($id), 'client_id' => self::$config['clientId'], 'client_secret' => self::$config['clientSecret'], 'grant_type' => 'refresh_token');
             } else {
                 $params = array('refresh_token' => $storage(), 'client_id' => self::$config['clientId'], 'client_secret' => self::$config['clientSecret'], 'grant_type' => 'refresh_token');
             }
             try {
                 $response = $client->post('token')->addPostFields($params)->send();
             } catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
                 throw new InvalidRefresh();
             }
             foreach ($response->json() as $key => $val) {
                 if ($key == 'access_token') {
                     $storage = self::$config['storage']['token'];
                     if ($id = self::getConfig('userID')) {
                         $storage($val, $id);
                     } else {
                         $storage($val);
                     }
                 }
             }
             $newRequest = clone $event['request'];
             $newRequest = $newRequest->setHeader('Authorization', $this->_buildAuthorizationHeader())->send();
             $event['response'] = $newRequest;
         } else {
             throw new KeyExpired();
         }
         $event->stopPropagation();
     }
 }