Skip to content

Commit

Permalink
Fixed backoff handling
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahzs committed Dec 15, 2017
1 parent 0d70823 commit 38965ed
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/se/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "net/http"
require "json"
require "uri"
require "time"

module SE
module API
Expand All @@ -20,7 +21,7 @@ def initialize(key, **params)
@params = params.merge({filter: '!*1_).BnZb8pdvWlZpJYNyauMekouxK9-RzUNUrwiB'})
@quota = nil
@quota_used = 0
@backoff = DateTime.now
@backoff = Time.now
@logger_raw = Logger.new 'api_raw.log'
@logger_json = Logger.new 'api_json.log'
end
Expand Down Expand Up @@ -50,7 +51,7 @@ def answers(ids = "", **params)

def json(uri, **params)
throw "No site specified" if params[:site].nil?
backoff_for = ((@backoff - DateTime.now) * 24 * 60 * 60)
backoff_for = @backoff-Time.now
backoff_for = 0 if backoff_for <= 0
if backoff_for > 0
puts "Backing off for #{backoff_for}"
Expand All @@ -67,7 +68,7 @@ def json(uri, **params)
end
@logger_raw.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp_raw}"
resp = JSON.parse(resp_raw)
@backoff = DateTime.now + (resp["backoff"].to_i/(24*60*60))
@backoff = Time.now + resp["backoff"].to_i
@logger_json.info "https://api.stackexchange.com/#{API_VERSION}/#{uri}?#{params} => #{resp}"
@quota = resp["quota_remaining"]
@quota_used += 1
Expand Down

0 comments on commit 38965ed

Please sign in to comment.