Skip to content

Commit

Permalink
Sprinkle of logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sqrrrl committed Jan 5, 2013
1 parent 1601c61 commit 1d7315e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
8 changes: 6 additions & 2 deletions lib/google/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Google
##
# This class manages APIs communication.
class APIClient
include Google::Logging
include Google::APIClient::Logging

##
# Creates a new Google API client.
Expand Down Expand Up @@ -71,6 +71,8 @@ class APIClient
# @option options [String] :discovery_path ("/discovery/v1")
# The discovery base path. This rarely needs to be changed.
def initialize(options={})
logger.debug { "#{self.class} - Initializing client with options #{options}" }

# Normalize key to String to allow indifferent access.
options = options.inject({}) do |accu, (key, value)|
accu[key.to_sym] = value
Expand All @@ -88,7 +90,7 @@ def initialize(options={})
app_version = options[:application_version]
application_string = "#{app_name}/#{app_version || '0.0.0'}"
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
self.user_agent = options[:user_agent] || (
"#{application_string} " +
Expand All @@ -105,6 +107,7 @@ def initialize(options={})
@discovery_uris = {}
@discovery_documents = {}
@discovered_apis = {}

return self
end

Expand Down Expand Up @@ -560,6 +563,7 @@ def execute(*params)
result = request.send(connection)
if result.status == 401 && authorization.respond_to?(:refresh_token) && auto_refresh_token
begin
logger.debug("Attempting refresh of access token & retry of request")
authorization.fetch_access_token!
result = request.send(connection)
rescue Signet::AuthorizationError
Expand Down
22 changes: 12 additions & 10 deletions lib/google/api_client/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ class << self

self.logger = Logger.new(STDOUT)
self.logger.level = Logger::WARN
end

##
# Module to make accessing the logger simpler
module Logging

##
# Logger for the API client
#
# @return [Logger] logger instance.
def logger
Google::APIClient.logger
# 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
10 changes: 9 additions & 1 deletion lib/google/api_client/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
require 'addressable/uri'
require 'stringio'
require 'google/api_client/discovery'
require 'google/api_client/logging'

module Google
class APIClient

##
# Represents an API request.
class Request
include Google::APIClient::Logging

MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze

# @return [Hash] Request parameters
Expand Down Expand Up @@ -151,14 +154,19 @@ def uri=(new_uri)
# @return [Google::APIClient::Result]
# result of API request
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)

logger.debug { "#{self.class} Result: #{result.status} #{result.headers}" }

# Resumamble slightly different than other upload protocols in that it requires at least
# 2 requests.
if self.upload_type == 'resumable'
upload = result.resumable_upload
unless upload.complete?
logger.debug { "#{self.class} Sending upload body" }
result = upload.send(connection)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/google/api_client/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Google
class APIClient
module VERSION
MAJOR = 0
MINOR = 5
MINOR = 6
TINY = 0
STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down

0 comments on commit 1d7315e

Please sign in to comment.