Commit f92a9c5a authored by Douwe Maan's avatar Douwe Maan

Refactor: Remove #tap for readability and performance and DRY up get/post.

parent f11e1bf9
...@@ -64,63 +64,61 @@ class GitlabNet ...@@ -64,63 +64,61 @@ class GitlabNet
"#{config.gitlab_url}/api/v3/internal" "#{config.gitlab_url}/api/v3/internal"
end end
def http_client_for(url) def http_client_for(uri)
Net::HTTP.new(url.host, url.port).tap do |http| http = Net::HTTP.new(uri.host, uri.port)
if URI::HTTPS === url
http.use_ssl = true if uri.is_a?(URI::HTTPS)
http.cert_store = cert_store http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if config.http_settings['self_signed_cert'] http.cert_store = cert_store
end http.verify_mode = OpenSSL::SSL::VERIFY_NONE if config.http_settings['self_signed_cert']
end end
http
end end
def http_request_for(url, method = :get) def http_request_for(method, uri, params = {})
request_klass = method == :get ? Net::HTTP::Get : Net::HTTP::Post
request = request_klass.new(uri.request_uri)
user = config.http_settings['user'] user = config.http_settings['user']
password = config.http_settings['password'] password = config.http_settings['password']
request.basic_auth(user, password) if user && password
request.set_form_data(params.merge(secret_token: secret_token))
request
end
def request(method, url, params = {})
$logger.debug "Performing #{method.to_s.upcase} #{url}"
uri = URI.parse(url)
http = http_client_for(uri)
request = http_request_for(method, uri, params)
if method == :get response = http.start { http.request(request) }
Net::HTTP::Get.new(url.request_uri).tap { |r| r.basic_auth(user, password) if user && password }
if response.code == "200"
$logger.debug "Received response #{response.code} => <#{response.body}>."
else else
Net::HTTP::Post.new(url.request_uri).tap { |r| r.basic_auth(user, password) if user && password } $logger.error "API call <#{method.to_s.upcase} #{url}> failed: #{response.code} => <#{response.body}>."
end end
response
end end
def get(url) def get(url)
$logger.debug "Performing GET #{url}" request(:get, url)
url = URI.parse(url)
http = http_client_for url
request = http_request_for url
request.set_form_data(secret_token: secret_token)
http.start { |http| http.request(request) }.tap do |resp|
if resp.code == "200"
$logger.debug { "Received response #{resp.code} => <#{resp.body}>." }
else
$logger.error { "API call <GET #{url}> failed: #{resp.code} => <#{resp.body}>." }
end
end
end end
def post(url, params) def post(url, params)
$logger.debug "Performing POST #{url}" request(:post, url, params)
url = URI.parse(url)
http = http_client_for(url)
request = http_request_for(url, :post)
request.set_form_data(params.merge(secret_token: secret_token))
http.start { |http| http.request(request) }.tap do |resp|
if resp.code == "200"
$logger.debug { "Received response #{resp.code} => <#{resp.body}>." }
else
$logger.error { "API call <POST #{url}> failed: #{resp.code} => <#{resp.body}>." }
end
end
end end
def cert_store def cert_store
@cert_store ||= OpenSSL::X509::Store.new.tap do |store| @cert_store ||= begin
store = OpenSSL::X509::Store.new
store.set_default_paths store.set_default_paths
if ca_file = config.http_settings['ca_file'] if ca_file = config.http_settings['ca_file']
...@@ -130,6 +128,8 @@ class GitlabNet ...@@ -130,6 +128,8 @@ class GitlabNet
if ca_path = config.http_settings['ca_path'] if ca_path = config.http_settings['ca_path']
store.add_path(ca_path) store.add_path(ca_path)
end end
store
end end
end end
......
...@@ -139,12 +139,13 @@ describe GitlabNet, vcr: true do ...@@ -139,12 +139,13 @@ describe GitlabNet, vcr: true do
let(:user) { 'user' } let(:user) { 'user' }
let(:password) { 'password' } let(:password) { 'password' }
let(:url) { URI 'http://localhost/' } let(:url) { URI 'http://localhost/' }
subject { gitlab_net.send :http_request_for, url } subject { gitlab_net.send :http_request_for, :get, url }
before do before do
gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user } gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user }
gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password } gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password }
get.should_receive(:basic_auth).with(user, password).once get.should_receive(:basic_auth).with(user, password).once
get.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')).once
end end
it { should_not be_nil } it { should_not be_nil }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment