gpt4 book ai didi

Magento 资源和 API 数据源

转载 作者:行者123 更新时间:2023-12-04 05:14:54 27 4
gpt4 key购买 nike

由于我经常使用第三方 API,我认为创建一些 Magento 模块以实现轻松连接和查询它们会很有帮助。理想情况下,您可以像这样查询 API...

$data = Mage::getModel( 'tools/apixyz_list' )->getCollection();

它将为其中一个列表项实例化一个模型,然后尝试通过查询 API 来获取它们的集合。这将需要在资源模型和 API 之间的配置中进行某种连接,这就是我遇到一些麻烦的地方。

有推荐的方法吗?我很难找到有关该主题的任何内容,但考虑到通常需要从项目到项目集成的 API 数量,我觉得这应该是一个非常普遍的问题。

最佳答案

是的!我实际上是为 Recurly 构建的——我试图让它开源,但它还没有开放。这是 load() 方法的一个片段,它是它的核心。

// TBT_Recurly_Model_Resource_Recurly_Abstract_Collection
public function load($printQuery = false, $logQuery = false)
{
if ($this->isLoaded()) {
return $this;
}

if ($this->_isCached()) {
return $this->_loadCache();
}

$this->_beforeLoad();
$this->_renderFilters()
->_renderOrders()
->_renderLimit();
$this->clear();

try {
# This is ultimately doing the API call
$recurly_list = $this->_getListSafe();
} catch (Recurly_Error $e) {
Mage::logException($e);
$this->setConnectionError($e->getMessage());
return $this;
}

foreach ($recurly_list as $recurly_item)
{
$item = $this->getNewEmptyItem();
$item->getResource()->setDataOnObject($item, $recurly_item);

// Recurly appears to sometimes return duplicate subscription items in it's list response.
if (!isset($this->_items[$item->getId()])) {
$this->addItem($item);
}
}
$this->_afterLoadRecurly();

// We have to setIsLoaded before we saveCache b/c otherwise it will infinite loop
$this->_setIsLoaded();
$this->_saveCache();
$this->_afterLoad();
return $this;
}

我们实际上最终采用了这个并将它放入一个基本的 REST 类中,这真的很酷,因为它最终很容易在它之上实现新的 REST API。

就最佳实践而言,我不确定我是否具体回答了您的问题。但基本上我认为要让它干净的主要事情是:
  • 按照 Magento 模型/集合方法签名进行查询。
  • 实现缓存
  • 在资源模型层实现API通信
  • 关于Magento 资源和 API 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14409138/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com