uncgits/ccps-api-logging-stats

本包最新版本(1.0.1)没有提供许可信息。

CCPS 框架的 API 调用日志和统计包

1.0.1 2020-11-18 17:18 UTC

This package is auto-updated.

Last update: 2024-09-19 01:32:11 UTC


README

本包是用于构建CCPS Core应用的一个插件,旨在提供关于应用程序进行的API调用数量的日志和统计信息。

本包提供以下功能

  • 视图和控制器
  • 数据库迁移
  • 事件/监听器对

谁在维护此包?

UNCG CCPS 开发者小组维护此应用 - ccps-developers-l@uncg.edu

安装

1. 需求包

composer require uncgits/ccps-api-logging-stats

2. 注册服务提供者

由于 CCPS Core 需要包含包自动发现的 Laravel 版本,因此注册服务提供者不是必需的。

3. 运行迁移

php artisan migrate

4. 添加路由

在您的 routes/web.php 文件中,将统计页面路由添加到您的应用的路由文件中(指向 Uncgits\CcpsApiLog\Http\Controllers\StatisticsController@index)。例如:

Route::get('apistats', 'Uncgits\CcpsApiLog\Http\Controllers\StatisticsController@index')

使用方法

您不需要在 EventServiceProvider.php 文件中注册事件+监听器组合 - 这在本包的 ServiceProvider.php 文件中处理。

触发事件

使用此包时,您只需要在尝试API调用后触发事件。该事件期望两个参数 - 第一个参数是 调用的结果集,第二个是 服务名称

所以您可能会这样做

// make API call however you do in your package.
$resultset = $api->makeCall('param', 'param', 'etc');
event(new ApiCallAttempted($resultset, 'my-service-name'));

结果集 是一个数组,应至少包含以下内容

  • 方法(GET、POST 等),
  • 端点(可以是您想要的任何内容 - 完整URI或表示哪个API方法被调用的自定义字符串等),
  • 结果代码(HTTP代码,如 200、403 等),
  • 结果原因(“OK”、“禁止”等)。

在监听器中,这些值将被查找并传递给 ApiCallStatistic 模型以插入到数据库中

'service' => $event->service,
'method' => $event->result['response']['method'],
'endpoint' => $event->result['response']['endpoint'],
'result_code' => $event->result['response']['httpCode'],
'result_reason' => $event->result['response']['httpReason']

将来可能可以对这些键进行自定义,但目前它们是硬编码的。