elyngved / ruby-mws

A Ruby library that wraps the Amazon MWS API.
MIT License
41 stars 64 forks source link

next fails after multiple list_order_items #1

Closed rlad closed 12 years ago

rlad commented 12 years ago

I get an error when trying to do a mws.orders.next : mwstestcase.rb:73:in <main>': undefined methodhas_key?' for nil:NilClass (NoMethodError)

and if I save the response.next_token and try mws.orders.list_orders_by_next_response, I get another error: mwstestcase.rb:70:in <main>': undefined methodlist_orders_by_next_response' for #MWS::API::Order:8366400 (NoMethodError)

The problem is duplicated if I do multiple mws.orders.list_order_items between the mws.orders.list_orders and the mws.orders.next.

Here is the test case for response.next_token:


require 'rubygems'
require 'ruby-mws'
require 'active_record'
require 'active_support/all'

def get_response # takes a block as a parameter
  #amazon requests may be throttled, so we need to check before returning results
  throttled = true
  while throttled == true 
    the_response = yield                        
    if the_response.has_key?("error_response")                  # all error responses should have this key
      if the_response.error_response.error.has_key?("code")         
        if the_response.error_response.error.code == "RequestThrottled"
          sleep(10.seconds)                     # throttle times may vary, so retry after 10 sec
        end
      else
        puts "unknown error"
        return nil 
      end
    else
      throttled = false
      return the_response
    end
  end
end

mws = MWS.new ({:aws_access_key_id => "",
  :secret_access_key => "++",
  :seller_id => "",
  :marketplace_id => ""})

start_time = Time.utc(2003)                                         # default start date

response = get_response {mws.orders.list_orders :last_updated_after => start_time}                        

n = 0
while n < 3 do 

  if mws.orders.has_next? == true
    puts "has next"
    next_token = response.next_token
  else
    puts "does not have next"
  end

  response.orders.each do |r|
    order_items = get_response {mws.orders.list_order_items :amazon_order_id => r.amazon_order_id}
      if order_items
        order_items.order_items.each do |item|
        puts r.amazon_order_id, item.title
      end 
    end
  end

puts "trying next_token!"  
response = get_response {mws.orders.list_orders_by_next_response :next_token => next_token}                       

n += 1
end
elyngved commented 12 years ago

So sorry, this is due to a misprint in the documentation. That method should be 'list_orders_by_next_token'. I will make the change in the docs. Thanks for reporting

Any and all bug reports or feature requests are welcome :)

elyngved commented 12 years ago

Closing this issue.. let me know if you run into more problems