Skip to content

Commit

Permalink
Added backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahzs committed Dec 13, 2017
1 parent 3b59696 commit eab2319
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/se/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(key, **params)
@params = params
@quota = nil
@quota_used = 0
@backoff = Datetime.now
@logger_raw = Logger.new 'api_raw.log'
@logger_json = Logger.new 'api_json.log'
end
Expand All @@ -33,12 +34,20 @@ def posts(ids = "", **params)

def json(uri, **params)
throw "No site specified" if params[:site].nil?
backoff_for = ((@backoff - Datetime.now) * 24 * 60 * 60)
backoff_for = 0 if backoff_for <= 0
if backoff_for > 0
puts "Backing off for #{backoff_for}"
sleep backoff_for+2
puts "Finished backing off!"
end
params = @params.merge(params).merge({key: @key}).map { |k,v| "#{k}=#{v}" }.join('&')
puts "Posting to https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params}"
resp_raw = Net::HTTP.get_response(URI("https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params}")).body
@logger_raw.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp_raw}"
resp = JSON.parse(resp_raw)
@logger_raw.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp}"
@backoff = Datetime.now + (resp["backoff"].to_i/(24*60*60))
@logger_json.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp}"
@quota = resp["quota_remaining"]
@quota_used += 1
resp["items"]
Expand Down

0 comments on commit eab2319

Please sign in to comment.