xman12/persistent-request

基于 Laravel 框架创建持久请求的库

1.1.0 2023-10-12 04:31 UTC

This package is auto-updated.

Last update: 2024-09-12 06:31:31 UTC


README

为什么需要它?

何时创建容错请求。

兼容性

  • php >=7.4
  • guzzlehttp/guzzle >=6.3
  • laravel/framework >=7.0
  • laravel/serializable-closure "^1.*"

Flow 库

flow

安装

composer require xman12/persistent-request

在库安装后,使用以下命令发布其配置文件

php artisan vendor:publish --provider="PersistentRequest\ServiceProvider"

或向 config/app.php 中添加以下提供者

'providers' => [PersistentRequest\ServiceProvider::class]

现在需要启动队列工作过程以处理无效请求,调用命令

php artisan queue:work

如果请求在请求从队列删除和派发 DeleteRequestEvent 事件时达到限制尝试,您可以处理此事件

创建简单持久请求的示例

// here we init service
$requestService = app(\PersistentRequest\Services\RequestServiceInterface::class);
// create instace of Request for send into guzzle client
$requestGuzzle = new \GuzzleHttp\Psr7\Request('get', 'https://google.com');
// create comfortable DTO for transfer to service, 
// first argument instance of guzzle request, second name event class, which will be call after success request
// third argument it'sleep seconds, number second after need try again do send request
// fourth argument, number try for send request
$requestDTO = new \PersistentRequest\DTO\RequestDTO($requestGuzzle, \PersistentRequest\Events\SuccessEvent::class, 30, 5);
// execute request, save into db serialize instance of RequestDTO, if request is success then row will be delete
$requestService->execute($requestDTO); 

创建使用闭包的持久请求的示例

// here we init service
$requestService = app(\PersistentRequest\Services\RequestServiceInterface::class);
// create instace of Request for send into guzzle client
$requestGuzzle = new \GuzzleHttp\Psr7\Request('get', 'https://google.com');
// create comfortable DTO for transfer to service, 
// first argument instance of guzzle request, second name event class, which will be call after success request
// third argument it'sleep seconds, number second after need try again do send request
// fourth argument, number try for send request
// fifth argrument anonymous function, inside of function we can do businec logic your application, dispatch events etc.
$requestDTO = new \PersistentRequest\DTO\RequestDTO(
    $requestGuzzle, 
    \PersistentRequest\Events\SuccessEvent::class, 
    30,
    5
    function (\GuzzleHttp\Psr7\Response $response) {
    //here we can do businec logic your application
    // if need then request try again, just throw exception
    if (200 !== $response->getStatusCode()) {
        throw new \Exception('error processed');
    }
});
// execute request
$requestService->execute($requestDTO);