Carl Edwards
Carl Edwards

Reputation: 14434

Error handling API calls via RestClient

I'm using rspotify to gather a list of data from albums names. All the while I've reached Spotify's api rate limit and would now like to create a few fallbacks to wait until the I can search and retry the search in order to not lose the (x) amount of data I've already retrieved.

The gem uses RestClient but the unfortunately when I reach the rate limit I don't get the amount of time needed to wait until I can make another call:

.rvm/gems/ruby-2.5.1/gems/rest-client 2.0.2/lib/restclient/abstract_response.rb:223:in 'exception_with_response': 429 Too Many Requests (RestClient::TooManyRequests)

The above is all I'm given. The begin/rescue statement below doesn't work as when the code fails, it fails entirely without retrying. What am I doing wrong here?

begin
  search = RSpotify::Album.search(album[:title])
rescue RestClient::ExceptionWithResponse, RestClient::TooManyRequests, Exception => e
  puts e
  retry
rescue e
  puts e
  retry
end

Here is how they suggest error handling:

https://github.com/rest-client/rest-client#response-callbacks-error-handling

Upvotes: 0

Views: 2002

Answers (1)

user2787635
user2787635

Reputation: 11

I was thinking of maybe throttling, so inside the exception to use RSpotify::authenticate("id", "token") with some of the multiple spotify accounts that I have and then retry

So something like that all put together

begin
   album = RSpotify::Album.find(track.first.album.id)
rescue RestClient::ExceptionWithResponse, RestClient::TooManyRequests, Exception => e            
   RSpotify::authenticate("id2", "token2")
   retry
end  

Upvotes: 1

Related Questions