nabytek-forliving / api-client
Nábytek FORLIVING API 客户端
v1.0
2018-02-28 09:21 UTC
Requires
- php: >=5.6.0
- guzzlehttp/guzzle: ^6.3
- nette/caching: ^2.5
- nette/http: ^2.4
Requires (Dev)
- nette/bootstrap: ^2.4.3
- nette/robot-loader: ^2.4 || ^3.0
- nette/security: ^2.4
- nette/tester: ^2.0
- psr/log: dev-master
- tracy/tracy: ^2.4
This package is not auto-updated.
Last update: 2024-09-20 07:06:24 UTC
README
此库用于实现合作伙伴与 Forliving 的通信。
该库需要 PHP 5.6 或更高版本,并假定使用工具 Composer。
安装库
composer require nabytek-forliving/api-client
使用库
API 通过 \Forliving\Api\Client 对象上的方法调用。
对象通过以下两步创建
- 首先创建一个工厂,在其中指定临时文件的存储位置
$cacheDir = __DIR__; // složka s cache
$storage = new \Nette\Caching\Storages\FileStorage($cacheDir);
$clientFactory = new \Forliving\Api\ClientFactory($storage);
- 从工厂创建单个客户端
$client = $clientFactory->create($url, // URL instance
$username, // uživatelské jméno
$apiKey // API klíč
);
- URL 实例、用户名和 API 密钥可以从管理界面获取。
调用客户端方法时将调用 Forliving API。例如。
$product = $client->products->get($productId);
⋮
$products = $client->products->whereCodeInternal($code)->fetchAll();
⋮
foreach ($client->products->whereCodeInternal($code)->fetchAll() as $product) {
⋮
}
Nette 框架扩展
如果您使用 Nette 框架,引入方式更加简单。
将准备好的文件从 docs/config/config.api-client.extension.neon 复制到您的项目配置文件夹,并在主配置文件(app/config.neon)中添加它
includes:
- config.api-client.extension.neon
错误状态
调用 API 可能会发生多种错误。以下异常将被抛出
\Forliving\Api\Request\ConnectionException- 无法连接到 API,请重试请求
如果成功连接到 API,库可能会抛出以下错误(所有错误都是 Forliving\Api\Exception 类型)
\Forliving\Api\Request\InvalidCredentialsException- 无效的登录凭证\Forliving\Api\Request\NotFoundException- 不存在的对象\Forliving\Api\Request\ProductNotFoundException- 不存在的产品\Forliving\Api\Request\OrderNotFoundException- 不存在的订单
\Forliving\Api\Request\InvalidOrderStatusChangeException- 订单状态转换无效\Forliving\Api\Request\InvalidOrderCancelException- 无效的取消\Forliving\Api\Request\OtherRequestErrorException- 其他错误
对于这些错误,在再次尝试之前需要修复发送的请求。如果发送相同的请求,则可能会遇到相同的错误。
库中的所有异常都实现了 Forliving\Api\Exception 接口,如果您不需要以特殊方式响应每个错误状态,可以捕获此类型。