fastruby / harvesting

Ruby wrapper for the Harvest API v2
MIT License
28 stars 30 forks source link

Add API throttling support #57

Closed edudepetris closed 4 years ago

edudepetris commented 4 years ago

What should this PR do?

Solve issue #56

Solution

Introduces a new ExceedRateLimit exception, and raise it when the response status code is 429.

Notice that running this script it will return the new execption.

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem 'concurrent-ruby', require: 'concurrent'
  gem 'harvesting', github: 'edudepetris/harvesting', branch: 'ed/429-throttled'
end

client = Harvesting::Client.new(
  access_token: <your-token-here>,
  account_id: <your-account-id>
)

promises = (1..200).map do
  Concurrent::Promise.execute do
    client.me
  end
end

Concurrent::Promise.zip(*promises).value!