/** * @param array $options * @param HandlerStack $stack */ private function pushStack($options, $stack) { $options = [ApiKey::CONFIG_APIKEY => $options['apiKey'], ApiKey::CONFIG_CLIENT_ID => $options['clientId'], ApiKey::CONFIG_CLIENT_SECRET => $options['clientSecret'], ApiKey::CONFIG_TOKEN_URL => '/oauth/v2/token', ApiKey::CONFIG_AUTH_LOCATION => RequestOptions::BODY]; $token = new ApiKey($this->client, $options); $refreshToken = new RefreshToken($this->client, $options); $this->middleware = new OAuthMiddleware($this->client, $token, $refreshToken); try { $token = $this->tokenStorageProvider->get(); $this->middleware->setAccessToken($token->getAccessToken(), null, $token->getExpires()); $this->middleware->setRefreshToken($token->getRefreshToken()); } catch (TokenNotFoundException $e) { } $stack->push($this->middleware->onBefore()); $stack->push($this->middleware->onFailure(5)); }
public function testOnFailureWhichSuccessOnThirdTime() { $client = $this->createClient([RequestOptions::AUTH => 'oauth2', RequestOptions::HTTP_ERRORS => false], [MockOAuth2Server::KEY_TOKEN_INVALID_COUNT => 2, MockOAuth2Server::KEY_EXPECTED_QUERY_COUNT => 4]); $credentials = ['client_id' => 'test', 'client_secret' => 'testSecret']; $accessTokenGrantType = new ClientCredentials($client, $credentials); $middleware = new OAuthMiddleware($client, $accessTokenGrantType); $handlerStack = $this->getHandlerStack(); $handlerStack->push($middleware->onBefore()); $handlerStack->push($middleware->onFailure(5)); // Will invoke 2 times onFailure $response = $client->get('/api/collection'); $this->assertEquals(200, $response->getStatusCode()); }