Sprinkle of logging

This commit is contained in:
Steven Bazyl 2013-01-04 16:14:14 -08:00
parent 1601c616fe
commit 1d7315ee9b
4 changed files with 29 additions and 15 deletions

View File

@ -37,7 +37,7 @@ module Google
## ##
# This class manages APIs communication. # This class manages APIs communication.
class APIClient class APIClient
include Google::Logging include Google::APIClient::Logging
## ##
# Creates a new Google API client. # Creates a new Google API client.
@ -71,6 +71,8 @@ module Google
# @option options [String] :discovery_path ("/discovery/v1") # @option options [String] :discovery_path ("/discovery/v1")
# The discovery base path. This rarely needs to be changed. # The discovery base path. This rarely needs to be changed.
def initialize(options={}) def initialize(options={})
logger.debug { "#{self.class} - Initializing client with options #{options}" }
# Normalize key to String to allow indifferent access. # Normalize key to String to allow indifferent access.
options = options.inject({}) do |accu, (key, value)| options = options.inject({}) do |accu, (key, value)|
accu[key.to_sym] = value accu[key.to_sym] = value
@ -88,7 +90,7 @@ module Google
app_version = options[:application_version] app_version = options[:application_version]
application_string = "#{app_name}/#{app_version || '0.0.0'}" application_string = "#{app_name}/#{app_version || '0.0.0'}"
else else
logger.warn("Please provide :application_name and :application_version when initializing the APIClient") logger.warn { "#{self.class} - Please provide :application_name and :application_version when initializing the client" }
end end
self.user_agent = options[:user_agent] || ( self.user_agent = options[:user_agent] || (
"#{application_string} " + "#{application_string} " +
@ -105,6 +107,7 @@ module Google
@discovery_uris = {} @discovery_uris = {}
@discovery_documents = {} @discovery_documents = {}
@discovered_apis = {} @discovered_apis = {}
return self return self
end end
@ -560,6 +563,7 @@ module Google
result = request.send(connection) result = request.send(connection)
if result.status == 401 && authorization.respond_to?(:refresh_token) && auto_refresh_token if result.status == 401 && authorization.respond_to?(:refresh_token) && auto_refresh_token
begin begin
logger.debug("Attempting refresh of access token & retry of request")
authorization.fetch_access_token! authorization.fetch_access_token!
result = request.send(connection) result = request.send(connection)
rescue Signet::AuthorizationError rescue Signet::AuthorizationError

View File

@ -13,18 +13,20 @@ module Google
self.logger = Logger.new(STDOUT) self.logger = Logger.new(STDOUT)
self.logger.level = Logger::WARN self.logger.level = Logger::WARN
##
# Module to make accessing the logger simpler
module Logging
##
# Logger for the API client
#
# @return [Logger] logger instance.
def logger
Google::APIClient.logger
end
end
end end
##
# Module to make accessing the logger simpler
module Logging
##
# Logger for the API client
#
# @return [Logger] logger instance.
def logger
Google::APIClient.logger
end
end
end end

View File

@ -19,6 +19,7 @@ require 'compat/multi_json'
require 'addressable/uri' require 'addressable/uri'
require 'stringio' require 'stringio'
require 'google/api_client/discovery' require 'google/api_client/discovery'
require 'google/api_client/logging'
module Google module Google
class APIClient class APIClient
@ -26,6 +27,8 @@ module Google
## ##
# Represents an API request. # Represents an API request.
class Request class Request
include Google::APIClient::Logging
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
# @return [Hash] Request parameters # @return [Hash] Request parameters
@ -151,14 +154,19 @@ module Google
# @return [Google::APIClient::Result] # @return [Google::APIClient::Result]
# result of API request # result of API request
def send(connection) def send(connection)
http_response = connection.app.call(self.to_env(connection)) env = self.to_env(connection)
logger.debug { "#{self.class} Sending API request #{env[:method]} #{env[:url].to_s} #{env[:request_headers]}" }
http_response = connection.app.call(env)
result = self.process_http_response(http_response) result = self.process_http_response(http_response)
logger.debug { "#{self.class} Result: #{result.status} #{result.headers}" }
# Resumamble slightly different than other upload protocols in that it requires at least # Resumamble slightly different than other upload protocols in that it requires at least
# 2 requests. # 2 requests.
if self.upload_type == 'resumable' if self.upload_type == 'resumable'
upload = result.resumable_upload upload = result.resumable_upload
unless upload.complete? unless upload.complete?
logger.debug { "#{self.class} Sending upload body" }
result = upload.send(connection) result = upload.send(connection)
end end
end end

View File

@ -21,7 +21,7 @@ if !defined?(::Google::APIClient::VERSION)
class APIClient class APIClient
module VERSION module VERSION
MAJOR = 0 MAJOR = 0
MINOR = 5 MINOR = 6
TINY = 0 TINY = 0
STRING = [MAJOR, MINOR, TINY].join('.') STRING = [MAJOR, MINOR, TINY].join('.')
end end