targito/targito-api-bundle

Targito API 的 Symfony 实现

安装数量: 2,689

依赖关系: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

公开问题: 0

类型:symfony-bundle

v1.2.0 2021-05-07 14:35 UTC

This package is auto-updated.

Last update: 2024-09-07 22:11:27 UTC


README

Build Status

要查看 API 方法的描述,请参阅库描述

安装

composer require targito/targito-api-bundle

如果你使用 Symfony Flex,则捆绑包应自动启用。如果你没有使用它,请在 config/bundles.php 文件中添加 Targito\Bundle\Api\TargitoApiBundle

<?php

return [
    // ...
    Targito\Bundle\Api\TargitoApiBundle::class => ['all' => true],
];

如果你想要更改任何默认设置,在你的 config/packages 目录中创建一个名为 targito_api.yaml 的文件,并使用键 targito_api

注意:你可以像这样让 Symfony 为你生成默认配置:./bin/console config:dump targito_api > config/packages/targito_api.yaml

targito_api:
  # your config goes here

使用方法

简单地使用提供的服务(支持自动注入)

  • Targito\Api\TargitoApi
  • Targito\Api\Endpoint\TargitoContactEndpoint
  • Targito\Api\Endpoint\TargitoTransactEndpoint

示例

获取整个 API 服务

<?php

use Targito\Api\TargitoApi;

class MyService
{
    public function __construct(TargitoApi $targitoApi)
    {
        $targitoApi->contacts()->addContact([
            //...
        ]);
        $targitoApi->transact()->sendEmail([
            //...
        ]);
    }
}

获取特定的 API 模块

<?php

use Targito\Api\Endpoint\TargitoContactEndpoint;
use Targito\Api\Endpoint\TargitoTransactEndpoint;

class MyService
{
    public function __construct(TargitoContactEndpoint $contactEndpoint, TargitoTransactEndpoint $transactEndpoint)
    {
        $contactEndpoint->addContact([]);
        $transactEndpoint->sendEmail([]);
    }
}

配置凭证

默认情况下,该捆绑包从环境变量 TARGITO_ACCOUNT_IDTARGITO_API_PASSWORD 中获取凭证。

如果你想以其他方式提供凭证,请继续阅读,否则可以跳过本节。

有两种内置选项用于指定凭证:通过环境变量或显式指定。你也可以指定自己的凭证服务。

显式

在显式模式下,凭证必须在配置中指定。

targito_api:
    credentials:
      type: explicit
      account_id: my-account-id
      api_password: my-api-password

环境变量

在使用环境变量时,你可以选择性地指定环境变量名称。

targito_api:
    credentials:
      type: environment # the type key can also be omitted as 'environment' is the default value
      account_id_env_name: MY_ACCOUNT_ID_ENV_VAR # optional, default is TARGITO_ACCOUNT_ID
      api_password_env_name: MY_API_PASSWORD_ENV_VAR # optional, default is TARGITO_API_PASSWORD

自定义服务

你可以指定实现 Targito\Api\Credentials\CredentialsInterface 接口的自定义服务。

targito_api:
    credentials:
      type: App\MyCredentialsService

配置 HTTP 请求服务

默认情况下,此捆绑包(以及底层库)自动在 curl 和 stream 实现之间进行选择。你可以提供自己的(或使用 curl/stream 库,无论 PHP 级别是否支持 curl)。服务必须实现 Targito\Api\Http\HttpRequestInterface

targito_api:
    http_request: targito_api.http.curl

targito_api:
    http_request: targito_api.http.stream

targito_api:
    http_request: App\MyCustomHttpRequest

完整的配置参考(自动生成)

# Default configuration for extension with alias: "targito_api"
targito_api:
    credentials:

        # Can be 'environment', 'explicit' or any service implementing Targito\Api\Credentials\CredentialsInterface
        type:                 environment

        # The account id that is used when type is 'explicit'
        account_id:           null

        # The api password that is used when type is 'explicit'
        api_password:         null

        # The account id environment variable name when type is 'environment'
        account_id_env_name:  TARGITO_ACCOUNT_ID

        # The api password environment variable name when type is 'environment'
        api_password_env_name: TARGITO_API_PASSWORD

    # The service that will be used as the http request (must implement Targito\Api\Http\HttpRequestInterface).
    # Defaults to null which means autodetect between default curl and stream based implementations.
    http_request:         null

    # The api url (including version) to issue requests to. Can be null which means to use default.
    api_url:              null