0

我正在使用 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 问题页面上提出了同样的问题,但没有任何回应,所以现在在这里提问。

4

1 回答 1

0

我已经为此找到了一个解决方案,下面是新的工作代码片段以供参考。

这可能有点麻烦,因为它在对象上设置的complete_record_downloaded标志与对象的状态不完全匹配,因此我不知道的其他方法可能会产生副作用,但就我的目的而言,这似乎可以作为故意的。

The find_in_batches method retrieves the line items along with the other necessary Invoice attributes for updating them successfully so that solves the issue I was having.

After reading the Xero API documentation again I found that sending an update to an Invoice requires listing at least all the Line Item IDs otherwise they'll be removed during the update, which explains why this step was necessary.

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
于 2019-11-25T23:04:07.567 回答