initital commit
This commit is contained in:
79
src/Utils/RetryableHttpClient.php
Normal file
79
src/Utils/RetryableHttpClient.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
|
||||
|
||||
class RetryableHttpClient implements HttpClientInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly LoggerInterface $logger,
|
||||
protected readonly HttpClientInterface $client,
|
||||
protected int $maxRetries = 3,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<mixed> $options
|
||||
*/
|
||||
public function request(string $method, string $url, array $options = []): ResponseInterface
|
||||
{
|
||||
$lastException = new \InvalidArgumentException("$this->maxRetries < 1");
|
||||
|
||||
for ($i = 1; $i <= $this->maxRetries; ++$i) {
|
||||
try {
|
||||
$response = $this->client->request($method, $url, $options);
|
||||
|
||||
// TODO: dynamic filters for codes & handle 400 error, slow down response ...
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
$this->logger->warning("Response $method on $url failed.", [
|
||||
'statusCode' => $response->getStatusCode(),
|
||||
]);
|
||||
// TODO: better exception
|
||||
$lastException = new \RuntimeException("Invalid status code, accept: 200, got " . $response->getStatusCode());
|
||||
continue;
|
||||
}
|
||||
|
||||
return $response;
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->error("Response $method on $url failed.", [
|
||||
'exception' => $exception,
|
||||
]);
|
||||
$lastException = $exception;
|
||||
}
|
||||
}
|
||||
|
||||
throw $lastException;
|
||||
}
|
||||
|
||||
public function stream(iterable|ResponseInterface $responses, ?float $timeout = null): ResponseStreamInterface
|
||||
{
|
||||
$lastException = new \InvalidArgumentException("$this->maxRetries < 1");
|
||||
|
||||
for ($i = 1; $i <= $this->maxRetries; ++$i) {
|
||||
try {
|
||||
return $this->client->stream($responses, $timeout);
|
||||
} catch (\Exception $exception) {
|
||||
$this->logger->error("Stream failed.", [
|
||||
'exception' => $exception,
|
||||
]);
|
||||
$lastException = $exception;
|
||||
}
|
||||
}
|
||||
|
||||
throw $lastException;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<mixed> $options
|
||||
*/
|
||||
public function withOptions(array $options): static
|
||||
{
|
||||
return new static($this->logger, $this->client->withOptions($options), $this->maxRetries); // @phpstan-ignore new.static
|
||||
}
|
||||
}
|
||||
10
src/Utils/Weather/WeatherInterval.php
Normal file
10
src/Utils/Weather/WeatherInterval.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Utils\Weather;
|
||||
|
||||
enum WeatherInterval
|
||||
{
|
||||
case DAILY;
|
||||
}
|
||||
Reference in New Issue
Block a user