lib/octopi.rb in fcoury-octopi-0.0.7 vs lib/octopi.rb in fcoury-octopi-0.0.8

- old
+ new

@@ -63,10 +63,13 @@ CONTENT_TYPE = { 'yaml' => 'application/x-yaml', 'json' => 'application/json', 'xml' => 'application/sml' } + RETRYABLE_STATUS = [403] + MAX_RETRIES = 10 + base_uri "http://github.com/api/v2" attr_accessor :format, :login, :token, :trace_level, :read_only def initialize(login = nil, token = nil, format = "yaml") @@ -75,16 +78,21 @@ if login @login = login @token = token @read_only = false + self.class.default_params :login => login, :token => token end end - - %w[keys emails].each do |action| + + def read_only? + read_only + end + + {:keys => 'public_keys', :emails => 'emails'}.each_pair do |action, key| define_method("#{action}") do - get("/user/#{action}") + get("/user/#{action}")[key] end end def user user_data = get("/user/show/#{login}") @@ -119,10 +127,29 @@ def get_raw(path, params) get(path, params, 'plain') end + def get(path, params = {}, format = "yaml") + @@retries = 0 + begin + trace "GET [#{format}]", "/#{format}#{path}", params + submit(path, params, format) do |path, params, format| + self.class.get "/#{format}#{path}" + end + rescue RetryableAPIError => e + if @@retries < MAX_RETRIES + $stderr.puts e.message + @@retries += 1 + retry + else + raise APIError, "GitHub returned status #{e.code}, despite" + + " repeating the request #{MAX_RETRIES} times. Giving up." + end + end + end + def post(path, params = {}, format = "yaml") trace "POST", "/#{format}#{path}", params submit(path, params, format) do |path, params, format| resp = self.class.post "/#{format}#{path}", :query => params resp @@ -138,22 +165,27 @@ end end query = login ? { :login => login, :token => token } : {} query.merge!(params) + begin + resp = yield(path, query.merge(params), format) + rescue Net::HTTPBadResponse + raise RetryableAPIError + end + if @trace_level case @trace_level when "curl" query_trace = [] query.each_pair { |k,v| query_trace << "-F '#{k}=#{v}'" } puts "===== [curl version]" puts "curl #{query_trace.join(" ")} #{self.class.base_uri}/#{format}#{path}" puts "====================" end end - - resp = yield(path, query, format) + raise RetryableAPIError, resp.code.to_i if RETRYABLE_STATUS.include? resp.code.to_i raise APIError, "GitHub returned status #{resp.code}" unless resp.code.to_i == 200 # FIXME: This fails for showing raw Git data because that call returns # text/html as the content type. This issue has been reported. ctype = resp.headers['content-type'].first @@ -163,23 +195,16 @@ raise APIError, resp['error'].first['error'] end resp end - def get(path, params = {}, format = "yaml") - trace "GET [#{format}]", "/#{format}#{path}", params - submit(path, params, format) do |path, params, format| - self.class.get "/#{format}#{path}" - end - end - def trace(oper, url, params) return unless trace_level par_str = " params: " + params.map { |p| "#{p[0]}=#{p[1]}" }.join(", ") if params and !params.empty? puts "#{oper}: #{url}#{par_str}" end end - %w{error base resource user tag repository issue file_object blob commit}. + %w{error base resource user tag repository issue file_object blob key commit branch}. each{|f| require "#{File.dirname(__FILE__)}/octopi/#{f}"} end