webparking/logic4-client

连接到 Logic4 的包


README

Header image

Packagist License Github stars Downloads

Logic4 客户端

请注意,此 API 客户端不是由 Logic4 创建或维护的。有关 Logic4 或其 API 的问题,请联系 Logic4 支持部门。

安装

您可以通过 composer 安装此包

composer require webparking/logic4-client

配置

为了使用 Logic4 API,您需要使用您的凭据配置 AccessTokenManager。您可以通过在 AccessTokenManager 实例上调用 configure 方法来完成此操作。

以下配置选项是必需的

  • public_key - 您 Logic4 账户的公钥
  • company_key - 您 Logic4 账户的公司密钥
  • username - 您 Logic4 账户的用户名
  • secret_key - 您 Logic4 账户的密钥
  • password - 您 Logic4 账户的密码
  • administration_id - 您 Logic4 账户的管理员 ID

当访问令牌过期时,它会自动刷新并存储在缓存中。缓存对象使用 AccessTokenManager 类上的 $cache 属性设置,并应实现 Psr\SimpleCache\CacheInterface 接口。

在配置 AccessTokenManager 之后,您可以在应用程序中使用任何请求类,无需担心访问令牌和其他中间件。

示例配置

# In this example, we load the configuration from a .env file.
$dotEnv = Dotenv\Dotenv::createImmutable(__DIR__);
$credentials = $dotEnv->load()

# We use the ArrayStore for testing purposes, but you can use any cache implementation that implements the Psr\SimpleCache\CacheInterface interface.
$cache = new \Illuminate\Cache\Repository(new \Illuminate\Cache\ArrayStore());
    
$tokenManager = new \Webparking\Logic4Client\AccessTokenManager($cache);
$tokenManager->configure($credentials);

# Initialize the ClientFactory using the AccessTokenManager
$clientFactory = new \Webparking\Logic4Client\ClientFactory($tokenManager);

# Initialize the request class using the ClientFactory
$request = new \Webparking\Logic4Client\Requests\ProductRequest($clientFactory);
$request->getProducts([
    'DebtorId' => 1,
]);

Laravel 集成

通过在 Laravel IoC 容器解析之后配置 AccessTokenManager,可以轻松地与 Laravel 集成。

将以下代码添加到您的 AppServiceProvider 类中

use Webparking\Logic4Client\AccessTokenManager;

$this->app
    ->afterResolving(AccessTokenManager::class, fn (AccessTokenManager $tokenManager) => $tokenManager->configure([
        'public_key' => config('services.logic4.public_key'),
        'company_key' => config('services.logic4.company_key'),
        'username' => config('services.logic4.username'),
        'secret_key' => config('services.logic4.secret_key'),
        'password' => config('services.logic4.password'),
        'administration_id' => config('services.logic4.administration_id'),
    ]));

这使得您可以在应用程序中直接使用任何请求类,而无需担心访问令牌和其他中间件。

示例请求

use Webparking\Logic4Client\Requests\ProductRequest;
use Webparking\Logic4Client\Data\Product;
use Illuminate\Support\Collection;

/** @return array<Product> */
public function index(ProductRequest $productRequest, int $debtorId): array
{
    $products = $productRequest->getProducts([
        'DebtorId' => $debtorId,
    ]);
    
    return iterator_to_array($products);
}

测试

您可以通过在项目根目录中运行 ./vendor/bin/phpunit 来测试此包。在运行测试之前,请确保通过运行 composer install 安装依赖项。

composer install
./vendor/bin/phpunit

防止散乱请求

如果您想确保通过客户端发送的所有请求在整个单个测试或完整的测试套件中都已模拟,可以在 \Webparking\Logic4Client\ClientFactory 类上调用 preventStrayRequests 方法。在调用此方法后,任何没有相应模拟的请求将引发异常,而不是执行实际请求

// turns on stray request prevention
\Webparking\Logic4Client\ClientFactory::preventStrayRequests();

// an exception is thrown if the request is not mocked
$request->getProducts(['DebtorId' => 1]);

// turns off stray request prevention
\Webparking\Logic4Client\ClientFactory::preventStrayRequests(false);

// no exception is thrown and will make the actual request
$request->getProducts(['DebtorId' => 1]);

贡献

请在问题页面上报告您发现的任何问题。拉取请求非常受欢迎。

生成端点

可以使用 generate 命令自动生成端点。此命令将为每个端点生成一个请求类,以及一个可用于实例化请求类的客户端类。

make generate

许可证

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