【问题标题】:Xeroizer RateLimitExceeded when using batch_save使用 batch_save 时 Xeroizer RateLimitExceeded
【发布时间】:2023-04-03 01:56:01
【问题描述】:

我正在使用 Xero API 检索本月的所有 Invoices,然后检索所有 CreditNotes,然后我计划适当分配 Credits。

在此之前,我需要授权我的所有发票和 CreditNotes。当我要保存更新时,我遇到了Xeroizer::OAuth::RateLimitExceeded 异常,该异常被验证步骤抛出,这似乎是通过 #download_complete_record! 单独检索每条记录lib/xeroizer/record/record_association_helper.rb:131.

我已尝试在所有对象上手动设置 complete_record_downloaded 标志,然后再保存它们,但随后我收到验证错误,提示发票没有行项目,这是预期的。

这就是我正在尝试做的事情,我应该以不同的方式来解决这个问题,这样它就不会在发送更新之前尝试重新验证所有记录?

require 'xeroizer'
key = Rails.application.secrets.xero[:key]
secret = Rails.application.secrets.xero[:secret]
cert = Rails.application.secrets.xero[:cert]
xeroizer = Xeroizer::PrivateApplication.new(key, secret, cert)

issue_date = Date.new(2019,11,22)
# Retrieving this broad selection of invoices here as it's used later in this worker for other purposes
xero_all_invoices = xeroizer.Invoice.all(where: "AmountDue>0")

xero_invoices = xero_all_invoices.select{|i| i.date == issue_date && i.status == 'DRAFT'}
xeroizer.Invoice.batch_save do
  xero_invoices.each{|i| i.status = 'AUTHORISED' }
end

# At this point, the Xeroizer::OAuth::RateLimitExceeded exception is thrown.

异常回溯的相关部分如下:

.../xeroizer/http.rb:175:in `handle_oauth_error!'
.../xeroizer/http.rb:124:in `http_request'
.../xeroizer/http.rb:31:in `http_get'
.../xeroizer/record/base_model.rb:152:in `find'
.../xeroizer/record/base.rb:100:in `download_complete_record!'
.../xeroizer/record/record_association_helper.rb:131:in `block in define_association_attribute'
.../xeroizer/record/base.rb:54:in `[]'
.../xeroizer/record/validators/associated_validator.rb:12:in `valid?'
.../xeroizer/record/validators/validator.rb:15:in `validate'
.../xeroizer/record/validation_helper.rb:59:in `block in valid?'
.../xeroizer/record/validation_helper.rb:58:in `each'
.../xeroizer/record/validation_helper.rb:58:in `valid?'
.../xeroizer/record/base_model.rb:161:in `each'
.../xeroizer/record/base_model.rb:161:in `all?'
.../xeroizer/record/base_model.rb:161:in `save_records'

作为参考,我已经在 Xeroizer gem 问题页面上询问了same question,但没有任何回应,所以现在在这里询问。

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。请参阅:How to create a Minimal, Reproducible Example。您的代码示例没有运行。请提供一个实际可以运行的版本,不要让我们编写一个工具。帮助我们帮助您。
  • 我已经用所需的 gem 和 API 代码的设置更新了代码 sn-p,但是您仍然无法运行它,因为它需要注册到 Xero API,并拥有一个 Xero 帐户。这不是我要问为什么它不起作用的情况。我知道它为什么不起作用,我不知道如何在 Xeroizer gem 的范围内解决它。我想找到一种方法来防止 Xeroizer gem 必须重新验证它已经下载的所有记录,我只是不确定如何做到这一点而不会造成无法预料的副作用。
  • 将该信息添加到问题中,将其合并到您最初应该放置的位置。不要使用“已编辑”或“已更新”标签,因为我们可以在必要时查看更改的内容。尝试调试我们无法运行的代码会很难为您提供帮助,并且可能会被视为离题。

标签: ruby xeroizer


【解决方案1】:

我已经为此找到了解决方案,下面的新工作代码 sn-p 供参考。

这可能有点棘手,因为它在对象上设置了 complete_record_downloaded 标志,该标志与对象的状态不太匹配,因此我不知道的其他方法可能会产生副作用,但对于我的这似乎按预期工作。

find_in_batches 方法检索行项目以及其他必要的 Invoice 属性以成功更新它们,从而解决我遇到的问题。

再次阅读 Xero API 文档后,我发现向发票发送更新需要至少列出所有行项目 ID,否则它们将在更新期间被删除,这解释了为什么需要此步骤。

issue_date = Date.new(2019,11,22)
# Retrieving this broad selection of invoices here as it's used later in this worker for other purposes
xero_all_invoices = []
xeroizer.Invoice.find_in_batches(where: "AmountDue>0") do |batch|
  batch.each do |invoice|
    invoice.complete_record_downloaded = true
    xero_all_invoices << invoice
  end
end

xero_invoices = xero_all_invoices.select{|i| i.date == issue_date && i.status == 'DRAFT'}
xeroizer.Invoice.batch_save do
  xero_invoices.each{|i| i.status = 'AUTHORISED' }
end
# Save successful

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2016-11-14
    • 2019-11-01
    • 2018-02-14
    • 2014-11-25
    • 2021-05-14
    • 2013-06-20
    相关资源
    最近更新 更多