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
|
|
|
gem 'faraday', '~> 0.7.0'
|
|
|
|
require 'faraday'
|
|
|
|
require 'faraday/utils'
|
2012-01-05 10:05:56 +00:00
|
|
|
require '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'
|
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'
|
2011-07-29 22:07:04 +00:00
|
|
|
require 'google/api_client/reference'
|
|
|
|
require 'google/api_client/result'
|
2010-09-16 19:12:52 +00:00
|
|
|
|
2011-12-02 13:43:55 +00:00
|
|
|
|
2010-10-05 23:49:00 +00:00
|
|
|
module Google
|
2010-09-28 23:09:07 +00:00
|
|
|
# TODO(bobaman): Document all this stuff.
|
|
|
|
|
2011-05-04 11:44:35 +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
|
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-01-25 08:23:03 +00:00
|
|
|
# @option options [String] :baseURI ("https://www.googleapis.com/discovery/v1")
|
|
|
|
# The base API URI used by the client. This rarely needs to be changed.
|
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.
|
2010-08-10 04:44:14 +00:00
|
|
|
def initialize(options={})
|
2011-05-04 11:44:35 +00:00
|
|
|
# Normalize key to String to allow indifferent access.
|
|
|
|
options = options.inject({}) do |accu, (key, value)|
|
|
|
|
accu[key.to_s] = value
|
|
|
|
accu
|
|
|
|
end
|
2012-01-25 08:23:03 +00:00
|
|
|
# Almost all API usage will have this base URI
|
|
|
|
self.baseURI = options["baseURI"] || "https://www.googleapis.com/discovery/v1"
|
|
|
|
|
2011-05-04 22:43:33 +00:00
|
|
|
# Most developers will want to leave this value alone and use the
|
|
|
|
# application_name option.
|
2011-12-02 13:43:55 +00:00
|
|
|
application_string = (
|
|
|
|
options["application_name"] ? (
|
|
|
|
"#{options["application_name"]}/" +
|
|
|
|
"#{options["application_version"] || '0.0.0'}"
|
|
|
|
) : ""
|
|
|
|
)
|
2011-05-04 11:44:35 +00:00
|
|
|
self.user_agent = options["user_agent"] || (
|
2011-12-02 13:43:55 +00:00
|
|
|
"#{application_string} " +
|
|
|
|
"google-api-ruby-client/#{VERSION::STRING} " +
|
|
|
|
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-01-25 08:24:22 +00:00
|
|
|
self.authorization = options.key?("authorization") ? options["authorization"] : :oauth_2
|
2011-09-21 07:51:51 +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 = {}
|
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
|
2012-01-27 14:57:57 +00:00
|
|
|
gem 'signet', '~> 0.3.0'
|
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
|
2012-01-27 14:57:57 +00:00
|
|
|
gem 'signet', '~> 0.3.0'
|
2010-10-22 00:36:23 +00:00
|
|
|
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
|
2012-01-27 14:57:57 +00:00
|
|
|
gem 'signet', '~> 0.3.0'
|
2011-05-04 11:44:35 +00:00
|
|
|
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
|
|
|
|
|
2011-09-21 07:51:51 +00:00
|
|
|
##
|
|
|
|
# The application's API key issued by the API console.
|
|
|
|
#
|
2011-12-02 13:43:55 +00:00
|
|
|
# @return [String] The API key.
|
2011-09-21 07:51:51 +00:00
|
|
|
attr_accessor :key
|
|
|
|
|
|
|
|
##
|
|
|
|
# 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
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
##
|
|
|
|
# The API hostname used by the client.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
# The API hostname. Should almost always be 'www.googleapis.com'.
|
2012-01-25 08:23:03 +00:00
|
|
|
attr_accessor :baseURI
|
2011-05-04 11:44:35 +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-12-29 04:04:16 +00:00
|
|
|
|
|
|
|
def relative_uri(path, expand={})
|
|
|
|
Addressable::Template.new(baseURI+path).expand(expand)
|
|
|
|
end
|
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
|
2011-12-29 04:04:16 +00:00
|
|
|
relative_uri('/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'
|
|
|
|
return @discovery_uris["#{api}:#{version}"] ||= (begin
|
2011-12-29 04:04:16 +00:00
|
|
|
relative_uri("/apis/{api}/{version}/rest", 'api' => api, 'version' => version)
|
2010-09-16 19:12:52 +00:00
|
|
|
end)
|
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-01-05 10:05:56 +00:00
|
|
|
MultiJson.decode(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
|
2011-09-22 07:41:13 +00:00
|
|
|
request = self.generate_request(
|
2012-01-27 14:57:57 +00:00
|
|
|
:http_method => :get,
|
2011-09-22 07:41:13 +00:00
|
|
|
:uri => self.directory_uri,
|
|
|
|
:authenticated => false
|
|
|
|
)
|
2012-01-27 14:57:57 +00:00
|
|
|
response = self.transmit(:request => request)
|
|
|
|
if response.status >= 200 && response.status < 300
|
|
|
|
MultiJson.decode(response.body)
|
|
|
|
elsif response.status >= 400
|
|
|
|
case response.status
|
|
|
|
when 400...500
|
|
|
|
exception_type = ClientError
|
|
|
|
when 500...600
|
|
|
|
exception_type = ServerError
|
|
|
|
else
|
|
|
|
exception_type = TransmissionError
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
2012-01-27 14:57:57 +00:00
|
|
|
url = request.to_env(Faraday.default_connection)[:url]
|
|
|
|
raise exception_type,
|
|
|
|
"Could not retrieve directory document at: #{url}"
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
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
|
2011-09-22 07:41:13 +00:00
|
|
|
request = self.generate_request(
|
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),
|
|
|
|
:authenticated => false
|
|
|
|
)
|
2012-01-27 14:57:57 +00:00
|
|
|
response = self.transmit(:request => request)
|
|
|
|
if response.status >= 200 && response.status < 300
|
|
|
|
MultiJson.decode(response.body)
|
|
|
|
elsif response.status >= 400
|
|
|
|
case response.status
|
|
|
|
when 400...500
|
|
|
|
exception_type = ClientError
|
|
|
|
when 500...600
|
|
|
|
exception_type = ServerError
|
|
|
|
else
|
|
|
|
exception_type = TransmissionError
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
2012-01-27 14:57:57 +00:00
|
|
|
url = request.to_env(Faraday.default_connection)[:url]
|
|
|
|
raise exception_type,
|
|
|
|
"Could not retrieve discovery document at: #{url}"
|
2010-09-16 19:12:52 +00:00
|
|
|
end
|
|
|
|
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-01-27 14:57:57 +00:00
|
|
|
# @param [String, Symbol] rpc_name The API the method is within.
|
|
|
|
# @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!
|
|
|
|
gem 'jwt', '~> 0.1.4'
|
|
|
|
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
|
|
|
|
request = self.generate_request(
|
|
|
|
:http_method => :get,
|
|
|
|
:uri => 'https://www.googleapis.com/oauth2/v1/certs',
|
|
|
|
:authenticated => false
|
|
|
|
)
|
|
|
|
response = self.transmit(:request => request)
|
|
|
|
if response.status >= 200 && response.status < 300
|
|
|
|
@certificates.merge!(
|
|
|
|
Hash[MultiJson.decode(response.body).map do |key, cert|
|
|
|
|
[key, OpenSSL::X509::Certificate.new(cert)]
|
|
|
|
end]
|
|
|
|
)
|
|
|
|
elsif response.status >= 400
|
|
|
|
case response.status
|
|
|
|
when 400...500
|
|
|
|
exception_type = ClientError
|
|
|
|
when 500...600
|
|
|
|
exception_type = ServerError
|
|
|
|
else
|
|
|
|
exception_type = TransmissionError
|
|
|
|
end
|
|
|
|
url = request.to_env(Faraday.default_connection)[:url]
|
|
|
|
raise exception_type,
|
|
|
|
"Could not retrieve certificates from: #{url}"
|
|
|
|
end
|
|
|
|
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-01-27 14:57:57 +00:00
|
|
|
# @option options [Google::APIClient::Method, String] :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.
|
|
|
|
#
|
|
|
|
# @return [Faraday::Request] 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={})
|
|
|
|
# Note: The merge method on a Hash object will coerce an API Reference
|
|
|
|
# object into a Hash and merge with the default options.
|
2010-09-16 23:40:08 +00:00
|
|
|
options={
|
2011-05-04 11:44:35 +00:00
|
|
|
:version => 'v1',
|
2011-09-21 07:51:51 +00:00
|
|
|
:authorization => self.authorization,
|
|
|
|
:key => self.key,
|
2012-01-27 14:57:57 +00:00
|
|
|
:user_ip => self.user_ip,
|
|
|
|
:connection => Faraday.default_connection
|
2010-09-16 23:40:08 +00:00
|
|
|
}.merge(options)
|
2011-07-29 22:07:04 +00:00
|
|
|
# The Reference object is going to need this to do method ID lookups.
|
|
|
|
options[:client] = self
|
2011-05-04 11:44:35 +00:00
|
|
|
# The default value for the :authenticated option depends on whether an
|
2010-10-12 20:39:09 +00:00
|
|
|
# authorization mechanism has been set.
|
|
|
|
if options[:authorization]
|
2011-05-04 11:44:35 +00:00
|
|
|
options = {:authenticated => true}.merge(options)
|
2010-10-12 20:39:09 +00:00
|
|
|
else
|
2011-05-04 11:44:35 +00:00
|
|
|
options = {:authenticated => false}.merge(options)
|
2010-10-12 20:39:09 +00:00
|
|
|
end
|
2011-07-29 22:07:04 +00:00
|
|
|
reference = Google::APIClient::Reference.new(options)
|
|
|
|
request = reference.to_request
|
2011-05-04 11:44:35 +00:00
|
|
|
if options[:authenticated]
|
2012-01-27 14:57:57 +00:00
|
|
|
request = self.generate_authenticated_request(
|
|
|
|
:request => request,
|
|
|
|
:connection => options[:connection]
|
|
|
|
)
|
2010-08-24 21:59:53 +00:00
|
|
|
end
|
2010-09-16 23:40:08 +00:00
|
|
|
return request
|
|
|
|
end
|
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
2011-07-29 22:07:04 +00:00
|
|
|
# Signs a request using the current authorization mechanism.
|
2010-10-05 23:46:43 +00:00
|
|
|
#
|
2012-01-27 14:57:57 +00:00
|
|
|
# @param [Hash] options a customizable set of options
|
2010-10-07 22:12:27 +00:00
|
|
|
#
|
2012-01-27 14:57:57 +00:00
|
|
|
# @return [Faraday::Request] The signed or otherwise authenticated request.
|
2011-07-29 22:07:04 +00:00
|
|
|
def generate_authenticated_request(options={})
|
|
|
|
return authorization.generate_authenticated_request(options)
|
2010-08-24 21:59:53 +00:00
|
|
|
end
|
2010-09-16 19:12:52 +00:00
|
|
|
|
2010-10-05 23:46:43 +00:00
|
|
|
##
|
|
|
|
# Transmits the request using the current HTTP adapter.
|
|
|
|
#
|
2012-01-27 14:57:57 +00:00
|
|
|
# @option options [Array, Faraday::Request] :request
|
|
|
|
# The HTTP request to transmit.
|
|
|
|
# @option options [String, Symbol] :method
|
|
|
|
# The method for the HTTP request.
|
|
|
|
# @option options [String, Addressable::URI] :uri
|
|
|
|
# The URI for the HTTP request.
|
|
|
|
# @option options [Array, Hash] :headers
|
|
|
|
# The headers for the HTTP request.
|
|
|
|
# @option options [String] :body
|
|
|
|
# The body for the HTTP request.
|
|
|
|
# @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
|
|
|
|
if options[:request]
|
|
|
|
if options[:request].kind_of?(Array)
|
|
|
|
method, uri, headers, body = options[:request]
|
|
|
|
elsif options[:request].kind_of?(Faraday::Request)
|
|
|
|
unless options[:connection]
|
|
|
|
raise ArgumentError,
|
|
|
|
"Faraday::Request used, requires a connection to be provided."
|
|
|
|
end
|
|
|
|
method = options[:request].method.to_s.downcase.to_sym
|
|
|
|
uri = options[:connection].build_url(
|
|
|
|
options[:request].path, options[:request].params
|
|
|
|
)
|
|
|
|
headers = options[:request].headers || {}
|
|
|
|
body = options[:request].body || ''
|
|
|
|
end
|
|
|
|
else
|
|
|
|
method = options[:method] || :get
|
|
|
|
uri = options[:uri]
|
|
|
|
headers = options[:headers] || []
|
|
|
|
body = options[:body] || ''
|
|
|
|
end
|
|
|
|
headers = headers.to_a if headers.kind_of?(Hash)
|
|
|
|
request_components = {
|
|
|
|
:method => method,
|
|
|
|
:uri => uri,
|
|
|
|
:headers => headers,
|
|
|
|
:body => body
|
|
|
|
}
|
|
|
|
# Verify that we have all pieces required to transmit an HTTP request
|
|
|
|
request_components.each do |(key, value)|
|
|
|
|
unless value
|
|
|
|
raise ArgumentError, "Missing :#{key} parameter."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-05 01:09:52 +00:00
|
|
|
if self.user_agent != nil
|
|
|
|
# If there's no User-Agent header, set one.
|
|
|
|
unless headers.kind_of?(Enumerable)
|
|
|
|
# We need to use some Enumerable methods, relying on the presence of
|
|
|
|
# the #each method.
|
2012-01-25 10:01:30 +00:00
|
|
|
class << headers
|
2011-01-05 01:09:52 +00:00
|
|
|
include Enumerable
|
|
|
|
end
|
|
|
|
end
|
2011-05-04 11:44:35 +00:00
|
|
|
if self.user_agent.kind_of?(String)
|
|
|
|
unless headers.any? { |k, v| k.downcase == 'User-Agent'.downcase }
|
|
|
|
headers = headers.to_a.insert(0, ['User-Agent', self.user_agent])
|
|
|
|
end
|
|
|
|
elsif self.user_agent != nil
|
|
|
|
raise TypeError,
|
|
|
|
"Expected User-Agent to be String, got #{self.user_agent.class}"
|
2011-01-05 01:09:52 +00:00
|
|
|
end
|
|
|
|
end
|
2012-01-27 14:57:57 +00:00
|
|
|
|
|
|
|
request = Faraday::Request.create(method.to_s.downcase.to_sym) do |req|
|
|
|
|
req.url(Addressable::URI.parse(uri))
|
|
|
|
req.headers = Faraday::Utils::Headers.new(headers)
|
|
|
|
req.body = body
|
|
|
|
end
|
|
|
|
request_env = request.to_env(options[:connection])
|
|
|
|
response = options[:connection].app.call(request_env)
|
|
|
|
return response
|
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
|
|
|
#
|
2011-07-29 22:07:04 +00:00
|
|
|
# @param [Google::APIClient::Method, String] api_method
|
|
|
|
# The method object or the RPC name of the method being executed.
|
|
|
|
# @param [Hash, Array] parameters
|
|
|
|
# The parameters to send to the method.
|
|
|
|
# @param [String] body The body of the request.
|
|
|
|
# @param [Hash, Array] headers The HTTP headers for the request.
|
2012-01-27 14:57:57 +00:00
|
|
|
# @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.
|
|
|
|
#
|
|
|
|
# @return [Google::APIClient::Result] The result from the API.
|
2011-07-29 22:07:04 +00:00
|
|
|
#
|
|
|
|
# @example
|
2012-01-27 14:57:57 +00:00
|
|
|
# result = client.execute(
|
|
|
|
# :api_method => 'plus.activities.list',
|
|
|
|
# :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)
|
|
|
|
# This block of code allows us to accept multiple parameter passing
|
2011-08-02 20:20:54 +00:00
|
|
|
# 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
|
2011-07-29 22:07:04 +00:00
|
|
|
options = params.pop
|
|
|
|
else
|
|
|
|
options = {}
|
|
|
|
end
|
|
|
|
options[:api_method] = params.shift if params.size > 0
|
|
|
|
options[:parameters] = params.shift if params.size > 0
|
2012-01-27 14:57:57 +00:00
|
|
|
options[:body] = params.shift if params.size > 0
|
2011-07-29 22:07:04 +00:00
|
|
|
options[:headers] = params.shift if params.size > 0
|
|
|
|
options[:client] = self
|
|
|
|
|
|
|
|
reference = Google::APIClient::Reference.new(options)
|
|
|
|
request = self.generate_request(reference)
|
|
|
|
response = self.transmit(
|
2012-01-27 14:57:57 +00:00
|
|
|
:request => request,
|
|
|
|
:connection => options[:connection]
|
2011-07-29 22:07:04 +00:00
|
|
|
)
|
|
|
|
return Google::APIClient::Result.new(reference, request, response)
|
|
|
|
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)
|
2011-09-22 09:21:05 +00:00
|
|
|
if result.data.respond_to?(:error) &&
|
|
|
|
result.data.error.respond_to?(:message)
|
2011-07-29 22:07:04 +00:00
|
|
|
# You're going to get a terrible error message if the response isn't
|
|
|
|
# parsed successfully as an error.
|
2011-09-22 09:21:05 +00:00
|
|
|
error_message = result.data.error.message
|
|
|
|
elsif result.data['error'] && result.data['error']['message']
|
|
|
|
error_message = result.data['error']['message']
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
2012-01-27 14:57:57 +00:00
|
|
|
if result.response.status >= 400
|
|
|
|
case result.response.status
|
|
|
|
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."
|
|
|
|
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
|
2010-08-10 04:44:14 +00:00
|
|
|
end
|
|
|
|
end
|
2010-08-19 23:21:45 +00:00
|
|
|
|
|
|
|
require 'google/api_client/version'
|