From 4ad6a4202952ca00225e4634e50d3375e731e2b7 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 15 Apr 2013 23:33:11 +0200 Subject: [PATCH] Added basic auth params. --- lib/import/otrs.rb | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index 9852a34f8..dd6b61717 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -7,6 +7,12 @@ module Import::OTRS # response = Net::HTTP.get_response( URI.parse(url), { :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE } ) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) + + user = Setting.get('import_otrs_user'); + password = Setting.get('import_otrs_password'); + if user && user != '' && password && password != '' + http.basic_auth user, password + end if url =~ /https/i http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE @@ -15,12 +21,18 @@ module Import::OTRS begin response = http.request(request) # puts 'R:' + response.body.to_s - return response rescue Exception => e puts "can't get #{url}" puts e.inspect return end + if !response + raise "Can't connect to #{url}, got no response!" + end + if response.code.to_s != '200' + raise "Connection to #{url} failed, '#{response.code.to_s}'!" + end + return response end def self.post(base, data) url = Setting.get('import_otrs_endpoint') + '/' + base @@ -28,6 +40,13 @@ module Import::OTRS puts 'POST: ' + url uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) + + user = Setting.get('import_otrs_user'); + password = Setting.get('import_otrs_password'); + if user && user != '' && password && password != '' + http.basic_auth user, password + end + if url =~ /https/i http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE @@ -35,6 +54,13 @@ module Import::OTRS request = Net::HTTP::Post.new(uri.request_uri) request.set_form_data(data) response = http.request(request) + + if !response + raise "Can't connect to #{url}, got no response!" + end + if response.code.to_s != '200' + raise "Connection to #{url} failed, '#{response.code.to_s}'!" + end return response end