2010-07-28 19:30:56 +00:00
|
|
|
# Copyright 2010 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
|
2012-01-27 14:57:57 +00:00
|
|
|
require 'faraday'
|
|
|
|
require 'faraday/utils'
|
2012-01-05 10:05:56 +00:00
|
|
|
require 'multi_json'
|
2012-08-30 09:15:59 +00:00
|
|
|
require 'compat/multi_json'
|
2010-10-20 23:49:15 +00:00
|
|
|
require 'stringio'
|
2010-09-16 19:12:52 +00:00
|
|
|
|
2011-10-15 08:21:21 +00:00
|
|
|
require 'google/api_client/version'
|
2013-01-05 00:49:27 +00:00
|
|
|
require 'google/api_client/logging'
|
2011-05-04 11:44:35 +00:00
|
|
|
require 'google/api_client/errors'
|
2011-05-04 22:43:33 +00:00
|
|
|
require 'google/api_client/environment'
|
2010-09-18 00:30:02 +00:00
|
|
|
require 'google/api_client/discovery'
|
2012-10-10 20:11:49 +00:00
|
|
|
require 'google/api_client/request'
|
2011-07-29 22:07:04 +00:00
|
|
|
require 'google/api_client/reference'
|
|
|
|
require 'google/api_client/result'
|
2012-03-02 01:23:36 +00:00
|
|
|
require 'google/api_client/media'
|
2012-06-07 00:27:20 +00:00
|
|
|
require 'google/api_client/service_account'
|
2012-05-16 19:05:24 +00:00
|
|
|
require 'google/api_client/batch'
|
2013-03-17 23:33:27 +00:00
|
|
|
require 'google/api_client/push'
|
2012-12-30 19:26:18 +00:00
|
|
|
require 'google/api_client/railtie' if defined?(Rails)
|
2011-12-02 13:43:55 +00:00
|
|
|
|
2010-10-05 23:49:00 +00:00
|
|
|
module Google
|
2012-12-30 19:26:18 +00:00
|
|
|
|
2010-08-10 04:44:14 +00:00
|
|
|
##
|
2011-05-04 11:44:35 +00:00
|
|
|
# This class manages APIs communication.
|
2010-08-10 04:44:14 +00:00
|
|
|
class APIClient
|
2013-01-05 00:14:14 +00:00
|
|
|
include Google::APIClient::Logging
|
2012-12-30 19:26:18 +00:00
|
|
|
|
2010-09-28 23:09:07 +00:00
|
|
|
##
|
2011-05-04 11:44:35 +00:00
|
|
|
# Creates a new Google API client.
|
|
|
|
#
|
|
|
|
# @param [Hash] options The configuration parameters for the client.
|
|
|
|
# @option options [Symbol, #generate_authenticated_request] :authorization
|
|
|
|
# (:oauth_1)
|
|
|
|
# The authorization mechanism used by the client. The following
|
|
|
|
# mechanisms are supported out-of-the-box:
|
|
|
|
# <ul>
|
|
|
|
# <li><code>:two_legged_oauth_1</code></li>
|
|
|
|
# <li><code>:oauth_1</code></li>
|
|
|
|
# <li><code>:oauth_2</code></li>
|
|
|
|
# </ul>
|
2012-12-29 00:02:02 +00:00
|
|
|
# @option options [Boolean] :auto_refresh_token (true)
|
|
|
|
# The setting that controls whether or not the api client attempts to
|
|
|
|
# refresh authorization when a 401 is hit in #execute. If the token does
|
|
|
|
# not support it, this option is ignored.
|
2011-05-12 17:12:15 +00:00
|
|
|
# @option options [String] :application_name
|
2011-12-02 13:43:55 +00:00
|
|
|
# The name of the application using the client.
|
|
|
|
# @option options [String] :application_version
|
|
|
|
# The version number of the application using the client.
|
2011-05-12 17:12:15 +00:00
|
|
|
# @option options [String] :user_agent
|
|
|
|
# ("{app_name} google-api-ruby-client/{version} {os_name}/{os_version}")
|
2011-05-04 11:44:35 +00:00
|
|
|
# The user agent used by the client. Most developers will want to
|
2011-05-12 17:12:15 +00:00
|
|
|
# leave this value alone and use the `:application_name` option instead.
|
2012-02-27 11:13:42 +00:00
|
|
|
# @option options [String] :host ("www.googleapis.com")
|
|
|
|
# The API hostname used by the client. This rarely needs to be changed.
|
|
|
|
# @option options [String] :port (443)
|
|
|
|
# The port number used by the client. This rarely needs to be changed.
|
|
|
|
# @option options [String] :discovery_path ("/discovery/v1")
|
|
|
|
# The discovery base path. This rarely needs to be changed.
|
2010-08-10 04:44:14 +00:00
|
|
|
def initialize(options={})
|
2013-01-05 00:14:14 +00:00
|
|
|
logger.debug { "#{self.class} - Initializing client with options #{options}" }
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
# Normalize key to String to allow indifferent access.
|
|
|
|
options = options.inject({}) do |accu, (key, value)|
|
2012-09-24 23:09:17 +00:00
|
|
|
accu[key.to_sym] = value
|
2011-05-04 11:44:35 +00:00
|
|
|
accu
|
|
|
|
end
|
|
|
|
# Almost all API usage will have a host of 'www.googleapis.com'.
|
2012-09-24 23:09:17 +00:00
|
|
|
self.host = options[:host] || 'www.googleapis.com'
|
|
|
|
self.port = options[:port] || 443
|
|
|
|
self.discovery_path = options[:discovery_path] || '/discovery/v1'
|
2012-01-25 08:23:03 +00:00
|
|
|
|
2011-05-04 22:43:33 +00:00
|
|
|
# Most developers will want to leave this value alone and use the
|
|
|
|
# application_name option.
|
2012-12-30 19:26:18 +00:00
|
|
|
if options[:application_name]
|
|
|
|
app_name = options[:application_name]
|
|
|
|
app_version = options[:application_version]
|
|
|
|
application_string = "#{app_name}/#{app_version || '0.0.0'}"
|
|
|
|
else
|
2013-01-05 00:14:14 +00:00
|
|
|
logger.warn { "#{self.class} - Please provide :application_name and :application_version when initializing the client" }
|
2012-12-30 19:26:18 +00:00
|
|
|
end
|
2012-09-24 23:09:17 +00:00
|
|
|
self.user_agent = options[:user_agent] || (
|
2011-12-02 13:43:55 +00:00
|
|
|
"#{application_string} " +
|
2013-01-05 00:49:27 +00:00
|
|
|
"google-api-ruby-client/#{Google::APIClient::VERSION::STRING} " +
|
2011-12-02 13:43:55 +00:00
|
|
|
ENV::OS_VERSION
|
2011-05-04 22:43:33 +00:00
|
|
|
).strip
|
2011-05-04 11:44:35 +00:00
|
|
|
# The writer method understands a few Symbols and will generate useful
|
|
|
|
# default authentication mechanisms.
|
2012-03-02 16:07:35 +00:00
|
|
|
self.authorization =
|
2012-09-24 23:09:17 +00:00
|
|
|
options.key?(:authorization) ? options[:authorization] : :oauth_2
|
2013-01-04 21:50:19 +00:00
|
|
|
self.auto_refresh_token = options.fetch(:auto_refresh_token) { true }
|
2012-09-24 23:09:17 +00:00
|
|
|
self.key = options[:key]
|
|
|
|
self.user_ip = options[:user_ip]
|
2011-05-04 11:44:35 +00:00
|
|
|
@discovery_uris = {}
|
|
|
|
@discovery_documents = {}
|
|
|
|
@discovered_apis = {}
|
2013-01-05 00:14:14 +00:00
|
|
|
|
2010-10-12 20:39:09 +00:00
|
|
|
return self
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the authorization mechanism used by the client.
|
2010-10-12 20:39:09 +00:00
|
|
|
#
|
|
|
|
# @return [#generate_authenticated_request] The authorization mechanism.
|
2011-05-04 11:44:35 +00:00
|
|
|
attr_reader :authorization
|
|
|
|
|
|
|
|
##
|
|
|
|
# Sets the authorization mechanism used by the client.
|
|
|
|
#
|
|
|
|
# @param [#generate_authenticated_request] new_authorization
|
|
|
|
# The new authorization mechanism.
|
|
|
|
def authorization=(new_authorization)
|
|
|
|
case new_authorization
|
2010-10-12 20:39:09 +00:00
|
|
|
when :oauth_1, :oauth
|
2010-09-13 21:54:43 +00:00
|
|
|
require 'signet/oauth_1/client'
|
2010-08-10 04:44:14 +00:00
|
|
|
# NOTE: Do not rely on this default value, as it may change
|
2011-05-04 11:44:35 +00:00
|
|
|
new_authorization = Signet::OAuth1::Client.new(
|
2010-09-13 21:54:43 +00:00
|
|
|
:temporary_credential_uri =>
|
|
|
|
'https://www.google.com/accounts/OAuthGetRequestToken',
|
|
|
|
:authorization_uri =>
|
|
|
|
'https://www.google.com/accounts/OAuthAuthorizeToken',
|
|
|
|
:token_credential_uri =>
|
|
|
|
'https://www.google.com/accounts/OAuthGetAccessToken',
|
|
|
|
:client_credential_key => 'anonymous',
|
|
|
|
:client_credential_secret => 'anonymous'
|
|
|
|
)
|
2010-10-22 00:36:23 +00:00
|
|
|
when :two_legged_oauth_1, :two_legged_oauth
|
|
|
|
require 'signet/oauth_1/client'
|
|
|
|
# NOTE: Do not rely on this default value, as it may change
|
2011-05-04 11:44:35 +00:00
|
|
|
new_authorization = Signet::OAuth1::Client.new(
|
2010-10-22 00:36:23 +00:00
|
|
|
:client_credential_key => nil,
|
|
|
|
:client_credential_secret => nil,
|
|
|
|
:two_legged => true
|
|
|
|
)
|
2011-05-04 11:44:35 +00:00
|
|
|
when :oauth_2
|
|
|
|
require 'signet/oauth_2/client'
|
|
|
|
# NOTE: Do not rely on this default value, as it may change
|
|
|
|
new_authorization = Signet::OAuth2::Client.new(
|
|
|
|
:authorization_uri =>
|
|
|
|
'https://accounts.google.com/o/oauth2/auth',
|
|
|
|
:token_credential_uri =>
|
|
|
|
'https://accounts.google.com/o/oauth2/token'
|
|
|
|
)
|
2010-10-12 20:39:09 +00:00
|
|
|
when nil
|
|
|
|
# No authorization mechanism
|
|
|
|
else
|
2011-05-04 11:44:35 +00:00
|
|
|
if !new_authorization.respond_to?(:generate_authenticated_request)
|
2010-10-12 20:39:09 +00:00
|
|
|
raise TypeError,
|
|
|
|
'Expected authorization mechanism to respond to ' +
|
|
|
|
'#generate_authenticated_request.'
|
|
|
|
end
|
2010-08-10 04:44:14 +00:00
|
|
|
end
|
2011-05-04 11:44:35 +00:00
|
|
|
@authorization = new_authorization
|
|
|
|
return @authorization
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
|
2012-12-29 00:02:02 +00:00
|
|
|
##
|
|
|
|
# The setting that controls whether or not the api client attempts to
|
|
|
|
# refresh authorization when a 401 is hit in #execute.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
attr_accessor :auto_refresh_token
|
|
|
|
|
2013-01-04 21:50:19 +00:00
|
|
|
##
|
|
|
|
# The application's API key issued by the API console.
|
|
|
|
#
|
|
|
|
# @return [String] The API key.
|
|
|
|
attr_accessor :key
|
|
|
|
|
2011-09-21 07:51:51 +00:00
|
|
|
##
|
|
|
|
# The IP address of the user this request is being performed on behalf of.
|
|
|
|
#
|
|
|
|
# @return [String] The user's IP address.
|
|
|
|
attr_accessor :user_ip
|
|
|
|
|
2012-02-27 11:13:42 +00:00
|
|
|
##
|
|
|
|
# The user agent used by the client.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
# The user agent string used in the User-Agent header.
|
|
|
|
attr_accessor :user_agent
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
##
|
|
|
|
# The API hostname used by the client.
|
|
|
|
#
|
|
|
|
# @return [String]
|
2012-02-27 11:13:42 +00:00
|
|
|
# The API hostname. Should almost always be 'www.googleapis.com'.
|
2011-05-04 11:44:35 +00:00
|
|
|
attr_accessor :host
|
|
|
|
|
|
|
|
##
|
2012-02-27 11:13:42 +00:00
|
|
|
# The port number used by the client.
|
2011-05-04 11:44:35 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
2012-02-27 11:13:42 +00:00
|
|
|
# The port number. Should almost always be 443.
|
|
|
|
attr_accessor :port
|
|
|
|
|
|
|
|
##
|
|
|
|
# The base path used by the client for discovery.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
# The base path. Should almost always be '/discovery/v1'.
|
|
|
|
attr_accessor :discovery_path
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
##
|
|
|
|
# Returns the URI for the directory document.
|
|
|
|
#
|
|
|
|
# @return [Addressable::URI] The URI of the directory document.
|
|
|
|
def directory_uri
|
2012-02-27 11:13:42 +00:00
|
|
|
return resolve_uri(self.discovery_path + '/apis')
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Manually registers a URI as a discovery document for a specific version
|
|
|
|
# of an API.
|
|
|
|
#
|
2012-01-27 14:57:57 +00:00
|
|
|
# @param [String, Symbol] api The API name.
|
|
|
|
# @param [String] version The desired version of the API.
|
2011-05-04 11:44:35 +00:00
|
|
|
# @param [Addressable::URI] uri The URI of the discovery document.
|
|
|
|
def register_discovery_uri(api, version, uri)
|
|
|
|
api = api.to_s
|
|
|
|
version = version || 'v1'
|
|
|
|
@discovery_uris["#{api}:#{version}"] = uri
|
2010-08-10 04:44:14 +00:00
|
|
|
end
|
2010-09-13 21:54:43 +00:00
|
|
|
|
2010-09-28 23:09:07 +00:00
|
|
|
##
|
|
|
|
# Returns the URI for the discovery document.
|
|
|
|
#
|
2012-01-27 14:57:57 +00:00
|
|
|
# @param [String, Symbol] api The API name.
|
|
|
|
# @param [String] version The desired version of the API.
|
2010-09-28 23:09:07 +00:00
|
|
|
# @return [Addressable::URI] The URI of the discovery document.
|
2011-05-04 11:44:35 +00:00
|
|
|
def discovery_uri(api, version=nil)
|
|
|
|
api = api.to_s
|
|
|
|
version = version || 'v1'
|
2012-02-27 11:13:42 +00:00
|
|
|
return @discovery_uris["#{api}:#{version}"] ||= (
|
|
|
|
resolve_uri(
|
|
|
|
self.discovery_path + '/apis/{api}/{version}/rest',
|
|
|
|
'api' => api,
|
|
|
|
'version' => version
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-02-27 11:13:42 +00:00
|
|
|
)
|
2010-08-19 23:21:45 +00:00
|
|
|
end
|
2010-09-13 21:54:43 +00:00
|
|
|
|
2010-10-22 22:21:26 +00:00
|
|
|
##
|
2011-05-04 11:44:35 +00:00
|
|
|
# Manually registers a pre-loaded discovery document for a specific version
|
|
|
|
# of an API.
|
2010-10-22 22:21:26 +00:00
|
|
|
#
|
2012-01-27 14:57:57 +00:00
|
|
|
# @param [String, Symbol] api The API name.
|
|
|
|
# @param [String] version The desired version of the API.
|
2011-05-04 11:44:35 +00:00
|
|
|
# @param [String, StringIO] discovery_document
|
|
|
|
# The contents of the discovery document.
|
|
|
|
def register_discovery_document(api, version, discovery_document)
|
|
|
|
api = api.to_s
|
|
|
|
version = version || 'v1'
|
|
|
|
if discovery_document.kind_of?(StringIO)
|
|
|
|
discovery_document.rewind
|
|
|
|
discovery_document = discovery_document.string
|
|
|
|
elsif discovery_document.respond_to?(:to_str)
|
|
|
|
discovery_document = discovery_document.to_str
|
|
|
|
else
|
|
|
|
raise TypeError,
|
|
|
|
"Expected String or StringIO, got #{discovery_document.class}."
|
|
|
|
end
|
|
|
|
@discovery_documents["#{api}:#{version}"] =
|
2012-04-24 11:39:25 +00:00
|
|
|
MultiJson.load(discovery_document)
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns the parsed directory document.
|
|
|
|
#
|
|
|
|
# @return [Hash] The parsed JSON from the directory document.
|
|
|
|
def directory_document
|
|
|
|
return @directory_document ||= (begin
|
2012-09-05 21:16:31 +00:00
|
|
|
response = self.execute!(
|
2012-01-27 14:57:57 +00:00
|
|
|
:http_method => :get,
|
2011-09-22 07:41:13 +00:00
|
|
|
:uri => self.directory_uri,
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false
|
2011-09-22 07:41:13 +00:00
|
|
|
)
|
2012-09-05 21:16:31 +00:00
|
|
|
response.data
|
2011-05-04 11:44:35 +00:00
|
|
|
end)
|
2010-10-22 22:21:26 +00:00
|
|
|
end
|
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
|
|
|
# Returns the parsed discovery document.
|
|
|
|
#
|
2012-01-27 14:57:57 +00:00
|
|
|
# @param [String, Symbol] api The API name.
|
|
|
|
# @param [String] version The desired version of the API.
|
2010-10-05 23:46:43 +00:00
|
|
|
# @return [Hash] The parsed JSON from the discovery document.
|
2011-05-04 11:44:35 +00:00
|
|
|
def discovery_document(api, version=nil)
|
|
|
|
api = api.to_s
|
|
|
|
version = version || 'v1'
|
|
|
|
return @discovery_documents["#{api}:#{version}"] ||= (begin
|
2012-09-05 21:16:31 +00:00
|
|
|
response = self.execute!(
|
2012-01-27 14:57:57 +00:00
|
|
|
:http_method => :get,
|
2011-09-22 07:41:13 +00:00
|
|
|
:uri => self.discovery_uri(api, version),
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false
|
2011-09-22 07:41:13 +00:00
|
|
|
)
|
2012-09-05 21:16:31 +00:00
|
|
|
response.data
|
2010-09-16 19:12:52 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
2011-05-04 11:44:35 +00:00
|
|
|
# Returns all APIs published in the directory document.
|
|
|
|
#
|
|
|
|
# @return [Array] The list of available APIs.
|
|
|
|
def discovered_apis
|
|
|
|
@directory_apis ||= (begin
|
|
|
|
document_base = self.directory_uri
|
|
|
|
if self.directory_document && self.directory_document['items']
|
|
|
|
self.directory_document['items'].map do |discovery_document|
|
2011-07-29 22:07:04 +00:00
|
|
|
Google::APIClient::API.new(
|
2011-05-04 11:44:35 +00:00
|
|
|
document_base,
|
|
|
|
discovery_document
|
2010-09-16 19:12:52 +00:00
|
|
|
)
|
|
|
|
end
|
2011-05-04 11:44:35 +00:00
|
|
|
else
|
|
|
|
[]
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
|
|
|
# Returns the service object for a given service name and service version.
|
|
|
|
#
|
2012-01-27 14:57:57 +00:00
|
|
|
# @param [String, Symbol] api The API name.
|
|
|
|
# @param [String] version The desired version of the API.
|
2010-10-05 23:46:43 +00:00
|
|
|
#
|
2011-05-04 11:44:35 +00:00
|
|
|
# @return [Google::APIClient::API] The service object.
|
|
|
|
def discovered_api(api, version=nil)
|
|
|
|
if !api.kind_of?(String) && !api.kind_of?(Symbol)
|
2010-09-30 21:33:23 +00:00
|
|
|
raise TypeError,
|
2011-05-04 11:44:35 +00:00
|
|
|
"Expected String or Symbol, got #{api.class}."
|
2010-09-30 21:33:23 +00:00
|
|
|
end
|
2011-05-04 11:44:35 +00:00
|
|
|
api = api.to_s
|
|
|
|
version = version || 'v1'
|
|
|
|
return @discovered_apis["#{api}:#{version}"] ||= begin
|
|
|
|
document_base = self.discovery_uri(api, version)
|
|
|
|
discovery_document = self.discovery_document(api, version)
|
|
|
|
if document_base && discovery_document
|
2011-07-29 22:07:04 +00:00
|
|
|
Google::APIClient::API.new(
|
2011-05-04 11:44:35 +00:00
|
|
|
document_base,
|
|
|
|
discovery_document
|
|
|
|
)
|
|
|
|
else
|
|
|
|
nil
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
end
|
2010-09-13 21:54:43 +00:00
|
|
|
end
|
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
|
|
|
# Returns the method object for a given RPC name and service version.
|
|
|
|
#
|
|
|
|
# @param [String, Symbol] rpc_name The RPC name of the desired method.
|
2012-09-28 19:07:11 +00:00
|
|
|
# @param [String, Symbol] api The API the method is within.
|
2012-01-27 14:57:57 +00:00
|
|
|
# @param [String] version The desired version of the API.
|
2010-10-05 23:46:43 +00:00
|
|
|
#
|
|
|
|
# @return [Google::APIClient::Method] The method object.
|
2011-05-04 11:44:35 +00:00
|
|
|
def discovered_method(rpc_name, api, version=nil)
|
2010-09-30 21:33:23 +00:00
|
|
|
if !rpc_name.kind_of?(String) && !rpc_name.kind_of?(Symbol)
|
|
|
|
raise TypeError,
|
|
|
|
"Expected String or Symbol, got #{rpc_name.class}."
|
|
|
|
end
|
|
|
|
rpc_name = rpc_name.to_s
|
2011-05-04 11:44:35 +00:00
|
|
|
api = api.to_s
|
|
|
|
version = version || 'v1'
|
|
|
|
service = self.discovered_api(api, version)
|
|
|
|
if service.to_h[rpc_name]
|
|
|
|
return service.to_h[rpc_name]
|
|
|
|
else
|
|
|
|
return nil
|
2010-09-16 23:40:08 +00:00
|
|
|
end
|
|
|
|
end
|
2010-09-16 19:12:52 +00:00
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
|
|
|
# Returns the service object with the highest version number.
|
|
|
|
#
|
2011-05-04 11:44:35 +00:00
|
|
|
# @note <em>Warning</em>: This method should be used with great care.
|
|
|
|
# As APIs are updated, minor differences between versions may cause
|
2010-10-05 23:46:43 +00:00
|
|
|
# incompatibilities. Requesting a specific version will avoid this issue.
|
|
|
|
#
|
2011-05-04 11:44:35 +00:00
|
|
|
# @param [String, Symbol] api The name of the service.
|
2010-10-05 23:46:43 +00:00
|
|
|
#
|
2011-05-04 11:44:35 +00:00
|
|
|
# @return [Google::APIClient::API] The service object.
|
|
|
|
def preferred_version(api)
|
|
|
|
if !api.kind_of?(String) && !api.kind_of?(Symbol)
|
2010-09-30 21:33:23 +00:00
|
|
|
raise TypeError,
|
2011-05-04 11:44:35 +00:00
|
|
|
"Expected String or Symbol, got #{api.class}."
|
2010-09-30 21:33:23 +00:00
|
|
|
end
|
2011-05-04 11:44:35 +00:00
|
|
|
api = api.to_s
|
|
|
|
return self.discovered_apis.detect do |a|
|
|
|
|
a.name == api && a.preferred == true
|
2011-01-05 01:09:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-15 12:23:54 +00:00
|
|
|
##
|
|
|
|
# Verifies an ID token against a server certificate. Used to ensure that
|
|
|
|
# an ID token supplied by an untrusted client-side mechanism is valid.
|
|
|
|
# Raises an error if the token is invalid or missing.
|
|
|
|
def verify_id_token!
|
|
|
|
require 'jwt'
|
|
|
|
require 'openssl'
|
|
|
|
@certificates ||= {}
|
|
|
|
if !self.authorization.respond_to?(:id_token)
|
|
|
|
raise ArgumentError, (
|
|
|
|
"Current authorization mechanism does not support ID tokens: " +
|
|
|
|
"#{self.authorization.class.to_s}"
|
|
|
|
)
|
|
|
|
elsif !self.authorization.id_token
|
|
|
|
raise ArgumentError, (
|
|
|
|
"Could not verify ID token, ID token missing. " +
|
|
|
|
"Scopes were: #{self.authorization.scope.inspect}"
|
|
|
|
)
|
|
|
|
else
|
|
|
|
check_cached_certs = lambda do
|
|
|
|
valid = false
|
|
|
|
for key, cert in @certificates
|
|
|
|
begin
|
|
|
|
self.authorization.decoded_id_token(cert.public_key)
|
|
|
|
valid = true
|
|
|
|
rescue JWT::DecodeError, Signet::UnsafeOperationError
|
|
|
|
# Expected exception. Ignore, ID token has not been validated.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
valid
|
|
|
|
end
|
|
|
|
if check_cached_certs.call()
|
|
|
|
return true
|
|
|
|
end
|
2012-09-05 21:16:31 +00:00
|
|
|
response = self.execute!(
|
2012-02-15 12:23:54 +00:00
|
|
|
:http_method => :get,
|
|
|
|
:uri => 'https://www.googleapis.com/oauth2/v1/certs',
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false
|
2012-09-05 21:16:31 +00:00
|
|
|
)
|
|
|
|
@certificates.merge!(
|
|
|
|
Hash[MultiJson.load(response.body).map do |key, cert|
|
|
|
|
[key, OpenSSL::X509::Certificate.new(cert)]
|
|
|
|
end]
|
2012-02-15 12:23:54 +00:00
|
|
|
)
|
|
|
|
if check_cached_certs.call()
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
raise InvalidIDTokenError,
|
|
|
|
"Could not verify ID token against any available certificate."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
|
|
|
# Generates a request.
|
|
|
|
#
|
2012-09-28 19:07:11 +00:00
|
|
|
# @option options [Google::APIClient::Method] :api_method
|
2010-10-05 23:46:43 +00:00
|
|
|
# The method object or the RPC name of the method being executed.
|
2012-01-27 14:57:57 +00:00
|
|
|
# @option options [Hash, Array] :parameters
|
2010-10-05 23:46:43 +00:00
|
|
|
# The parameters to send to the method.
|
2012-01-27 14:57:57 +00:00
|
|
|
# @option options [Hash, Array] :headers The HTTP headers for the request.
|
|
|
|
# @option options [String] :body The body of the request.
|
|
|
|
# @option options [String] :version ("v1")
|
|
|
|
# The service version. Only used if `api_method` is a `String`.
|
|
|
|
# @option options [#generate_authenticated_request] :authorization
|
|
|
|
# The authorization mechanism for the response. Used only if
|
|
|
|
# `:authenticated` is `true`.
|
|
|
|
# @option options [TrueClass, FalseClass] :authenticated (true)
|
|
|
|
# `true` if the request must be signed or somehow
|
|
|
|
# authenticated, `false` otherwise.
|
|
|
|
#
|
2012-09-05 21:56:52 +00:00
|
|
|
# @return [Google::APIClient::Reference] The generated request.
|
2010-10-07 22:12:34 +00:00
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# request = client.generate_request(
|
2012-01-27 14:57:57 +00:00
|
|
|
# :api_method => 'plus.activities.list',
|
2011-07-29 22:07:04 +00:00
|
|
|
# :parameters =>
|
2012-01-27 14:57:57 +00:00
|
|
|
# {'collection' => 'public', 'userId' => 'me'}
|
2010-10-07 22:12:34 +00:00
|
|
|
# )
|
2011-07-29 22:07:04 +00:00
|
|
|
def generate_request(options={})
|
2012-09-24 23:09:17 +00:00
|
|
|
options = {
|
|
|
|
:api_client => self
|
2010-09-16 23:40:08 +00:00
|
|
|
}.merge(options)
|
2012-09-24 23:09:17 +00:00
|
|
|
return Google::APIClient::Request.new(options)
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
2011-07-29 22:07:04 +00:00
|
|
|
# Executes a request, wrapping it in a Result object.
|
2010-10-05 23:46:43 +00:00
|
|
|
#
|
2012-09-24 23:09:17 +00:00
|
|
|
# @param [Google::APIClient::Request, Hash, Array] params
|
|
|
|
# Either a Google::APIClient::Request, a Hash, or an Array.
|
2012-05-16 19:05:24 +00:00
|
|
|
#
|
2012-09-24 23:09:17 +00:00
|
|
|
# If a Google::APIClient::Request, no other parameters are expected.
|
2012-05-16 19:05:24 +00:00
|
|
|
#
|
|
|
|
# If a Hash, the below parameters are handled. If an Array, the
|
|
|
|
# parameters are assumed to be in the below order:
|
2012-01-27 14:57:57 +00:00
|
|
|
#
|
2012-09-28 19:07:11 +00:00
|
|
|
# - (Google::APIClient::Method) api_method:
|
2012-05-16 19:05:24 +00:00
|
|
|
# The method object or the RPC name of the method being executed.
|
|
|
|
# - (Hash, Array) parameters:
|
|
|
|
# The parameters to send to the method.
|
|
|
|
# - (String) body: The body of the request.
|
|
|
|
# - (Hash, Array) headers: The HTTP headers for the request.
|
|
|
|
# - (Hash) options: A set of options for the request, of which:
|
|
|
|
# - (#generate_authenticated_request) :authorization (default: true) -
|
|
|
|
# The authorization mechanism for the response. Used only if
|
|
|
|
# `:authenticated` is `true`.
|
|
|
|
# - (TrueClass, FalseClass) :authenticated (default: true) -
|
|
|
|
# `true` if the request must be signed or somehow
|
|
|
|
# authenticated, `false` otherwise.
|
|
|
|
#
|
|
|
|
# @return [Google::APIClient::Result] The result from the API, nil if batch.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# result = client.execute(batch_request)
|
2011-07-29 22:07:04 +00:00
|
|
|
#
|
|
|
|
# @example
|
2012-09-28 19:07:11 +00:00
|
|
|
# plus = client.discovered_api('plus')
|
2012-01-27 14:57:57 +00:00
|
|
|
# result = client.execute(
|
2012-09-28 19:07:11 +00:00
|
|
|
# :api_method => plus.activities.list,
|
2012-01-27 14:57:57 +00:00
|
|
|
# :parameters => {'collection' => 'public', 'userId' => 'me'}
|
2011-07-29 22:07:04 +00:00
|
|
|
# )
|
2012-01-27 14:57:57 +00:00
|
|
|
#
|
|
|
|
# @see Google::APIClient#generate_request
|
2011-07-29 22:07:04 +00:00
|
|
|
def execute(*params)
|
2012-09-11 18:04:21 +00:00
|
|
|
if params.last.kind_of?(Google::APIClient::Request) &&
|
2012-05-16 19:05:24 +00:00
|
|
|
params.size == 1
|
2012-09-11 18:04:21 +00:00
|
|
|
request = params.pop
|
2012-09-24 23:09:17 +00:00
|
|
|
options = {}
|
2011-07-29 22:07:04 +00:00
|
|
|
else
|
2012-05-16 19:05:24 +00:00
|
|
|
# This block of code allows us to accept multiple parameter passing
|
|
|
|
# styles, and maintaining some backwards compatibility.
|
|
|
|
#
|
|
|
|
# Note: I'm extremely tempted to deprecate this style of execute call.
|
|
|
|
if params.last.respond_to?(:to_hash) && params.size == 1
|
|
|
|
options = params.pop
|
|
|
|
else
|
|
|
|
options = {}
|
|
|
|
end
|
2011-07-29 22:07:04 +00:00
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
options[:api_method] = params.shift if params.size > 0
|
|
|
|
options[:parameters] = params.shift if params.size > 0
|
|
|
|
options[:body] = params.shift if params.size > 0
|
|
|
|
options[:headers] = params.shift if params.size > 0
|
2012-10-04 22:30:49 +00:00
|
|
|
options.update(params.shift) if params.size > 0
|
2012-09-11 18:04:21 +00:00
|
|
|
request = self.generate_request(options)
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
2012-09-24 23:09:17 +00:00
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
request.headers['User-Agent'] ||= '' + self.user_agent unless self.user_agent.nil?
|
|
|
|
request.parameters['key'] ||= self.key unless self.key.nil?
|
|
|
|
request.parameters['userIp'] ||= self.user_ip unless self.user_ip.nil?
|
|
|
|
|
2012-09-24 23:09:17 +00:00
|
|
|
connection = options[:connection] || Faraday.default_connection
|
|
|
|
request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
|
|
|
|
|
|
|
|
result = request.send(connection)
|
2013-03-17 23:33:27 +00:00
|
|
|
if result.status == 401 && request.authorization.respond_to?(:refresh_token) && auto_refresh_token
|
2012-10-30 20:18:12 +00:00
|
|
|
begin
|
2013-01-05 00:14:14 +00:00
|
|
|
logger.debug("Attempting refresh of access token & retry of request")
|
2013-03-17 23:33:27 +00:00
|
|
|
request.authorization.fetch_access_token!
|
2012-10-30 20:18:12 +00:00
|
|
|
result = request.send(connection)
|
|
|
|
rescue Signet::AuthorizationError
|
|
|
|
# Ignore since we want the original error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-11 18:04:21 +00:00
|
|
|
return result
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Same as Google::APIClient#execute, but raises an exception if there was
|
|
|
|
# an error.
|
|
|
|
#
|
|
|
|
# @see Google::APIClient#execute
|
|
|
|
def execute!(*params)
|
|
|
|
result = self.execute(*params)
|
2012-07-25 03:35:07 +00:00
|
|
|
if result.error?
|
|
|
|
error_message = result.error_message
|
2012-01-27 14:57:57 +00:00
|
|
|
case result.response.status
|
2012-07-25 03:35:07 +00:00
|
|
|
when 400...500
|
|
|
|
exception_type = ClientError
|
|
|
|
error_message ||= "A client error has occurred."
|
|
|
|
when 500...600
|
|
|
|
exception_type = ServerError
|
|
|
|
error_message ||= "A server error has occurred."
|
|
|
|
else
|
|
|
|
exception_type = TransmissionError
|
|
|
|
error_message ||= "A transmission error has occurred."
|
2012-01-27 14:57:57 +00:00
|
|
|
end
|
|
|
|
raise exception_type, error_message
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
return result
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
2012-09-24 23:09:17 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
##
|
|
|
|
# Resolves a URI template against the client's configured base.
|
|
|
|
#
|
2012-09-28 19:07:11 +00:00
|
|
|
# @api private
|
2012-09-24 23:09:17 +00:00
|
|
|
# @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
|
|
|
|
|
2010-08-10 04:44:14 +00:00
|
|
|
end
|
|
|
|
end
|
2010-08-19 23:21:45 +00:00
|
|
|
|
|
|
|
require 'google/api_client/version'
|