I'm using the Xero API to retrieve all Invoices in this month, then retrieving all CreditNotes, then I plan to allocate credits as appropriate.
Before doing that, I need to authorise all my Invoices and CreditNotes. When I get to the point of saving the updates, I'm hitting the Xeroizer::OAuth::RateLimitExceeded
exception, being thrown by the validation step which seems to be individually retrieving each record via #download_complete_record! at lib/xeroizer/record/record_association_helper.rb:131.
I've tried manually setting the complete_record_downloaded flag on all the objects before saving them but then I'm getting validation errors saying the invoices have no line items, which is expected.
This is what I'm trying to do, is there a different way I should be approaching this so it doesn't try to revalidate all the records before sending the updates?
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.
The relevant part of the backtrace from the exception is as follows:
.../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'
For reference, I've asked the same question on the Xeroizer gem issues page but had no response so asking here now.
I've come to a solution for this, new working code snippet below for reference.
This may be a bit hacky as it's setting the
complete_record_downloaded
flag on the object which doesn't quite match the state of the object so there could be side effects for other methods I'm not aware of, but for my purposes this appears to work as intended.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.