feat : Decorator Http
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Decorator\Http;
|
||||
|
||||
use App\Interface\Test\Decorator\HttpClientInterface;
|
||||
use App\Interface\Test\Decorator\LoggerInterface;
|
||||
use Exception;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class LoggableHttpClientService implements HttpClientInterface
|
||||
{
|
||||
/**
|
||||
* @var HttpClientInterface
|
||||
*/
|
||||
private HttpClientInterface $httpClient;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private LoggerInterface $logger;
|
||||
|
||||
public function __construct(
|
||||
HttpClientInterface $httpClient,
|
||||
LoggerInterface $logger,
|
||||
)
|
||||
{
|
||||
$this->httpClient = $httpClient;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param array $options
|
||||
* @return ResponseInterface|false
|
||||
* @throws Exception
|
||||
*/
|
||||
public function request(string $method, string $url, array $options = []): ResponseInterface|false
|
||||
{
|
||||
$this->logger->log("Sending {{$method}} request to {{$url}}");
|
||||
|
||||
try {
|
||||
$response = $this->httpClient->request($method, $url, $options);
|
||||
$this->logger->log("Request to {{$url}} completed successfully");
|
||||
return $response;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error("Request to {{$url} failed: {{$e->getMessage()}}}");
|
||||
// throw $e;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Decorator\Http;
|
||||
|
||||
use App\Interface\Test\Decorator\HttpClientInterface;
|
||||
use App\Interface\Test\Decorator\LoggerInterface;
|
||||
use Exception;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class RetryableHttpClientService implements HttpClientInterface
|
||||
{
|
||||
/**
|
||||
* 请求接口注入
|
||||
* @var HttpClientInterface
|
||||
*/
|
||||
protected HttpClientInterface $httpClient;
|
||||
|
||||
/**
|
||||
* 日志接口注入
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
/**
|
||||
* 最大重试
|
||||
* @var int
|
||||
*/
|
||||
private int $maxRetries;
|
||||
|
||||
public function __construct(
|
||||
HttpClientInterface $httpClient,
|
||||
LoggerInterface $logger,
|
||||
int $maxRetries = 3
|
||||
)
|
||||
{
|
||||
$this->httpClient = $httpClient;
|
||||
$this->maxRetries = $maxRetries;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param array $options
|
||||
* @return false|ResponseInterface
|
||||
*/
|
||||
public function request(string $method, string $url, array $options = []): ResponseInterface|false
|
||||
{
|
||||
var_dump('maxRetries:'.$this->maxRetries);
|
||||
$retryCount = 0;
|
||||
|
||||
while ($retryCount < $this->maxRetries) {
|
||||
try {
|
||||
return $this->httpClient->request($method, $url, $options);
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error('重试请求'.$url.':第'.$retryCount.'次'.$e->getMessage());
|
||||
$retryCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -11,12 +11,31 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Decorator;
|
||||
|
||||
use App\Interface\Test\Decorator\HttpClientInterface;
|
||||
use App\Service\Test\TestBaseService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class HttpService extends TestBaseService
|
||||
{
|
||||
public function handle()
|
||||
/**
|
||||
* @var HttpClientInterface
|
||||
*/
|
||||
#[Inject]
|
||||
protected HttpClientInterface $httpClient;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
return $this->return->success();
|
||||
$response = $this->httpClient->request('GET','https://api.example.com/data',[
|
||||
'timeout' => 5
|
||||
]);
|
||||
|
||||
if (!$response) return $this->return->error();
|
||||
|
||||
return $this->return->success('请求成功',[
|
||||
'body' => json_decode($response->getBody()->getContents(), true),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user