/** * Set request access_token query. */ protected function attachAccessToken() { if (!$this->accessToken) { return; } // log $this->getHttp()->addMiddleware(function (callable $handler) { return function (RequestInterface $request, array $options) use($handler) { $field = $this->accessToken->getQueryName(); $token = $this->accessToken->getToken(); $request = $request->withUri(Uri::withQueryValue($request->getUri(), $field, $token)); Log::debug("Request Token: {$token}"); Log::debug('Request Uri: ' . $request->getUri()); return $handler($request, $options); }; }); // retry $this->getHttp()->addMiddleware(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, RequestException $exception = null) { // Limit the number of retries to 2 if ($retries <= 2 && $response && ($body = $response->getBody())) { // Retry on server errors if (stripos($body, 'errcode') && (stripos($body, '40001') || stripos($body, '42001'))) { return true; } } return false; })); }
/** * Test getToken() without cache. */ public function testNonCachedGetToken() { $cacheObj = new stdClass(); // non-cached $cache = Mockery::mock(Cache::class, function ($mock) use($cacheObj) { $mock->shouldReceive('fetch')->andReturnUsing(function ($cacheKey) { return; }); $mock->shouldReceive('save')->andReturnUsing(function ($key, $token, $expire) use($cacheObj) { $cacheObj->cacheKey = $key; $cacheObj->token = $token; $cacheObj->expire = $expire; return $token; }); }); $http = Mockery::mock(Http::class . '[get]', function ($mock) { $mock->shouldReceive('get')->andReturn(json_encode(['access_token' => 'thisIsATokenFromHttp', 'expires_in' => 7200])); }); $accessToken = new AccessToken('appId', 'secret', $cache); $accessToken->setHttp($http); $this->assertEquals('thisIsATokenFromHttp', $accessToken->getToken()); $this->assertEquals('thisIsATokenFromHttp', $cacheObj->token); $this->assertEquals(5700, $cacheObj->expire); }
/** * Return retry middleware. * * @return \GuzzleHttp\RetryMiddleware */ protected function retryMiddleware() { return Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, RequestException $exception = null) { // Limit the number of retries to 2 if ($retries <= 2 && $response && ($body = $response->getBody())) { // Retry on server errors if (stripos($body, 'errcode') && (stripos($body, '40001') || stripos($body, '42001'))) { $field = $this->accessToken->getQueryName(); $token = $this->accessToken->getToken(); $request = $request->withUri($newUri = Uri::withQueryValue($request->getUri(), $field, $token)); Log::debug("Retry with Request Token: {$token}"); Log::debug("Retry with Request Uri: {$newUri}"); return true; } } return false; }); }
/** * constructor * * @param AccessToken $token */ public function __construct($token = null) { $this->token = $token instanceof AccessToken ? $token->getToken() : $token; parent::__construct(); }
public function __construct($appId, OpenPlatform $openPlatform, $componentRefreshToken) { parent::__construct($appId, ''); $this->openPlatform = $openPlatform; $this->componentRefreshToken = $componentRefreshToken; }
public function __construct(Config $config, Ticket $ticket, Cache $cache = null) { parent::__construct($config->get('app_id'), $config->get('secret'), $cache); $this->ticket = $ticket; }
public function __construct(OpenPlatform $openPlatform, $queryAuthCode) { parent::__construct('wx570bc396a51b8ff8', ''); $this->openPlatform = $openPlatform; $this->queryAuthCode = $queryAuthCode; }