kielabokkie / laravel-apise
通过Guzzle与外部API交互的基类
Requires
- php: ^7.2
- guzzlehttp/guzzle: ^6.3|^7.0
- kielabokkie/laravel-conceal: ^1.1
- ramsey/uuid: ^3.9|^4.0
- rtheunissen/guzzle-log-middleware: ^0.4.2|^1.0
Requires (Dev)
- laravel/framework: ~5.8.0|^6.0|^7.0|^8.0
- orchestra/testbench: ^3.7|^4.0|^5.0|^6.0
- symfony/var-dumper: ^4.1|^5.0
- dev-master
- v1.0.1
- v1.0.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-dependabot/npm_and_yarn/json5-1.0.2
- dev-dependabot/npm_and_yarn/express-4.18.2
- dev-dependabot/npm_and_yarn/qs-and-express-6.11.0
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/loader-utils-1.4.2
- dev-dependabot/npm_and_yarn/moment-2.29.4
- dev-dependabot/npm_and_yarn/eventsource-1.1.1
- dev-dependabot/npm_and_yarn/async-2.6.4
- dev-dependabot/npm_and_yarn/minimist-1.2.6
- dev-dependabot/npm_and_yarn/url-parse-1.5.10
- dev-dependabot/npm_and_yarn/follow-redirects-1.14.8
- dev-dependabot/npm_and_yarn/path-parse-1.0.7
- dev-dependabot/npm_and_yarn/ws-6.2.2
- dev-dependabot/npm_and_yarn/dns-packet-1.3.4
- dev-dependabot/npm_and_yarn/browserslist-4.16.6
- dev-dependabot/npm_and_yarn/hosted-git-info-2.8.9
- dev-dependabot/npm_and_yarn/lodash-4.17.21
- dev-dependabot/npm_and_yarn/ssri-6.0.2
This package is auto-updated.
Last update: 2024-09-06 11:03:03 UTC
README
Apise for Laravel可用于简化创建用于与外部JSON API集成的API服务。它还附带了一个可选的UI,用于查看API服务的请求和响应数据。
要求
- PHP >= 7.2
- Laravel 5.8 | 6 | 7 | 8
注意:Laravel 8需要PHP 7.3
安装
通过composer安装包
composer require kielabokkie/laravel-apise
安装包后,您需要发布其资产
php artisan apise:install
运行迁移以设置用于日志记录的表
php artisan migrate
更新Apise
当有Apise的新版本时,始终使用以下命令发布资产,以确保您有最新的JavaScript和其他资产。
php artisan vendor:publish --tag=apise-assets
包配置
包的配置可以在config/apise.php中找到。以下是配置文件的详细内容
return [ /** * The namespace where your API Service classes are created under. * This will be appended to your base namespace. So the config below * will create a class under App\Support\Services. */ 'namespace' => 'Support\Services', /** * These middlewares will be assigned to the Apise routes. You can * add your own middleware to this list or change any of the existing * middleware. */ 'middleware' => [ 'web', Authorize::class, ], /** * Enable logging of requests and responses */ 'logging_enabled' => env('APISE_LOGGING_ENABLED', true), /** * Enable concealing of sensitive data */ 'conceal_enabled' => env('APISE_CONCEAL_ENABLED', true), /** * Keys that should be concealed when displayed on the Apise UI */ 'conceal_keys' => [ 'api_key' ] /** * This is the URI path where the UI will be accessible from */ 'path' => env('APISE_PATH', 'apise'), ];
日志记录
默认情况下,启用所有请求的日志记录。这将为您在/apise下提供一个页面,您可以在此处检查请求和响应数据。启用日志记录时会有一些开销,因为记录将被保存到数据库中。要禁用日志记录,请将以下内容添加到您的.env文件中
APISE_LOGGING_ENABLED=false
Apise UI
如前所述,Apise的UI可以通过/apise访问。如果您想更改此设置,可以通过设置以下环境变量来完成
APISE_PATH='admin/apise'
隐藏敏感数据
您可能不希望将敏感数据存储在数据库中,因此Apise将使隐藏此类数据变得容易。在后台,Apise使用kielabokie/laravel-conceal包自动隐藏敏感头和请求数据。默认情况下,包隐藏password和password_confirmation字段的值,但您可以在apise.php配置文件中的conceal_keys数组中添加任何您想要的键。
'conceal_keys' => [ 'api_key', 'Authorization', 'token' ]
设置
要使用基础API客户端类,您需要添加所需的$baseUrl来设置API的基本URL。您还必须在服务类的构造函数中调用$this->setClient();函数。
<?php namespace App\Support\Services; use Kielabokkie\Apise\ApiseClient; class HttpBinService extends ApiseClient { protected $baseUrl = 'https://httpbin.org'; public function __construct() { $this->setClient(); } }
为了便于入门,您可以使用以下命令来生成API服务类
php artisan make:api-service HttpBinService
这将在app/Support/Services文件夹中创建一个名为HttpBinService.php的类。您只需设置您的$baseUrl即可。
注意:如果您希望将类放在其他位置,可以覆盖apise.php配置文件中的namespace变量。
用法
获取请求
现在要执行一个GET请求,您只需做以下操作
public function getUsers() { $response = $this->get('/users')); return json_decode($response->getBody()); }
这是一些基本操作,与您通常使用Guzzle进行GET请求的方式相同。
添加默认头
API通常要求您在每个请求中添加一个特定的头,例如用于授权。为了避免在每次请求中作为选项传递,您可以在服务类的顶部添加以下函数
protected function defaultHeaders() { return [ 'Authorization' => 'Bearer abcdef123456', ]; }
添加默认查询参数
您可以通过类似的方式自动添加每个请求的默认查询参数
protected function defaultQueryParams() { return [ 'token' => 'your-token' ]; }
这将为get参数自动附加令牌,如下所示:https://httpbin.org/get?token=your-token。
清除日志
根据您的API服务执行的调用次数,日志表可以快速填满。您可以通过运行修剪命令来删除任何超过给定小时数的旧日志。
php artisan apise:prune --hours 24
要自动清理旧日志,可以将它添加到Laravel任务调度器app/Console/Kernel.php中,例如:
protected function schedule(Schedule $schedule) { // Clean up logs older than 3 days $schedule->command('apise:prune --hours=72') ->daily(); }
鸣谢
此包从laravel/telescope包借鉴了许多通用结构。所有功劳都属于原始作者。
开发
当你在显示日志的视图中工作时,可以运行Webpack开发服务器
npm run hot
这将启用热重载并在http://127.0.0.1:8080上运行开发服务器。
