dot-env-it/laravel-api-integrator

简化第三方API集成的包。使用此包,可以使API调用看起来像你代码的一部分。无需记住任何API的基础URL或路径。

v1.3.0 2023-11-12 07:20 UTC

This package is auto-updated.

Last update: 2024-09-14 09:50:58 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

简化第三方API集成的包。使用此包,可以使API调用看起来像你代码的一部分。无需记住任何API的基础URL或路径。只需像这样调用:Integration::for('api-provider')->getSomethingCool()->json();

成为赞助商

您的支持使我能够免费、及时地维护此包。或者,您也可以 传播信息!

安装

composer require dot-env-it/laravel-api-integrator

运行 install 命令以发布配置文件和yaml文件

php artisan api-integrator:install

此命令将在项目根目录下创建 api-integrator.yaml 文件,并在 config 文件夹下创建 api-integrator.php 文件

示例 api-integrator.yaml 文件

integrations:
  github:
    url: 'https://api.github.com/'
    auth:
      type: Bearer
      value: !config 'api-integrator.token.github'
      name: 'Authorization'

  example:
    url: 'https://api.example.com'
    auth:
      type: Header
      token: !config 'api-integrator.token.example'
      name: 'X-API-KEY'

您可以使用 !config 标签将配置变量传递到yaml文件中

用法

use DotEnvIt\ApiIntegrator\Facades\Integration;

//api url https://api.github.com/foo
Integration::for('github')->get('foo')->json();

//api url https://api.example.com/foo
Integration::for('example')->get('foo')->json();

此包还为每种HTTP方法提供了一种魔法方法

use DotEnvIt\ApiIntegrator\Facades\Integration;

//api url https://api.example.com/foo
Integration::for('example')->getFoo()->json();

//api url https://api.example.com/foo with dynamic token
Integration::for('example')->withToken('new-token')->getFoo()->json();

//api url https://api.example.com/foo with dynamic header
Integration::for('example')->withHeader('X-CUSTOM-HEADER', 'CUSTOM')->withHeader('X-CUSTOM-HEADER-2', 'CUSTOM-2')->getFoo()->json();

//api url https://api.example.com/foo with headers array
Integration::for('example')->withHeaders(['X-CUSTOM-HEADER' => 'CUSTOM', 'X-CUSTOM-HEADER-2' => 'CUSTOM-2'])->getFoo()->json();

//api url https://api.example.com/foo/1
Integration::for('example')->getFoo_id(['id' => 1])->json();

//api url https://api.example.com/foo/1/bar/2
Integration::for('example')->getFoo_foo_id_bar_bar_id(['foo_id' => 1, 'bar_id' => 2])->json();

//api url https://api.example.com/foo/1?foo=bar&bar=baz
Integration::for('example')->getFoo_id(['id' => 1, 'foo' => 'bar', 'bar' => 'baz'])->json();

//POST api url https://api.example.com/foo/1/bar/2/baz
Integration::for('example')->postFoo_foo_id_bar_bar_id_baz(['foo_id' => 1, 'bar_id' => 2])->json();

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件发送给 jagdish.j.ptl@gmail.com,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件