Skip to content

Commit cc02a4a

Browse files
Copilotneilime
andcommitted
Add documentation for HTTP retry strategy and update .gitignore
Co-authored-by: neilime <314088+neilime@users.noreply.github.com> Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent eb9260c commit cc02a4a

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
composer.lock
77
.cache
88
tests/.phpunit.result.cache
9-
build/
9+
build/

tests/Fixtures/Downloader/CachedHttpDownloader.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use GuzzleHttp\Exception\ClientException;
77
use GuzzleHttp\Exception\RequestException;
88
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
9+
use Psr\Http\Message\ResponseInterface;
10+
use RuntimeException;
911

1012
class CachedHttpDownloader
1113
{
@@ -62,7 +64,7 @@ public function fetch(string $url, bool $forceRefresh = false): string
6264
return $body;
6365
}
6466

65-
private function fetchWithRetry(string $url, array $options = []): \Psr\Http\Message\ResponseInterface
67+
private function fetchWithRetry(string $url, array $options = []): ResponseInterface
6668
{
6769
$attempt = 0;
6870
$maxRetries = $this->maxRetries;
@@ -79,7 +81,7 @@ private function fetchWithRetry(string $url, array $options = []): \Psr\Http\Mes
7981

8082
$response = $this->client->get($url, $options);
8183
return $response;
82-
84+
8385
} catch (ClientException $e) {
8486
// Check if it's a 429 (Too Many Requests) or other retryable client error
8587
if ($e->getResponse() && in_array($e->getResponse()->getStatusCode(), [429, 503, 502, 504])) {
@@ -98,7 +100,7 @@ private function fetchWithRetry(string $url, array $options = []): \Psr\Http\Mes
98100
throw $e;
99101
}
100102
}
101-
102-
throw new \RuntimeException("Max retries ({$maxRetries}) exceeded for URL: {$url}");
103+
104+
throw new RuntimeException("Max retries ({$maxRetries}) exceeded for URL: {$url}");
103105
}
104106
}

tests/Fixtures/Downloader/CssReferentialScraper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public function __construct(bool $forceRefresh = false)
2727
public function fetchReferentials(): array
2828
{
2929
$w3cReferencial = $this->fetchW3CReferential();
30-
30+
3131
// Add delay between different API calls to be respectful to servers
3232
usleep(2000000); // 2 seconds
33-
33+
3434
$mdnReferencial = $this->fetchMdnReferential();
3535

3636
$properties = $w3cReferencial['properties'] ?? [];

tests/TestSuite/CachedHttpDownloaderTest.php renamed to tests/TestSuite/Fixtures/Downloader/CachedHttpDownloaderTest.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\TestSuite;
3+
namespace Tests\TestSuite\Fixtures\Downloader;
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Exception\ClientException;
@@ -10,6 +10,9 @@
1010
use GuzzleHttp\Psr7\Request;
1111
use PHPUnit\Framework\TestCase;
1212
use Tests\Fixtures\Downloader\CachedHttpDownloader;
13+
use RecursiveDirectoryIterator;
14+
use RecursiveIteratorIterator;
15+
use ReflectionClass;
1316

1417
class CachedHttpDownloaderTest extends TestCase
1518
{
@@ -18,7 +21,7 @@ class CachedHttpDownloaderTest extends TestCase
1821
protected function setUp(): void
1922
{
2023
$this->tempCacheDir = sys_get_temp_dir() . '/test_cache_' . uniqid();
21-
mkdir($this->tempCacheDir, 0777, true);
24+
mkdir($this->tempCacheDir, 0o777, true);
2225
}
2326

2427
protected function tearDown(): void
@@ -39,15 +42,15 @@ public function testRetryOn429Error(): void
3942
$client = new Client(['handler' => $handlerStack]);
4043

4144
$downloader = new CachedHttpDownloader('test', $this->tempCacheDir, 10, 3); // 10ms delay, 3 max retries
42-
45+
4346
// Use reflection to replace the client
44-
$reflection = new \ReflectionClass($downloader);
47+
$reflection = new ReflectionClass($downloader);
4548
$clientProperty = $reflection->getProperty('client');
4649
$clientProperty->setAccessible(true);
4750
$clientProperty->setValue($downloader, $client);
4851

4952
$result = $downloader->fetch('http://test.com', true);
50-
53+
5154
$this->assertEquals('success content', $result);
5255
}
5356

@@ -65,9 +68,9 @@ public function testRetryFailsAfterMaxAttempts(): void
6568
$client = new Client(['handler' => $handlerStack]);
6669

6770
$downloader = new CachedHttpDownloader('test', $this->tempCacheDir, 10, 3); // 10ms delay, 3 max retries
68-
71+
6972
// Use reflection to replace the client
70-
$reflection = new \ReflectionClass($downloader);
73+
$reflection = new ReflectionClass($downloader);
7174
$clientProperty = $reflection->getProperty('client');
7275
$clientProperty->setAccessible(true);
7376
$clientProperty->setValue($downloader, $client);
@@ -87,15 +90,15 @@ public function testSuccessfulRequest(): void
8790
$client = new Client(['handler' => $handlerStack]);
8891

8992
$downloader = new CachedHttpDownloader('test', $this->tempCacheDir, 10, 3);
90-
93+
9194
// Use reflection to replace the client
92-
$reflection = new \ReflectionClass($downloader);
95+
$reflection = new ReflectionClass($downloader);
9396
$clientProperty = $reflection->getProperty('client');
9497
$clientProperty->setAccessible(true);
9598
$clientProperty->setValue($downloader, $client);
9699

97100
$result = $downloader->fetch('http://test.com', true);
98-
101+
99102
$this->assertEquals('success content', $result);
100103
}
101104

@@ -109,8 +112,8 @@ public function testCachedResponse(): void
109112
$client1 = new Client(['handler' => $handlerStack1]);
110113

111114
$downloader = new CachedHttpDownloader('test', $this->tempCacheDir, 10, 3);
112-
113-
$reflection = new \ReflectionClass($downloader);
115+
116+
$reflection = new ReflectionClass($downloader);
114117
$clientProperty = $reflection->getProperty('client');
115118
$clientProperty->setAccessible(true);
116119
$clientProperty->setValue($downloader, $client1);
@@ -135,10 +138,10 @@ private function removeDirectory(string $dir): void
135138
if (!is_dir($dir)) {
136139
return;
137140
}
138-
139-
$files = new \RecursiveIteratorIterator(
140-
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
141-
\RecursiveIteratorIterator::CHILD_FIRST
141+
142+
$files = new RecursiveIteratorIterator(
143+
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
144+
RecursiveIteratorIterator::CHILD_FIRST
142145
);
143146

144147
foreach ($files as $fileinfo) {
@@ -148,4 +151,4 @@ private function removeDirectory(string $dir): void
148151

149152
rmdir($dir);
150153
}
151-
}
154+
}

0 commit comments

Comments
 (0)