Signed requests can now be generated and transmitted by the client.
Example code: require 'google/api_client' client = Google::APIClient.new(:service => 'buzz') client.authorization.fetch_temporary_credential!( :additional_parameters => { 'scope' => 'https://www.googleapis.com/auth/buzz' } ) client.authorization.authorization_uri # Redirect user here client.authorization.fetch_token_credential!(:verifier => '12345') response = client.execute( 'buzz.activities.list', 'scope' => '@self', 'userId' => '@me', 'alt' => 'json' ) status, headers, body = response git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@34 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
parent
f462322be5
commit
3a9d58108a
|
@ -132,29 +132,44 @@ module Google #:nodoc:
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_request(*args, &block)
|
def discovered_method(rpc_name, service_version='1.0')
|
||||||
if !args.empty? || block
|
for service in self.discovered_services
|
||||||
if args.size != 2
|
# This looks kinda weird, but is not a real problem because there's
|
||||||
raise ArgumentError, "wrong number of arguments (#{args.size} for 2)"
|
# almost always only one service, and this is memoized anyhow.
|
||||||
|
if service.version.to_s == service_version.to_s
|
||||||
|
return service.to_h[rpc_name] if service.to_h[rpc_name]
|
||||||
end
|
end
|
||||||
rpc_name = args[0]
|
|
||||||
if !rpc_name.kind_of?(String)
|
|
||||||
raise TypeError, "Expected String, got #{rpc_name.class}."
|
|
||||||
end
|
end
|
||||||
parameters = args[1]
|
return nil
|
||||||
if !parameters.kind_of?(Hash)
|
|
||||||
raise TypeError, "Expected Hash, got #{parameters.class}."
|
|
||||||
end
|
end
|
||||||
|
|
||||||
signature_needed = false
|
def generate_request(
|
||||||
if signature_needed
|
api_method, parameters={}, body='', headers=[], options={})
|
||||||
|
options={
|
||||||
|
:signed => true,
|
||||||
|
:service_version => '1.0'
|
||||||
|
}.merge(options)
|
||||||
|
if api_method.kind_of?(String)
|
||||||
|
api_method = self.discovered_method(
|
||||||
|
api_method, options[:service_version]
|
||||||
|
)
|
||||||
|
elsif !api_method.kind_of?(::Google::APIClient::Service)
|
||||||
|
raise TypeError,
|
||||||
|
"Expected String or Google::APIClient::Service, " +
|
||||||
|
"got #{api_method.class}."
|
||||||
|
end
|
||||||
|
request = api_method.generate_request(parameters, body, headers)
|
||||||
|
if options[:signed]
|
||||||
request = self.sign_request(request)
|
request = self.sign_request(request)
|
||||||
end
|
end
|
||||||
return args
|
return request
|
||||||
else
|
|
||||||
require 'google/api_client/discovery/method_builder'
|
|
||||||
return ::Google::APIClient::MethodBuilder.new(self, :build_request)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def execute(api_method, parameters={}, body='', headers=[], options={})
|
||||||
|
request = self.generate_request(
|
||||||
|
api_method, parameters, body, headers, options
|
||||||
|
)
|
||||||
|
return self.transmit_request(request)
|
||||||
end
|
end
|
||||||
|
|
||||||
def transmit_request(request)
|
def transmit_request(request)
|
||||||
|
@ -172,10 +187,6 @@ module Google #:nodoc:
|
||||||
'#generate_authenticated_request.'
|
'#generate_authenticated_request.'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute_signed_request(*args, &block)
|
|
||||||
return self.transmit_request(self.build_request(*args, &block))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,19 @@ module Google #:nodoc:
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
return @hash ||= (begin
|
||||||
|
methods_hash = {}
|
||||||
|
self.methods.each do |method|
|
||||||
|
methods_hash[method.rpc_name] = method
|
||||||
|
end
|
||||||
|
self.resources.each do |resource|
|
||||||
|
methods_hash.merge!(resource.to_h)
|
||||||
|
end
|
||||||
|
methods_hash
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns a <code>String</code> representation of the service's state.
|
# Returns a <code>String</code> representation of the service's state.
|
||||||
#
|
#
|
||||||
|
@ -117,6 +130,19 @@ module Google #:nodoc:
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
return @hash ||= (begin
|
||||||
|
methods_hash = {}
|
||||||
|
self.methods.each do |method|
|
||||||
|
methods_hash[method.rpc_name] = method
|
||||||
|
end
|
||||||
|
self.resources.each do |resource|
|
||||||
|
methods_hash.merge!(resource.to_h)
|
||||||
|
end
|
||||||
|
methods_hash
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns a <code>String</code> representation of the resource's state.
|
# Returns a <code>String</code> representation of the resource's state.
|
||||||
#
|
#
|
||||||
|
@ -137,12 +163,18 @@ module Google #:nodoc:
|
||||||
|
|
||||||
attr_reader :name, :description, :base
|
attr_reader :name, :description, :base
|
||||||
|
|
||||||
def uri_template
|
def rpc_name
|
||||||
return Addressable::Template.new(base + self.description['pathUrl'])
|
return self.description['rpcName']
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_uri(parameters={})
|
def uri_template
|
||||||
|
return @uri_template ||=
|
||||||
|
Addressable::Template.new(base + self.description['pathUrl'])
|
||||||
|
end
|
||||||
|
|
||||||
|
def normalize_parameters(parameters={})
|
||||||
# Convert keys to Strings when appropriate
|
# Convert keys to Strings when appropriate
|
||||||
|
if parameters.kind_of?(Hash) || parameters.kind_of?(Array)
|
||||||
parameters = parameters.inject({}) do |accu, (k, v)|
|
parameters = parameters.inject({}) do |accu, (k, v)|
|
||||||
k = k.to_s if k.kind_of?(Symbol)
|
k = k.to_s if k.kind_of?(Symbol)
|
||||||
k = k.to_str if k.respond_to?(:to_str)
|
k = k.to_str if k.respond_to?(:to_str)
|
||||||
|
@ -152,12 +184,17 @@ module Google #:nodoc:
|
||||||
accu[k] = v
|
accu[k] = v
|
||||||
accu
|
accu
|
||||||
end
|
end
|
||||||
template_variables = self.uri_template.variables
|
else
|
||||||
missing_variables = template_variables - parameters.keys
|
raise TypeError,
|
||||||
if missing_variables.size > 0
|
"Expected Hash or Array, got #{parameters.class}."
|
||||||
raise ArgumentError,
|
|
||||||
"Missing required parameters: #{missing_variables.join(', ')}."
|
|
||||||
end
|
end
|
||||||
|
return parameters
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_uri(parameters={})
|
||||||
|
parameters = self.normalize_parameters(parameters)
|
||||||
|
self.validate_parameters(parameters)
|
||||||
|
template_variables = self.uri_template.variables
|
||||||
uri = self.uri_template.expand(parameters)
|
uri = self.uri_template.expand(parameters)
|
||||||
query_parameters = parameters.reject do |k, v|
|
query_parameters = parameters.reject do |k, v|
|
||||||
template_variables.include?(k)
|
template_variables.include?(k)
|
||||||
|
@ -170,13 +207,46 @@ module Google #:nodoc:
|
||||||
return uri.normalize
|
return uri.normalize
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def generate_request(parameters={}, body='', headers=[])
|
||||||
|
method = self.description['httpMethod'] || 'GET'
|
||||||
|
uri = self.generate_uri(parameters)
|
||||||
|
return [method, uri.to_str, headers, [body]]
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_parameters(parameters={})
|
||||||
|
parameters = self.normalize_parameters(parameters)
|
||||||
|
parameter_description = self.description['parameters'] || {}
|
||||||
|
required_variables = Hash[parameter_description.select do |k, v|
|
||||||
|
v['required']
|
||||||
|
end].keys
|
||||||
|
missing_variables = required_variables - parameters.keys
|
||||||
|
if missing_variables.size > 0
|
||||||
|
raise ArgumentError,
|
||||||
|
"Missing required parameters: #{missing_variables.join(', ')}."
|
||||||
|
end
|
||||||
|
parameters.each do |k, v|
|
||||||
|
if parameter_description[k]
|
||||||
|
pattern = parameter_description[k]['pattern']
|
||||||
|
if pattern
|
||||||
|
regexp = Regexp.new("^#{pattern}$")
|
||||||
|
if v !~ regexp
|
||||||
|
raise ArgumentError,
|
||||||
|
"Parameter '#{k}' has an invalid value: #{v}. " +
|
||||||
|
"Must match: /^#{pattern}$/."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns a <code>String</code> representation of the method's state.
|
# Returns a <code>String</code> representation of the method's state.
|
||||||
#
|
#
|
||||||
# @return [String] The method's state, as a <code>String</code>.
|
# @return [String] The method's state, as a <code>String</code>.
|
||||||
def inspect
|
def inspect
|
||||||
sprintf(
|
sprintf(
|
||||||
"#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.name
|
"#<%s:%#0x NAME:%s>", self.class.to_s, self.object_id, self.rpc_name
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue