Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit ee74043

Browse files
committed
:octocat: let OAuthProvider also act as PSR-18 ClientInterface
1 parent bf51d58 commit ee74043

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/Core/OAuthProvider.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use chillerlan\OAuth\Storage\OAuthStorageInterface;
1919
use chillerlan\Settings\SettingsContainerInterface;
2020
use Psr\Http\Client\ClientInterface;
21-
use Psr\Http\Message\{RequestFactoryInterface, ResponseInterface, StreamFactoryInterface, StreamInterface, UriFactoryInterface};
21+
use Psr\Http\Message\{RequestInterface, RequestFactoryInterface, ResponseInterface, StreamFactoryInterface, StreamInterface, UriFactoryInterface};
2222
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger};
2323
use ReflectionClass;
2424

@@ -29,7 +29,7 @@
2929
* @property string $serviceName
3030
* @property string $userRevokeURL
3131
*/
32-
abstract class OAuthProvider implements OAuthInterface, ApiClientInterface, LoggerAwareInterface{
32+
abstract class OAuthProvider implements OAuthInterface, ApiClientInterface, ClientInterface, LoggerAwareInterface{
3333
use LoggerAwareTrait;
3434

3535
/**
@@ -286,22 +286,10 @@ protected function cleanBodyParams(array $params):array{
286286
* @return \Psr\Http\Message\ResponseInterface
287287
*/
288288
public function request(string $path, array $params = null, string $method = null, $body = null, array $headers = null):ResponseInterface{
289-
$token = $this->storage->getAccessToken($this->serviceName);
290-
291-
// attempt to refresh an expired token
292-
if($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)){
293-
$token = $this->refreshAccessToken($token);
294-
}
295289

296290
$request = $this->requestFactory
297291
->createRequest($method ?? 'GET', Psr7\merge_query($this->apiURL.$path, $params ?? []));
298292

299-
foreach(array_merge($this->apiHeaders, $headers ?? []) as $header => $value){
300-
$request = $request->withAddedHeader($header, $value);
301-
}
302-
303-
$request = $this->getRequestAuthorization($request, $token);
304-
305293
if(is_array($body) && $request->hasHeader('content-type')){
306294
$contentType = strtolower($request->getHeaderLine('content-type'));
307295

@@ -318,6 +306,28 @@ public function request(string $path, array $params = null, string $method = nul
318306
$request = $request->withBody($body);
319307
}
320308

309+
return $this->sendRequest($request);
310+
}
311+
312+
/**
313+
* @param \Psr\Http\Message\RequestInterface $request
314+
*
315+
* @return \Psr\Http\Message\ResponseInterface
316+
*/
317+
public function sendRequest(RequestInterface $request):ResponseInterface{
318+
$token = $this->storage->getAccessToken($this->serviceName);
319+
320+
// attempt to refresh an expired token
321+
if($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)){
322+
$token = $this->refreshAccessToken($token);
323+
}
324+
325+
foreach(array_merge($this->apiHeaders, $headers ?? []) as $header => $value){
326+
$request = $request->withAddedHeader($header, $value);
327+
}
328+
329+
$request = $this->getRequestAuthorization($request, $token);
330+
321331
return $this->http->sendRequest($request);
322332
}
323333

0 commit comments

Comments
 (0)