Skip to content

Commit

Permalink
Continue internal shuffling...
Browse files Browse the repository at this point in the history
  • Loading branch information
sqrrrl committed Oct 10, 2012
1 parent f9c1ab6 commit fc45135
Show file tree
Hide file tree
Showing 13 changed files with 492 additions and 374 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.5.0
* Beta candidate, potential incompatible changes with how requests are processed. All requests
should be made using execute() or execute!()
* Reduce memory utilization when uploading large files
* Simplify internal request processing.

# 0.4.7

* Added the ability to convert client secrets to an authorization object
Expand Down
187 changes: 83 additions & 104 deletions lib/google/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
require 'google/api_client/batch'

module Google
# TODO(bobaman): Document all this stuff.


##
# This class manages APIs communication.
class APIClient
Expand Down Expand Up @@ -66,33 +63,33 @@ class APIClient
def initialize(options={})
# Normalize key to String to allow indifferent access.
options = options.inject({}) do |accu, (key, value)|
accu[key.to_s] = value
accu[key.to_sym] = value
accu
end
# Almost all API usage will have a host of 'www.googleapis.com'.
self.host = options["host"] || 'www.googleapis.com'
self.port = options["port"] || 443
self.discovery_path = options["discovery_path"] || '/discovery/v1'
self.host = options[:host] || 'www.googleapis.com'
self.port = options[:port] || 443
self.discovery_path = options[:discovery_path] || '/discovery/v1'

# Most developers will want to leave this value alone and use the
# application_name option.
application_string = (
options["application_name"] ? (
"#{options["application_name"]}/" +
"#{options["application_version"] || '0.0.0'}"
options[:application_name] ? (
"#{options[:application_name]}/" +
"#{options[:application_version] || '0.0.0'}"
) : ""
)
self.user_agent = options["user_agent"] || (
self.user_agent = options[:user_agent] || (
"#{application_string} " +
"google-api-ruby-client/#{VERSION::STRING} " +
ENV::OS_VERSION
).strip
# The writer method understands a few Symbols and will generate useful
# default authentication mechanisms.
self.authorization =
options.key?("authorization") ? options["authorization"] : :oauth_2
self.key = options["key"]
self.user_ip = options["user_ip"]
options.key?(:authorization) ? options[:authorization] : :oauth_2
self.key = options[:key]
self.user_ip = options[:user_ip]
@discovery_uris = {}
@discovery_documents = {}
@discovered_apis = {}
Expand Down Expand Up @@ -195,31 +192,6 @@ def authorization=(new_authorization)
# The base path. Should almost always be '/discovery/v1'.
attr_accessor :discovery_path

##
# Resolves a URI template against the client's configured base.
#
# @param [String, Addressable::URI, Addressable::Template] template
# The template to resolve.
# @param [Hash] mapping The mapping that corresponds to the template.
# @return [Addressable::URI] The expanded URI.
def resolve_uri(template, mapping={})
@base_uri ||= Addressable::URI.new(
:scheme => 'https',
:host => self.host,
:port => self.port
).normalize
template = if template.kind_of?(Addressable::Template)
template.pattern
elsif template.respond_to?(:to_str)
template.to_str
else
raise TypeError,
"Expected String, Addressable::URI, or Addressable::Template, " +
"got #{template.class}."
end
return Addressable::Template.new(@base_uri + template).expand(mapping)
end

##
# Returns the URI for the directory document.
#
Expand Down Expand Up @@ -292,7 +264,7 @@ def directory_document
response = self.execute!(
:http_method => :get,
:uri => self.directory_uri,
:authorization => :none
:authenticated => false
)
response.data
end)
Expand All @@ -311,7 +283,7 @@ def discovery_document(api, version=nil)
response = self.execute!(
:http_method => :get,
:uri => self.discovery_uri(api, version),
:authorization => :none
:authenticated => false
)
response.data
end)
Expand Down Expand Up @@ -447,7 +419,7 @@ def verify_id_token!
response = self.execute!(
:http_method => :get,
:uri => 'https://www.googleapis.com/oauth2/v1/certs',
:authorization => :none
:authenticated => false
)
@certificates.merge!(
Hash[MultiJson.load(response.body).map do |key, cert|
Expand All @@ -464,31 +436,6 @@ def verify_id_token!
return nil
end

def normalize_api_method(options)
method = options[:api_method]
version = options[:version]
if method.kind_of?(Google::APIClient::Method) || method == nil
return method
elsif method.respond_to?(:to_str) || method.kind_of?(Symbol)
# This method of guessing the API is unreliable. This will fail for
# APIs where the first segment of the RPC name does not match the
# service name. However, this is a fallback mechanism anyway.
# Developers should be passing in a reference to the method, rather
# than passing in a string or symbol. This should raise an error
# in the case of a mismatch.
method = method.to_s
api = method[/^([^.]+)\./, 1]
api_method = self.discovered_method(method, api, version)
if api_method.nil?
raise ArgumentError, "API method could not be found."
end
return api_method
else
raise TypeError,
"Expected Google::APIClient::Method, got #{new_api_method.class}."
end
end

##
# Generates a request.
#
Expand Down Expand Up @@ -516,46 +463,19 @@ def normalize_api_method(options)
# {'collection' => 'public', 'userId' => 'me'}
# )
def generate_request(options={})
# Note: The merge method on a Hash object will coerce an API Reference
# object into a Hash and merge with the default options.

options={
:version => 'v1',
:authorization => self.authorization,
:key => self.key,
:user_ip => self.user_ip,
:connection => Faraday.default_connection
options = {
:api_client => self
}.merge(options)

options[:api_method] = self.normalize_api_method(options) unless options[:api_method].nil?
return Google::APIClient::Reference.new(options)
end

##
# Transmits the request using the current HTTP adapter.
#
# @option options [Array, Faraday::Request] :request
# The HTTP request to transmit.
# @option options [Faraday::Connection] :connection
# The HTTP connection to use.
#
# @return [Faraday::Response] The response from the server.
def transmit(options={})
options[:connection] ||= Faraday.default_connection
request = options[:request]
request['User-Agent'] ||= '' + self.user_agent unless self.user_agent.nil?
request_env = request.to_env(options[:connection])
response = options[:connection].app.call(request_env)
return response
return Google::APIClient::Request.new(options)
end

##
# Executes a request, wrapping it in a Result object.
#
# @param [Google::APIClient::BatchRequest, Hash, Array] params
# Either a Google::APIClient::BatchRequest, a Hash, or an Array.
# @param [Google::APIClient::Request, Hash, Array] params
# Either a Google::APIClient::Request, a Hash, or an Array.
#
# If a Google::APIClient::BatchRequest, no other parameters are expected.
# If a Google::APIClient::Request, no other parameters are expected.
#
# If a Hash, the below parameters are handled. If an Array, the
# parameters are assumed to be in the below order:
Expand Down Expand Up @@ -592,6 +512,7 @@ def execute(*params)
if params.last.kind_of?(Google::APIClient::Request) &&
params.size == 1
request = params.pop
options = {}
else
# This block of code allows us to accept multiple parameter passing
# styles, and maintaining some backwards compatibility.
Expand All @@ -610,13 +531,16 @@ def execute(*params)
options[:client] = self
request = self.generate_request(options)
end
response = self.transmit(:request => request.to_http_request, :connection => Faraday.default_connection)
result = request.process_response(response)

connection = options[:connection] || Faraday.default_connection
request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false

result = request.send(connection)

if request.upload_type == 'resumable'
upload = result.resumable_upload
unless upload.complete?
response = self.transmit(:request => upload.to_http_request, :connection => Faraday.default_connection)
result = upload.process_response(response)
result = upload.send(connection)
end
end
return result
Expand Down Expand Up @@ -646,6 +570,61 @@ def execute!(*params)
end
return result
end

##
# Ensures API method names specified as strings resolve to
# discovered method instances
def resolve_method(method, version)
version ||= 'v1'
if method.kind_of?(Google::APIClient::Method) || method == nil
return method
elsif method.respond_to?(:to_str) || method.kind_of?(Symbol)
# This method of guessing the API is unreliable. This will fail for
# APIs where the first segment of the RPC name does not match the
# service name. However, this is a fallback mechanism anyway.
# Developers should be passing in a reference to the method, rather
# than passing in a string or symbol. This should raise an error
# in the case of a mismatch.
method = method.to_s
api = method[/^([^.]+)\./, 1]
api_method = self.discovered_method(method, api, version)
if api_method.nil?
raise ArgumentError, "API method could not be found."
end
return api_method
else
raise TypeError,
"Expected Google::APIClient::Method, got #{method.class}."
end
end

protected

##
# Resolves a URI template against the client's configured base.
#
# @param [String, Addressable::URI, Addressable::Template] template
# The template to resolve.
# @param [Hash] mapping The mapping that corresponds to the template.
# @return [Addressable::URI] The expanded URI.
def resolve_uri(template, mapping={})
@base_uri ||= Addressable::URI.new(
:scheme => 'https',
:host => self.host,
:port => self.port
).normalize
template = if template.kind_of?(Addressable::Template)
template.pattern
elsif template.respond_to?(:to_str)
template.to_str
else
raise TypeError,
"Expected String, Addressable::URI, or Addressable::Template, " +
"got #{template.class}."
end
return Addressable::Template.new(@base_uri + template).expand(mapping)
end

end
end

Expand Down
28 changes: 14 additions & 14 deletions lib/google/api_client/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class BatchRequest < Request
# Creates a new batch request.
#
# @param [Hash] options
# Set of options for this request, the only important one being
# :connection, which specifies an HTTP connection to use.
# Set of options for this request
# @param [Proc] block
# Callback for every call's response. Won't be called if a call defined
# a callback of its own.
Expand All @@ -67,7 +66,7 @@ def initialize(options = {}, &block)
# automatically be generated, avoiding collisions. If duplicate call IDs
# are provided, an error will be thrown.
#
# @param [Hash, Google::APIClient::Reference] call: the call to be added.
# @param [Hash, Google::APIClient::Request] call: the call to be added.
# @param [String] call_id: the ID to be used for this call. Must be unique
# @param [Proc] block: callback for this call's response.
#
Expand All @@ -90,7 +89,7 @@ def add(call, call_id = nil, &block)
# Processes the HTTP response to the batch request, issuing callbacks.
#
# @param [Faraday::Response] response: the HTTP response.
def process_response(response)
def process_http_response(response)
content_type = find_header('Content-Type', response.headers)
boundary = /.*boundary=(.+)/.match(content_type)[1]
parts = response.body.split(/--#{Regexp.escape(boundary)}/)
Expand Down Expand Up @@ -212,21 +211,22 @@ def deserialize_call_response(call_response)
#
# @return [StringIO] The request as a string in application/http format.
def serialize_call(call_id, call)
http_request = call.to_http_request
body = "#{http_request.method.to_s.upcase} #{http_request.path} HTTP/1.1"
http_request.headers.each do |header, value|
body << "\r\n%s: %s" % [header, value]
call.api_client = self.api_client
method, uri, headers, body = call.to_http_request
request = "#{method.to_s.upcase} #{Addressable::URI.parse(uri).path} HTTP/1.1"
headers.each do |header, value|
request << "\r\n%s: %s" % [header, value]
end
if http_request.body
if body
# TODO - CompositeIO if body is a stream
body << "\r\n\r\n"
if http_request.body.respond_to?(:read)
body << http_request.body.read
request << "\r\n\r\n"
if body.respond_to?(:read)
request << body.read
else
body << http_request.body.to_s
request << body.to_s
end
end
Faraday::UploadIO.new(StringIO.new(body), 'application/http', 'ruby-api-request', 'Content-ID' => id_to_header(call_id))
Faraday::UploadIO.new(StringIO.new(request), 'application/http', 'ruby-api-request', 'Content-ID' => id_to_header(call_id))
end

##
Expand Down
15 changes: 4 additions & 11 deletions lib/google/api_client/discovery/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,14 @@ def generate_uri(parameters={})
# The HTTP connection to use.
#
# @return [Array] The generated HTTP request.
def generate_request(parameters={}, body='', headers=[], options={})
options[:connection] ||= Faraday.default_connection
def generate_request(parameters={}, body='', headers={}, options={})
if !headers.kind_of?(Array) && !headers.kind_of?(Hash)
raise TypeError, "Expected Hash or Array, got #{headers.class}."
end
method = self.http_method
method = self.http_method.to_s.downcase.to_sym
uri = self.generate_uri(parameters)
headers = headers.to_a if headers.kind_of?(Hash)
return options[:connection].build_request(
method.to_s.downcase.to_sym
) do |req|
req.url(Addressable::URI.parse(uri).to_s)
req.headers = Faraday::Utils::Headers.new(headers)
req.body = body
end
headers = Faraday::Utils::Headers.new(headers)
return [method, uri, headers, body]
end


Expand Down
2 changes: 1 addition & 1 deletion lib/google/api_client/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def to_hash
#
# @param [Faraday::Response] r
# Result of a chunk upload or range query
def process_response(response)
def process_http_response(response)
case response.status
when 200...299
@complete = true
Expand Down
Loading

0 comments on commit fc45135

Please sign in to comment.