3slab / vdm-library-http-transport-bundle
Vdm Http Transport
3.1.0
2021-07-07 19:40 UTC
Requires
- 3slab/vdm-library-bundle: ^3.0
- symfony/http-client: ^5.0
Requires (Dev)
- ext-curl: *
- ext-mbstring: *
- ext-xml: *
- phpunit/phpunit: ^9
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-09-08 03:08:53 UTC
README
此 symfony messenger 扩展提供了一个从 HTTP 源拉取数据的传输。
安装
composer require 3slab/vdm-library-http-transport-bundle
配置参考
framework:
messenger:
transports:
consumer:
dsn: "http://ipconfig.io/json"
retry_strategy:
max_retries: 0
options:
method: GET
http_options: {}
http_executor: ~
monitoring:
enabled: true
retry:
enabled: true
number: 5
timeBeforeRetry: 5
HTTP 执行器
HTTP 执行器允许您根据 messenger.yaml
文件中的传输定义自定义 HTTP 传输的行为。一些示例用例包括 API 具有分页或需要预请求进行身份验证。
如果在声明传输时未设置自定义 http_executor
选项,则默认使用 DefaultHttpExecutor,它只是使用您配置的 method
和 http_options
调用 API。
您可以通过提供扩展 Vdm\Bundle\LibraryBundle\Executor\Http\AbstractHttpExecutor
的类的实例来覆盖此行为。
namespace App\Executor\Http;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Vdm\Bundle\LibraryBundle\Model\Message;
use Vdm\Bundle\LibraryBundle\Stamp\StopAfterHandleStamp;
class CustomHttpExecutor extends AbstractHttpExecutor
{
public function __construct(
HttpClientInterface $httpClient,
LoggerInterface $vdmLogger = null
) {
parent::__construct($httpClient, $vdmLogger);
}
public function execute(string $dsn, string $method, array $options): iterable
{
// In HttpClient, request just build the request but does not execute it
$response = $this->httpClient->request($method, $dsn, $options);
$message = new HttpMessage($response->getContent());
yield new Envelope($message, [new StopAfterHandleStamp()]);
}
}
您的自定义执行器需要执行以下两个重要操作
yield
一个新的信封- 如果想要在处理最后一个消息后停止,则向产生的信封添加
StopAfterHandleStamp
标记(如果不这样做,messenger 工作员可能会循环并再次执行它,而不会停止)
注意:多亏了 yield 系统,您可以在 execute 函数中实现循环,并一次返回一个项目
注意:您可以在自定义执行器中保持状态,如果再次执行,则适应您的 API 调用
然后在您的项目中的传输定义 messenger.yaml
中引用此自定义执行器
framework:
messenger:
transports:
api-call:
options:
http_executor: App\Executor\Http\CustomHttpExecutor
监控
如果启用监控,它将跟踪以下指标
- HTTP 响应状态代码计数器
- HTTP 响应体大小
- HTTP 响应时间