1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 require 'compat/multi_json'
22 require 'google/api_client/version'
23 require 'google/api_client/logging'
24 require 'google/api_client/errors'
25 require 'google/api_client/environment'
26 require 'google/api_client/discovery'
27 require 'google/api_client/request'
28 require 'google/api_client/reference'
29 require 'google/api_client/result'
30 require 'google/api_client/media'
31 require 'google/api_client/service_account'
32 require 'google/api_client/batch'
33 require 'google/api_client/gzip'
34 require 'google/api_client/client_secrets'
35 require 'google/api_client/railtie' if defined?(Rails)
40 # This class manages APIs communication.
42 include Google::APIClient::Logging
45 # Creates a new Google API client.
47 # @param [Hash] options The configuration parameters for the client.
48 # @option options [Symbol, #generate_authenticated_request] :authorization
50 # The authorization mechanism used by the client. The following
51 # mechanisms are supported out-of-the-box:
53 # <li><code>:two_legged_oauth_1</code></li>
54 # <li><code>:oauth_1</code></li>
55 # <li><code>:oauth_2</code></li>
57 # @option options [Boolean] :auto_refresh_token (true)
58 # The setting that controls whether or not the api client attempts to
59 # refresh authorization when a 401 is hit in #execute. If the token does
60 # not support it, this option is ignored.
61 # @option options [String] :application_name
62 # The name of the application using the client.
63 # @option options [String] :application_version
64 # The version number of the application using the client.
65 # @option options [String] :user_agent
66 # ("{app_name} google-api-ruby-client/{version} {os_name}/{os_version}")
67 # The user agent used by the client. Most developers will want to
68 # leave this value alone and use the `:application_name` option instead.
69 # @option options [String] :host ("www.googleapis.com")
70 # The API hostname used by the client. This rarely needs to be changed.
71 # @option options [String] :port (443)
72 # The port number used by the client. This rarely needs to be changed.
73 # @option options [String] :discovery_path ("/discovery/v1")
74 # The discovery base path. This rarely needs to be changed.
75 # @option options [String] :ca_file
76 # Optional set of root certificates to use when validating SSL connections.
77 # By default, a bundled set of trusted roots will be used.
78 def initialize(options={})
79 logger.debug { "#{self.class} - Initializing client with options #{options}" }
81 # Normalize key to String to allow indifferent access.
82 options = options.inject({}) do |accu, (key, value)|
83 accu[key.to_sym] = value
86 # Almost all API usage will have a host of 'www.googleapis.com'.
87 self.host = options[:host] || 'www.googleapis.com'
88 self.port = options[:port] || 443
89 self.discovery_path = options[:discovery_path] || '/discovery/v1'
91 # Most developers will want to leave this value alone and use the
92 # application_name option.
93 if options[:application_name]
94 app_name = options[:application_name]
95 app_version = options[:application_version]
96 application_string = "#{app_name}/#{app_version || '0.0.0'}"
98 logger.warn { "#{self.class} - Please provide :application_name and :application_version when initializing the client" }
100 self.user_agent = options[:user_agent] || (
101 "#{application_string} " +
102 "google-api-ruby-client/#{Google::APIClient::VERSION::STRING} #{ENV::OS_VERSION} (gzip)"
104 # The writer method understands a few Symbols and will generate useful
105 # default authentication mechanisms.
107 options.key?(:authorization) ? options[:authorization] : :oauth_2
108 self.auto_refresh_token = options.fetch(:auto_refresh_token) { true }
109 self.key = options[:key]
110 self.user_ip = options[:user_ip]
111 self.retries = options.fetch(:retries) { 0 }
113 @discovery_documents = {}
114 @discovered_apis = {}
115 ca_file = options[:ca_file] || File.expand_path('../../cacerts.pem', __FILE__)
116 self.connection = Faraday.new do |faraday|
117 faraday.response :gzip
118 faraday.options.params_encoder = Faraday::FlatParamsEncoder
119 faraday.ssl.ca_file = ca_file
120 faraday.ssl.verify = true
121 faraday.adapter Faraday.default_adapter
127 # Returns the authorization mechanism used by the client.
129 # @return [#generate_authenticated_request] The authorization mechanism.
130 attr_reader :authorization
133 # Sets the authorization mechanism used by the client.
135 # @param [#generate_authenticated_request] new_authorization
136 # The new authorization mechanism.
137 def authorization=(new_authorization)
138 case new_authorization
139 when :oauth_1, :oauth
140 require 'signet/oauth_1/client'
141 # NOTE: Do not rely on this default value, as it may change
142 new_authorization = Signet::OAuth1::Client.new(
143 :temporary_credential_uri =>
144 'https://www.google.com/accounts/OAuthGetRequestToken',
145 :authorization_uri =>
146 'https://www.google.com/accounts/OAuthAuthorizeToken',
147 :token_credential_uri =>
148 'https://www.google.com/accounts/OAuthGetAccessToken',
149 :client_credential_key => 'anonymous',
150 :client_credential_secret => 'anonymous'
152 when :two_legged_oauth_1, :two_legged_oauth
153 require 'signet/oauth_1/client'
154 # NOTE: Do not rely on this default value, as it may change
155 new_authorization = Signet::OAuth1::Client.new(
156 :client_credential_key => nil,
157 :client_credential_secret => nil,
161 require 'signet/oauth_2/client'
162 # NOTE: Do not rely on this default value, as it may change
163 new_authorization = Signet::OAuth2::Client.new(
164 :authorization_uri =>
165 'https://accounts.google.com/o/oauth2/auth',
166 :token_credential_uri =>
167 'https://accounts.google.com/o/oauth2/token'
170 # No authorization mechanism
172 if !new_authorization.respond_to?(:generate_authenticated_request)
174 'Expected authorization mechanism to respond to ' +
175 '#generate_authenticated_request.'
178 @authorization = new_authorization
179 return @authorization
183 # Default Faraday/HTTP connection.
185 # @return [Faraday::Connection]
186 attr_accessor :connection
189 # The setting that controls whether or not the api client attempts to
190 # refresh authorization when a 401 is hit in #execute.
193 attr_accessor :auto_refresh_token
196 # The application's API key issued by the API console.
198 # @return [String] The API key.
202 # The IP address of the user this request is being performed on behalf of.
204 # @return [String] The user's IP address.
205 attr_accessor :user_ip
208 # The user agent used by the client.
211 # The user agent string used in the User-Agent header.
212 attr_accessor :user_agent
215 # The API hostname used by the client.
218 # The API hostname. Should almost always be 'www.googleapis.com'.
222 # The port number used by the client.
225 # The port number. Should almost always be 443.
229 # The base path used by the client for discovery.
232 # The base path. Should almost always be '/discovery/v1'.
233 attr_accessor :discovery_path
236 # Number of times to retry on recoverable errors
240 attr_accessor :retries
243 # Returns the URI for the directory document.
245 # @return [Addressable::URI] The URI of the directory document.
247 return resolve_uri(self.discovery_path + '/apis')
251 # Manually registers a URI as a discovery document for a specific version
254 # @param [String, Symbol] api The API name.
255 # @param [String] version The desired version of the API.
256 # @param [Addressable::URI] uri The URI of the discovery document.
257 def register_discovery_uri(api, version, uri)
259 version = version || 'v1'
260 @discovery_uris["#{api}:#{version}"] = uri
264 # Returns the URI for the discovery document.
266 # @param [String, Symbol] api The API name.
267 # @param [String] version The desired version of the API.
268 # @return [Addressable::URI] The URI of the discovery document.
269 def discovery_uri(api, version=nil)
271 version = version || 'v1'
272 return @discovery_uris["#{api}:#{version}"] ||= (
274 self.discovery_path + '/apis/{api}/{version}/rest',
282 # Manually registers a pre-loaded discovery document for a specific version
285 # @param [String, Symbol] api The API name.
286 # @param [String] version The desired version of the API.
287 # @param [String, StringIO] discovery_document
288 # The contents of the discovery document.
289 def register_discovery_document(api, version, discovery_document)
291 version = version || 'v1'
292 if discovery_document.kind_of?(StringIO)
293 discovery_document.rewind
294 discovery_document = discovery_document.string
295 elsif discovery_document.respond_to?(:to_str)
296 discovery_document = discovery_document.to_str
299 "Expected String or StringIO, got #{discovery_document.class}."
301 @discovery_documents["#{api}:#{version}"] =
302 MultiJson.load(discovery_document)
306 # Returns the parsed directory document.
308 # @return [Hash] The parsed JSON from the directory document.
309 def directory_document
310 return @directory_document ||= (begin
311 response = self.execute!(
312 :http_method => :get,
313 :uri => self.directory_uri,
314 :authenticated => false
321 # Returns the parsed discovery document.
323 # @param [String, Symbol] api The API name.
324 # @param [String] version The desired version of the API.
325 # @return [Hash] The parsed JSON from the discovery document.
326 def discovery_document(api, version=nil)
328 version = version || 'v1'
329 return @discovery_documents["#{api}:#{version}"] ||= (begin
330 response = self.execute!(
331 :http_method => :get,
332 :uri => self.discovery_uri(api, version),
333 :authenticated => false
340 # Returns all APIs published in the directory document.
342 # @return [Array] The list of available APIs.
344 @directory_apis ||= (begin
345 document_base = self.directory_uri
346 if self.directory_document && self.directory_document['items']
347 self.directory_document['items'].map do |discovery_document|
348 Google::APIClient::API.new(
360 # Returns the service object for a given service name and service version.
362 # @param [String, Symbol] api The API name.
363 # @param [String] version The desired version of the API.
365 # @return [Google::APIClient::API] The service object.
366 def discovered_api(api, version=nil)
367 if !api.kind_of?(String) && !api.kind_of?(Symbol)
369 "Expected String or Symbol, got #{api.class}."
372 version = version || 'v1'
373 return @discovered_apis["#{api}:#{version}"] ||= begin
374 document_base = self.discovery_uri(api, version)
375 discovery_document = self.discovery_document(api, version)
376 if document_base && discovery_document
377 Google::APIClient::API.new(
388 # Returns the method object for a given RPC name and service version.
390 # @param [String, Symbol] rpc_name The RPC name of the desired method.
391 # @param [String, Symbol] api The API the method is within.
392 # @param [String] version The desired version of the API.
394 # @return [Google::APIClient::Method] The method object.
395 def discovered_method(rpc_name, api, version=nil)
396 if !rpc_name.kind_of?(String) && !rpc_name.kind_of?(Symbol)
398 "Expected String or Symbol, got #{rpc_name.class}."
400 rpc_name = rpc_name.to_s
402 version = version || 'v1'
403 service = self.discovered_api(api, version)
404 if service.to_h[rpc_name]
405 return service.to_h[rpc_name]
412 # Returns the service object with the highest version number.
414 # @note <em>Warning</em>: This method should be used with great care.
415 # As APIs are updated, minor differences between versions may cause
416 # incompatibilities. Requesting a specific version will avoid this issue.
418 # @param [String, Symbol] api The name of the service.
420 # @return [Google::APIClient::API] The service object.
421 def preferred_version(api)
422 if !api.kind_of?(String) && !api.kind_of?(Symbol)
424 "Expected String or Symbol, got #{api.class}."
427 return self.discovered_apis.detect do |a|
428 a.name == api && a.preferred == true
433 # Verifies an ID token against a server certificate. Used to ensure that
434 # an ID token supplied by an untrusted client-side mechanism is valid.
435 # Raises an error if the token is invalid or missing.
437 # @deprecated Use the google-id-token gem for verifying JWTs
442 if !self.authorization.respond_to?(:id_token)
443 raise ArgumentError, (
444 "Current authorization mechanism does not support ID tokens: " +
445 "#{self.authorization.class.to_s}"
447 elsif !self.authorization.id_token
448 raise ArgumentError, (
449 "Could not verify ID token, ID token missing. " +
450 "Scopes were: #{self.authorization.scope.inspect}"
453 check_cached_certs = lambda do
455 for key, cert in @certificates
457 self.authorization.decoded_id_token(cert.public_key)
459 rescue JWT::DecodeError, Signet::UnsafeOperationError
460 # Expected exception. Ignore, ID token has not been validated.
465 if check_cached_certs.call()
468 response = self.execute!(
469 :http_method => :get,
470 :uri => 'https://www.googleapis.com/oauth2/v1/certs',
471 :authenticated => false
473 @certificates.merge!(
474 Hash[MultiJson.load(response.body).map do |key, cert|
475 [key, OpenSSL::X509::Certificate.new(cert)]
478 if check_cached_certs.call()
481 raise InvalidIDTokenError,
482 "Could not verify ID token against any available certificate."
489 # Generates a request.
491 # @option options [Google::APIClient::Method] :api_method
492 # The method object or the RPC name of the method being executed.
493 # @option options [Hash, Array] :parameters
494 # The parameters to send to the method.
495 # @option options [Hash, Array] :headers The HTTP headers for the request.
496 # @option options [String] :body The body of the request.
497 # @option options [String] :version ("v1")
498 # The service version. Only used if `api_method` is a `String`.
499 # @option options [#generate_authenticated_request] :authorization
500 # The authorization mechanism for the response. Used only if
501 # `:authenticated` is `true`.
502 # @option options [TrueClass, FalseClass] :authenticated (true)
503 # `true` if the request must be signed or somehow
504 # authenticated, `false` otherwise.
506 # @return [Google::APIClient::Reference] The generated request.
509 # request = client.generate_request(
510 # :api_method => 'plus.activities.list',
512 # {'collection' => 'public', 'userId' => 'me'}
514 def generate_request(options={})
518 return Google::APIClient::Request.new(options)
522 # Executes a request, wrapping it in a Result object.
524 # @param [Google::APIClient::Request, Hash, Array] params
525 # Either a Google::APIClient::Request, a Hash, or an Array.
527 # If a Google::APIClient::Request, no other parameters are expected.
529 # If a Hash, the below parameters are handled. If an Array, the
530 # parameters are assumed to be in the below order:
532 # - (Google::APIClient::Method) api_method:
533 # The method object or the RPC name of the method being executed.
534 # - (Hash, Array) parameters:
535 # The parameters to send to the method.
536 # - (String) body: The body of the request.
537 # - (Hash, Array) headers: The HTTP headers for the request.
538 # - (Hash) options: A set of options for the request, of which:
539 # - (#generate_authenticated_request) :authorization (default: true) -
540 # The authorization mechanism for the response. Used only if
541 # `:authenticated` is `true`.
542 # - (TrueClass, FalseClass) :authenticated (default: true) -
543 # `true` if the request must be signed or somehow
544 # authenticated, `false` otherwise.
545 # - (TrueClass, FalseClass) :gzip (default: true) -
546 # `true` if gzip enabled, `false` otherwise.
547 # - (FixNum) :retries -
548 # # of times to retry on recoverable errors
550 # @return [Google::APIClient::Result] The result from the API, nil if batch.
553 # result = client.execute(batch_request)
556 # plus = client.discovered_api('plus')
557 # result = client.execute(
558 # :api_method => plus.activities.list,
559 # :parameters => {'collection' => 'public', 'userId' => 'me'}
562 # @see Google::APIClient#generate_request
563 def execute!(*params)
564 if params.first.kind_of?(Google::APIClient::Request)
565 request = params.shift
566 options = params.shift || {}
568 # This block of code allows us to accept multiple parameter passing
569 # styles, and maintaining some backwards compatibility.
571 # Note: I'm extremely tempted to deprecate this style of execute call.
572 if params.last.respond_to?(:to_hash) && params.size == 1
578 options[:api_method] = params.shift if params.size > 0
579 options[:parameters] = params.shift if params.size > 0
580 options[:body] = params.shift if params.size > 0
581 options[:headers] = params.shift if params.size > 0
582 options.update(params.shift) if params.size > 0
583 request = self.generate_request(options)
586 request.headers['User-Agent'] ||= '' + self.user_agent unless self.user_agent.nil?
587 request.headers['Accept-Encoding'] ||= 'gzip' unless options[:gzip] == false
588 request.headers['Content-Type'] ||= ''
589 request.parameters['key'] ||= self.key unless self.key.nil?
590 request.parameters['userIp'] ||= self.user_ip unless self.user_ip.nil?
592 connection = options[:connection] || self.connection
593 request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
595 tries = 1 + (options[:retries] || self.retries)
597 Retriable.retriable :tries => tries,
598 :on => [TransmissionError],
599 :on_retry => client_error_handler(request.authorization),
600 :interval => lambda {|attempts| (2 ** attempts) + rand} do
601 result = request.send(connection, true)
606 when 301, 302, 303, 307
607 request = generate_request(request.to_hash.merge({
608 :uri => result.headers['location'],
611 raise RedirectError.new(result.headers['location'], result)
613 raise ClientError.new(result.error_message || "A client error has occurred", result)
615 raise ServerError.new(result.error_message || "A server error has occurred", result)
617 raise TransmissionError.new(result.error_message || "A transmission error has occurred", result)
623 # Same as Google::APIClient#execute!, but does not raise an exception for
626 # @see Google::APIClient#execute
629 return self.execute!(*params)
630 rescue TransmissionError => e
638 # Resolves a URI template against the client's configured base.
641 # @param [String, Addressable::URI, Addressable::Template] template
642 # The template to resolve.
643 # @param [Hash] mapping The mapping that corresponds to the template.
644 # @return [Addressable::URI] The expanded URI.
645 def resolve_uri(template, mapping={})
646 @base_uri ||= Addressable::URI.new(
651 template = if template.kind_of?(Addressable::Template)
653 elsif template.respond_to?(:to_str)
657 "Expected String, Addressable::URI, or Addressable::Template, " +
658 "got #{template.class}."
660 return Addressable::Template.new(@base_uri + template).expand(mapping)
665 # Returns on proc for special processing of retries as not all client errors
666 # are recoverable. Only 401s should be retried and only if the credentials
669 # @param [#fetch_access_token!] authorization
670 # OAuth 2 credentials
672 def client_error_handler(authorization)
673 can_refresh = authorization.respond_to?(:refresh_token) && auto_refresh_token
674 Proc.new do |exception, tries|
675 next unless exception.kind_of?(ClientError)
676 if exception.result.status == 401 && can_refresh && tries == 1
678 logger.debug("Attempting refresh of access token & retry of request")
679 authorization.fetch_access_token!
681 rescue Signet::AuthorizationError