Issue #3228350 by scott_euser, asirjacques, smustgrave, longwave, catch, alexpott, quietone, wotnak: oEmbed resource fetcher should allow adjusting timeout

(cherry picked from commit 2840a9f2c9)
merge-requests/8200/head
catch 2024-05-23 14:31:45 +01:00
parent 179e737044
commit c47495418d
3 changed files with 20 additions and 31 deletions

View File

@ -1,3 +1,6 @@
parameters:
media.resource_fetcher_timeout: 5
services:
_defaults:
autoconfigure: true
@ -14,7 +17,7 @@ services:
Drupal\media\OEmbed\ProviderRepositoryInterface: '@media.oembed.provider_repository'
media.oembed.resource_fetcher:
class: Drupal\media\OEmbed\ResourceFetcher
arguments: ['@http_client', '@media.oembed.provider_repository', '@cache.default']
arguments: ['@http_client', '@media.oembed.provider_repository', '@cache.default', '%media.resource_fetcher_timeout%']
Drupal\media\OEmbed\ResourceFetcherInterface: '@media.oembed.resource_fetcher'
media.oembed.iframe_url_helper:
class: Drupal\media\IFrameUrlHelper

View File

@ -15,41 +15,25 @@ use Psr\Http\Client\ClientExceptionInterface;
*/
class ResourceFetcher implements ResourceFetcherInterface {
/**
* The HTTP client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The oEmbed provider repository service.
*
* @var \Drupal\media\OEmbed\ProviderRepositoryInterface
*/
protected $providers;
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Constructs a ResourceFetcher object.
*
* @param \GuzzleHttp\ClientInterface $http_client
* @param \GuzzleHttp\ClientInterface $httpClient
* The HTTP client.
* @param \Drupal\media\OEmbed\ProviderRepositoryInterface $providers
* The oEmbed provider repository service.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
* The cache backend.
* @param int $timeout
* The length of time to wait for the request before the request
* should time out.
*/
public function __construct(ClientInterface $http_client, ProviderRepositoryInterface $providers, CacheBackendInterface $cache_backend) {
$this->httpClient = $http_client;
$this->providers = $providers;
$this->cacheBackend = $cache_backend;
public function __construct(
protected ClientInterface $httpClient,
protected ProviderRepositoryInterface $providers,
protected CacheBackendInterface $cacheBackend,
protected int $timeout = 5,
) {
}
/**
@ -65,7 +49,7 @@ class ResourceFetcher implements ResourceFetcherInterface {
try {
$response = $this->httpClient->request('GET', $url, [
RequestOptions::TIMEOUT => 5,
RequestOptions::TIMEOUT => $this->timeout,
]);
}
catch (ClientExceptionInterface $e) {

View File

@ -37,15 +37,17 @@ class ResourceFetcherTest extends UnitTestCase {
]);
$response = new Response(200, $headers, $body);
$non_default_timeout = 10;
$client = $this->prophesize(Client::class);
$client->request('GET', $url, [RequestOptions::TIMEOUT => 5])
$client->request('GET', $url, [RequestOptions::TIMEOUT => $non_default_timeout])
->shouldBeCalled()
->willReturn($response);
$fetcher = new ResourceFetcher(
$client->reveal(),
$this->createMock('\Drupal\media\OEmbed\ProviderRepositoryInterface'),
new NullBackend('default')
new NullBackend('default'),
$non_default_timeout
);
$fetcher->fetchResource($url);
}