moldersmedia / lightspeedapi
为您的平台和Lightspeed API提供连接
1.1.14
2019-11-11 13:55 UTC
Requires
- php: >=7.1.0
- guzzlehttp/guzzle: 6.*
README
本包是对官方Lightspeed API(也称为seoshop API)的重新构建。官方API功能相当有限。本包与官方包的区别如下
- 当请求过多时,使请求失败时可以暂停请求
- 无需手动重复计算即可计算资源项
- 对于API调用限制达到和SleepTime差异(包括HTTP响应)抛出不同的异常
- 更清晰的错误信息
- 将资源类从WebshopappApiClient中分离出来
要求
- PHP 7.1或更高版本(在composer.json中要求)
- Guzzle HTTP客户端6.*或更高版本(在composer.json中要求)
安装
您只需执行composer require moldersmedia/lightspeedapi
官方Lightspeed API兼容性
API类的基类是官方API。用本包替换Lightspeed包不会发生冲突(除非您扩展了官方类)。本包是独立的,不扩展任何内容。
使用ApiAclient
您可以通过调用类MoldersMedia\LightspeedApi\Classes\Api\ApiClient
来构建客户端。第一个参数是$apiServer
,第二个参数是$apiKey
,然后是$apiSecret
和$language
。最后一个参数是配置,它只接受一个数组。数组可以接受以下数组键及其值
$config = [
'max_sleep_time' => 5, // The maximum time that the sleep handler should wait. If the sleep time is
// smaller then the next available request, an exception will be thrown
'extra_sleep_time' => 'extraSleepTime', // This time will be added on the calculated sleep time.
// Sleep time formula is (next available request time + extra_sleep_time). Max sleep time has nothing to do with this config
// Below is the same Lightspeed API config as in the constructor and official Lightspeed API
'cluster' => 'apiServer',
'language' => 'apiLanguage',
'user_secret' => 'apiSecret',
'api_key' => 'apiKey'
];
异常处理
有两个异常会返回请求数据:ApiClientException
和ApiLimitReachedException
。将给出清晰的错误信息。对于一些更具体的数据,您可以使用以下函数
try {
$api = ( new MoldersMedia\LightspeedApi\Classes\Api\ApiClient( 'eu', 'your_api_key', 'your_user_secret', 'nl' ) );
$api->products->count();
} catch( MoldersMedia\LightspeedApi\Classes\Exceptions\General\ApiLimitReachedException $exception ) {
$exception->getResource(); // Return the resource that thrown the exception if available. In this example 'products'
$exception->getWaitTime(); // Time that the next API call can be made
$exception->getMessage(); // Gives the error message
$exception->getPayload(); // The GET or POST parameters that are given
}
待办事项
- 将资源名称添加到所有资源类中
- 缓存一些不太经常改变的需求(例如 $api->languages->get())
- 添加一个错误日志的好方法。当使用队列工作者时非常有用
- 文档化代码
- 添加数据格式化器,以便轻松更改响应数组的键
- 计算每小时和每日速率
贡献
目前没有代码规范。
许可证
本包可免费使用。不允许出售代码。