18
18
use chillerlan \OAuth \Storage \OAuthStorageInterface ;
19
19
use chillerlan \Settings \SettingsContainerInterface ;
20
20
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 };
22
22
use Psr \Log \{LoggerAwareInterface , LoggerAwareTrait , LoggerInterface , NullLogger };
23
23
use ReflectionClass ;
24
24
29
29
* @property string $serviceName
30
30
* @property string $userRevokeURL
31
31
*/
32
- abstract class OAuthProvider implements OAuthInterface, ApiClientInterface, LoggerAwareInterface{
32
+ abstract class OAuthProvider implements OAuthInterface, ApiClientInterface, ClientInterface, LoggerAwareInterface{
33
33
use LoggerAwareTrait;
34
34
35
35
/**
@@ -286,22 +286,10 @@ protected function cleanBodyParams(array $params):array{
286
286
* @return \Psr\Http\Message\ResponseInterface
287
287
*/
288
288
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
- }
295
289
296
290
$ request = $ this ->requestFactory
297
291
->createRequest ($ method ?? 'GET ' , Psr7 \merge_query ($ this ->apiURL .$ path , $ params ?? []));
298
292
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
-
305
293
if (is_array ($ body ) && $ request ->hasHeader ('content-type ' )){
306
294
$ contentType = strtolower ($ request ->getHeaderLine ('content-type ' ));
307
295
@@ -318,6 +306,28 @@ public function request(string $path, array $params = null, string $method = nul
318
306
$ request = $ request ->withBody ($ body );
319
307
}
320
308
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
+
321
331
return $ this ->http ->sendRequest ($ request );
322
332
}
323
333
0 commit comments