Skip to content

Commit

Permalink
Merge branch 'master' into add_logs
Browse files Browse the repository at this point in the history
  • Loading branch information
walteraa committed Jun 6, 2017
2 parents 5d4bef8 + 419def1 commit 4064c93
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
8 changes: 6 additions & 2 deletions lib/utils/logger.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
require 'logger'

require 'fileutils'
module XClarityClient

class XClarityLogger

def initialize(global_log=nil)
# This block below looks for a log in the project which uses
# this client or a defined log coming from initialize param.
# If none of these logs exists, is created a log by default.
@log = $lenovo_log ||= $log ||= global_log

if not @log
FileUtils::mkdir_p 'logs'
file = File.open('logs/lxca_client.log', 'a+')
file.sync = true
@log = Logger.new file
Expand All @@ -22,7 +26,7 @@ def info(header, msg)

def error(header, msg)
@log.level = Logger::ERROR
@log.error "[#{header} - #{msg}]"
@log.error "[#{header}] - #{msg}"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/xclarity_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'utils/logger'

module XClarityClient
$log = (XClarityClient::XClarityLogger.new).freeze
$lxca_log = XClarityClient::XClarityLogger.new
end

require 'xclarity_client/configuration'
Expand Down
25 changes: 18 additions & 7 deletions lib/xclarity_client/xclarity_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(conf, uri)
end

def connection_builder(conf, uri)
$log.info "XClarityClient::XClarityBase connection_builder", "Creating connection to #{conf.host + uri}"
$lxca_log.info "XClarityClient::XClarityBase connection_builder", "Creating connection to #{conf.host + uri}"
#Building configuration
@conn = Faraday.new(url: conf.host + uri) do |faraday|
faraday.request :url_encoded # form-encode POST params
Expand All @@ -26,22 +26,33 @@ def connection_builder(conf, uri)
response = authentication(conf) unless conf.auth_type != 'token'
#TODO: What's to do with the response of authentication request?
@conn.basic_auth(conf.username, conf.password) if conf.auth_type == 'basic_auth'
$log.info "XClarityClient::XclarityBase connection_builder", "Connection created Successfuly"
$lxca_log.info "XClarityClient::XclarityBase connection_builder", "Connection created Successfuly"
@conn
end

private

def connection(uri = "", opts = {})
query = opts.size > 0 ? "?" + opts.map {|k, v| "#{k}=#{v}"}.join(",") : ""
@conn.get(uri + query)
begin
@conn.get(uri + query)
rescue Faraday::Error::ConnectionFailed => e
$lxca_log.error "XClarityClient::XclarityBase connection", "Error trying to send a GET to #{uri + query}"
Faraday::Response.new
end
end

def do_put (uri="", request = {})
@conn.put do |req|
req.url uri
req.headers['Content-Type'] = 'application/json'
req.body = request
begin
@conn.put do |req|
req.url uri
req.headers['Content-Type'] = 'application/json'
req.body = request
end
rescue Faraday::Error::ConnectionFailed => e
$lxca_log.error "XClarityClient::XclarityBase do_put", "Error trying to send a PUT to #{uri}"
$lxca_log.error "XClarityClient::XclarityBase do_put", "Request sent: #{request}"
Faraday::Response.new
end
end

Expand Down
Empty file removed logs/lxca_client.log
Empty file.

0 comments on commit 4064c93

Please sign in to comment.