From 607a679a81e4407e3492a4c0d6284270f0472acf Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 14 Sep 2015 22:15:01 -0700 Subject: [PATCH] Add http request method that takes user/password combo --- lib/vmfloaty/auth.rb | 10 +++++----- lib/vmfloaty/http.rb | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 3e7ebde..3bbc94c 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -4,7 +4,7 @@ require 'vmfloaty/http' class Auth def self.get_token(verbose, url, user, password) - conn = Http.get_conn(verbose, url) + conn = Http.get_conn(verbose, url, user, password) resp = conn.post do |req| req.url '/token' @@ -15,26 +15,26 @@ class Auth resp_body end - def self.delete_token(verbose, url, user, pass, token) + def self.delete_token(verbose, url, user, password, token) if token.nil? puts 'You did not provide a token' return end - conn = Http.get_conn(verbose, url) + conn = Http.get_conn(verbose, url, user, password) response = conn.delete "/token/#{token}" res_body = JSON.parse(response) puts res_body end - def self.token_status(verbose, url, user, pass, token) + def self.token_status(verbose, url, user, password, token) if token.nil? puts 'You did not provide a token' return end - conn = Http.get_conn(verbose, url) + conn = Http.get_conn(verbose, url, user, password) response = conn.get "/token/#{token}" res_body = JSON.parse(response.body) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index 22fb874..47f892a 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -15,4 +15,25 @@ class Http return conn end + + def self.get_conn(verbose, url, user, password) + if url.nil? + STDERR.puts "The url you provided was empty" + exit 1 + end + + if user.nil? + STDERR.puts "You did not provide a user to authenticate with" + exit 1 + end + + conn = Faraday.new(:url => url) do |faraday| + faraday.request :url_encoded + faraday.request :basic_auth, user, password + faraday.response :logger if verbose + faraday.adapter Faraday.default_adapter + end + + return conn + end end