From b52d1efe7872435733e95f9dcd92058623404e24 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 18 Jul 2011 16:55:10 +0200 Subject: [PATCH] get rid of multiple get_from_api definitions, put it in application_controller and make it more generic --- app/controllers/application_controller.rb | 34 +++++++++++++++++++++++++++++++ app/controllers/download_controller.rb | 21 ++----------------- app/controllers/main_controller.rb | 12 ----------- config/environments/development.rb | 4 ++++ 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7242844..d7ee9be 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,8 @@ # Filters added to this controller apply to all controllers in the application. # Likewise, all the methods added will be available for all controllers. +require 'net/https' + class ApplicationController < ActionController::Base before_filter :set_language @@ -97,5 +99,37 @@ class ApplicationController < ActionController::Base render({:content_type => :js, :text => response}.merge(options)) end + private + + def get_from_api(path) + host, port = API_HOST.split(/:/) + if defined? API_SSL and API_SSL + port ||= 443 + else + port ||= 80 + end + begin + http = Net::HTTP.new(host, port) + if defined? API_SSL and API_SSL + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + request = Net::HTTP::Get.new(path) + request['x-username'] = ICHAIN_USER if defined? ICHAIN_USER + request['x-password'] = ICHAIN_PASS if defined? ICHAIN_PASS + request.basic_auth BA_USER, BA_PASS if defined? BA_USER and defined? BA_PASS + http.read_timeout = 15 + response = http.request(request) + case response + when Net::HTTPSuccess + response + else + raise "Response was: #{response} #{response.body}" + end + rescue Exception => e + logger.error "Error connecting to #{host}:#{port}: #{e.to_s}" + return nil + end + end end diff --git a/app/controllers/download_controller.rb b/app/controllers/download_controller.rb index 1a35e8a..acd4e2a 100644 --- a/app/controllers/download_controller.rb +++ b/app/controllers/download_controller.rb @@ -9,15 +9,15 @@ class DownloadController < ApplicationController api_result = get_from_api("/search/published/binary/id?match=project='#{@prj}'+and+package='#{@pkg}'") if api_result + doc = REXML::Document.new api_result.body @data = Hash.new - api_result.elements.each("/collection/binary") { |e| + doc.elements.each("/collection/binary") { |e| distro = e.attributes['repository'] if not @data.has_key?(distro) @data[distro] = { :repo => "http://download.opensuse.org/repositories/#{@prj}/#{distro}/", :pkg => Hash.new } - puts e.attributes['baseproject'] case e.attributes['baseproject'] when /^(DISCONTINUED:)?openSUSE:/ @data[distro][:flavor] = 'openSUSE' @@ -72,21 +72,4 @@ class DownloadController < ApplicationController render_json @data.to_json end - private - - def get_from_api(path) - begin - req = Net::HTTP::Get.new(path) - req['x-username'] = ICHAIN_USER - host, port = API_HOST.split(/:/) - port ||= 80 - res = Net::HTTP.new(host, port).start do |http| - http.request(req) - end - doc = REXML::Document.new res.body - rescue - nil - end - end - end diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index 6e6043a..3d88858 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -205,16 +205,4 @@ class MainController < ApplicationController end end - private - - def get_from_api(path) - req = Net::HTTP::Get.new(path) - req['x-username'] = "obs_read_only" - - host, port = API_HOST.split(/:/) - port ||= 80 - res = Net::HTTP.new(host, port).start do |http| - http.request(req) - end - end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 7bf1ba2..b5ed365 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -19,3 +19,7 @@ config.action_mailer.raise_delivery_errors = false API_HOST = "api-internal.opensuse.org" ICHAIN_USER = "obs_read_only" +# API_HOST = "api.opensuse.org" +# API_SSL = true +# BA_USER = "username" +# BA_PASS = "password" -- 2.1.4