webparking / logic4-client
连接到 Logic4 的包
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.8
- nesbot/carbon: ^2.71
- psr/simple-cache: ^3.0
Requires (Dev)
- cebe/php-openapi: ^1.7
- friendsofphp/php-cs-fixer: ^3.35
- mockery/mockery: ^1.6
- nette/php-generator: ^4.1
- orchestra/testbench: ^8.13
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.4
- rector/rector: ^1.1.1
- rregeer/phpunit-coverage-check: ^0.3.1
- symfony/var-dumper: ^6.3
- dev-master
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.0
- dev-dependabot/github_actions/peter-evans/create-pull-request-7
- dev-feature/update-endpoints
- dev-improvement/dto-per-api-version
- dev-feature/fix-github-job
- dev-generator-v2
- dev-feature/improve-pagination-support
- dev-bugfix/missing-request-parameters
This package is auto-updated.
Last update: 2024-09-09 11:15:54 UTC
README
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)。有关更多信息,请参阅许可证文件。