src/FM/Keystone/Client/Factory.php
public function onRequestError(Event $event)
    {
        // if token validity expired, re-request with a new token.
        if (in_array($event['response']->getStatusCode(), array(401, 403))) {
            /** @var \Guzzle\Http\Message\Request $request */
            $request = $event['request'];

            // if this is the token-url, stop now because we won't be able to fetch a token
            if ($request->getUrl() === $request->getClient()->getTokenUrl()) {
                return;
            }

            // see if we have retries left
            if ($request->hasHeader('X-Auth-Retries')) {
                $headerValues = $request->getHeader('X-Auth-Retries')->toArray();
                $retriesValue = array_shift($headerValues);
            }

            $retries = $request->hasHeader('X-Auth-Retries') ? $retriesValue : 1;
            if ($retries < 1) {
                if ($this->logger) {
                    $this->logger->addError('Keystone request failed, no more retries left');
                }

                return;
            }

            if ($this->logger) {
                $this->logger->addDebug('Token expired, fetching a new one');
            }

            // set new token in client
            $client = $request->getClient();
            $this->resetToken($client);

            // clone request and update token header
            $newRequest = clone $request;
            $newRequest->setHeader('X-Auth-Token', $client->getToken()->getId());
            $newRequest->setHeader('X-Auth-Retries', --$retries);
            $newResponse = $newRequest->send();

            // Set the response object of the request without firing more events
            $event['response'] = $newResponse;

            // Stop other events from firing when you override 401 responses
            $event->stopPropagation();
        }
    }
public function onRequestError(Event $event)
    {
        // if token validity expired, re-request with a new token.
        if (in_array($event['response']->getStatusCode(), array(401, 403))) {
            /** @var \Guzzle\Http\Message\Request $request */
            $request = $event['request'];

            // if this is the token-url, stop now because we won't be able to fetch a token
            if ($request->getUrl() === $request->getClient()->getTokenUrl()) {
                return;
            }

            // see if we have retries left
            if ($request->hasHeader('X-Auth-Retries')) {
                $headerValues = $request->getHeader('X-Auth-Retries')->toArray();
                $retriesValue = array_shift($headerValues);
            }

            $retries = $request->hasHeader('X-Auth-Retries') ? $retriesValue : 1;
            if ($retries < 1) {
                if ($this->logger) {
                    $this->logger->addError('Keystone request failed, no more retries left');
                }

                return;
            }

            if ($this->logger) {
                $this->logger->addDebug('Token expired, fetching a new one');
            }

            // set new token in client
            $client = $request->getClient();
            $this->resetToken($client);

            // clone request and update token header
            $newRequest = clone $request;
            $newRequest->setHeader('X-Auth-Token', $client->getToken()->getId());
            $newRequest->setHeader('X-Auth-Retries', --$retries);
            $newResponse = $newRequest->send();

            // Set the response object of the request without firing more events
            $event['response'] = $newResponse;

            // Stop other events from firing when you override 401 responses
            $event->stopPropagation();
        }
    }
public function onRequestError ( Event $ event ) { // if token validity expired, re-request with a new token. if ( in_array ( $ event [ 'response' ] -> getStatusCode ( ) , array ( 401 , 403 ) ) ) { /** @var \Guzzle\Http\Message\Request $request */ $ request = $ event [ 'request' ] ; // if this is the token-url, stop now because we won't be able to fetch a token if ( $ request -> getUrl ( ) === $ request -> getClient ( ) -> getTokenUrl ( ) ) { return ; } // see if we have retries left if ( $ request -> hasHeader ( 'X-Auth-Retries' ) ) { $ headerValues = $ request -> getHeader ( 'X-Auth-Retries' ) -> toArray ( ) ; $ retriesValue = array_shift ( $ headerValues ) ; } $ retries = $ request -> hasHeader ( 'X-Auth-Retries' ) ? $ retriesValue : 1 ; if ( $ retries < 1 ) { if ( $ this -> logger ) { $ this -> logger -> addError ( 'Keystone request failed, no more retries left' ) ; } return ; } if ( $ this -> logger ) { $ this -> logger -> addDebug ( 'Token expired, fetching a new one' ) ; } // set new token in client $ client = $ request -> getClient ( ) ; $ this -> resetToken ( $ client ) ; // clone request and update token header $ newRequest = clone $ request ; $ newRequest -> setHeader ( 'X-Auth-Token' , $ client -> getToken ( ) -> getId ( ) ) ; $ newRequest -> setHeader ( 'X-Auth-Retries' , -- $ retries ) ; $ newResponse = $ newRequest -> send ( ) ; // Set the response object of the request without firing more events $ event [ 'response' ] = $ newResponse ; // Stop other events from firing when you override 401 responses $ event -> stopPropagation ( ) ; } }
Listener for request errors. Handles requests with expired
authentication, by reauthenticating and sending the request again.
Listener for request errors . Handles requests with expired authentication by reauthenticating and sending the request again .