aef/laravel-batch-requests

这是一个 Laravel 扩展包,用于高效处理批量 API 请求,通过单次批量操作减少网络开销,并提高大批量操作的性能。

v1.0.1 2024-08-13 11:38 UTC

This package is auto-updated.

Last update: 2024-09-13 11:51:33 UTC


README

Latest Version on Packagist Total Downloads License

这是一个 Laravel 扩展包,用于高效处理批量 API 请求,通过单次批量操作减少网络开销,并提高大批量操作的性能。

安装

您可以通过 composer 安装此包

composer require aef/laravel-batch-requests

配置

发布配置文件

php artisan vendor:publish --provider="LaravelBatchRequests\BatchRequestServiceProvider" --tag="config"

这将创建一个 config/batch-requests.php 文件,您可以在此文件中修改包的设置。

使用方法

use LaravelBatchRequests\Http\Controllers\BatchRequestController;

Route::post('batch', [BatchRequestController::class, 'process']);

要使用批量请求功能,向 /api/batch 端点发送包含请求数组的 JSON 负载的 POST 请求

{
  "requests": [
    {
      "id": "get-user",
      "method": "GET",
      "uri": "/api/users/1",
      "headers": {
        "Accept": "application/json"
      }
    },
    {
      "id": "create-post",
      "method": "POST",
      "uri": "/api/posts",
      "parameters": {
        "title": "New Post",
        "content": "This is the content of the new post."
      },
      "headers": {
        "Content-Type": "application/json",
        "Accept": "application/json"
      }
    }
  ]
}

响应将包含所有批量请求的结果,并将正文返回为解析后的 JSON

{
  "results": [
    {
      "id": "get-user",
      "status": 200,
      "headers": {
        "Content-Type": "application/json"
      },
      "body": {
        "id": 1,
        "name": "John Doe"
      }
    },
    {
      "id": "create-post",
      "status": 201,
      "headers": {
        "Content-Type": "application/json"
      },
      "body": {
        "id": 101,
        "title": "New Post",
        "content": "This is the content of the new post."
      }
    }
  ]
}