Class: Google::Apis::Core::BaseService

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/google/apis/core/base_service.rb

Overview

Base service for all APIs. Not to be used directly.

Constant Summary collapse

ENDPOINT_SUBSTITUTION =

A substitution string for the universe domain in an endpoint template

Returns:

  • (String)
"$UNIVERSE_DOMAIN$".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(root_url, base_path, client_name: nil, client_version: nil, universe_domain: nil) ⇒ BaseService

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of BaseService.

Parameters:

  • root_url (String, Addressable::URI)

    Root URL for the API

  • base_path (String, Addressable::URI)

    Additional path prefix for all API methods



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/google/apis/core/base_service.rb', line 177

def initialize(root_url, base_path, client_name: nil, client_version: nil, universe_domain: nil)
  @root_url_template = nil
  self.universe_domain = universe_domain
  self.root_url = root_url
  self.base_path = base_path
  self.client_name = client_name || 'google-api-ruby-client'
  self.client_version = client_version || Google::Apis::Core::VERSION
  self.upload_path = "upload/#{base_path}"
  self.batch_path = 'batch'
  self.client_options = Google::Apis::ClientOptions.default.dup
  self.request_options = Google::Apis::RequestOptions.default.dup
end

Instance Attribute Details

#authorizationSignet::OAuth2::Client

Returns OAuth2 credentials.

Returns:

  • (Signet::OAuth2::Client)

    OAuth2 credentials



193
194
195
# File 'lib/google/apis/core/base_service.rb', line 193

def authorization=(authorization)
  request_options.authorization = authorization
end

#base_pathAddressable::URI

Additional path prefix for all API methods

Returns:

  • (Addressable::URI)


142
143
144
# File 'lib/google/apis/core/base_service.rb', line 142

def base_path
  @base_path
end

#batch_pathAddressable::URI

Alternate path prefix for all batch methods

Returns:

  • (Addressable::URI)


150
151
152
# File 'lib/google/apis/core/base_service.rb', line 150

def batch_path
  @batch_path
end

#clientHTTPClient

Get the current HTTP client

Returns:

  • (HTTPClient)


154
155
156
# File 'lib/google/apis/core/base_service.rb', line 154

def client
  @client
end

#client_nameString

Client library name.

Returns:

  • (String)


166
167
168
# File 'lib/google/apis/core/base_service.rb', line 166

def client_name
  @client_name
end

#client_optionsGoogle::Apis::ClientOptions

General settings



158
159
160
# File 'lib/google/apis/core/base_service.rb', line 158

def client_options
  @client_options
end

#client_versionString

Client library version.

Returns:

  • (String)


170
171
172
# File 'lib/google/apis/core/base_service.rb', line 170

def client_version
  @client_version
end

#request_optionsGoogle::Apis::RequestOptions

Default options for all requests



162
163
164
# File 'lib/google/apis/core/base_service.rb', line 162

def request_options
  @request_options
end

#root_urlAddressable::URI, String

Root URL (host/port) for the API

Returns:

  • (Addressable::URI, String)


123
124
125
# File 'lib/google/apis/core/base_service.rb', line 123

def root_url
  @root_url
end

#universe_domainString

Universe domain

Returns:

  • (String)


106
107
108
# File 'lib/google/apis/core/base_service.rb', line 106

def universe_domain
  @universe_domain
end

#upload_pathAddressable::URI

Alternate path prefix for media uploads

Returns:

  • (Addressable::URI)


146
147
148
# File 'lib/google/apis/core/base_service.rb', line 146

def upload_path
  @upload_path
end

Instance Method Details

#batch(options = nil) {|self| ... }

This method returns an undefined value.

Perform a batch request. Calls made within the block are sent in a single network request to the server.

Examples:

service.batch do |s|
  s.get_item(id1) do |res, err|
    # process response for 1st call
  end
  # ...
  s.get_item(idN) do |res, err|
    # process response for Nth call
  end
end

Parameters:

Yields:

  • (self)


221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/google/apis/core/base_service.rb', line 221

def batch(options = nil)
  batch_command = BatchCommand.new(:post, Addressable::URI.parse(root_url + batch_path))
  batch_command.options = request_options.merge(options)
  apply_command_defaults(batch_command)
  begin
    start_batch(batch_command)
    yield self
  ensure
    end_batch
  end
  batch_command.execute(client)
end

#batch_upload(options = nil) {|self| ... }

This method returns an undefined value.

Perform a batch upload request. Calls made within the block are sent in a single network request to the server. Batch uploads are useful for uploading multiple small files. For larger files, use single requests which use a resumable upload protocol.

Examples:

service.batch do |s|
  s.insert_item(upload_source: 'file1.txt') do |res, err|
    # process response for 1st call
  end
  # ...
  s.insert_item(upload_source: 'fileN.txt') do |res, err|
    # process response for Nth call
  end
end

Parameters:

Yields:

  • (self)


253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/google/apis/core/base_service.rb', line 253

def batch_upload(options = nil)
  batch_command = BatchUploadCommand.new(:put, Addressable::URI.parse(root_url + upload_path))
  batch_command.options = request_options.merge(options)
  apply_command_defaults(batch_command)
  begin
    start_batch(batch_command)
    yield self
  ensure
    end_batch
  end
  batch_command.execute(client)
end

#fetch_all(max: nil, items: :items, cache: true, response_page_token: :next_page_token) {|token, service| ... } ⇒ Enumerable

Executes a given query with paging, automatically retrieving additional pages as necessary. Requires a block that returns the result set of a page. The current page token is supplied as an argument to the block.

Note: The returned enumerable also contains a last_result field containing the full result of the last query executed.

Examples:

Retrieve all files,

file_list = service.fetch_all { |token, s| s.list_files(page_token: token) }
file_list.each { |f| ... }

Parameters:

  • max (Fixnum) (defaults to: nil)

    Maximum number of items to iterate over. Defaults to nil -- no upper bound.

  • items (Symbol) (defaults to: :items)

    Name of the field in the result containing the items. Defaults to :items

  • cache (Boolean) (defaults to: true)

    True (default) if results should be cached so multiple iterations can be used.

Yields:

  • (token, service)

    Current page token & service instance

Yield Parameters:

  • token (String)

    Current page token to be used in the query

  • Current (service)

    service instance

Returns:

  • (Enumerable)

Since:

  • 0.9.4



334
335
336
337
# File 'lib/google/apis/core/base_service.rb', line 334

def fetch_all(max: nil, items: :items, cache: true, response_page_token: :next_page_token, &block)
  fail "fetch_all may not be used inside a batch" if batch?
  return PagedResults.new(self, max: max, items: items, cache: cache, response_page_token: response_page_token, &block)
end

#http(method, url, params: nil, body: nil, download_dest: nil, options: nil) {|result, err| ... } ⇒ String

Simple escape hatch for making API requests directly to a given URL. This is not intended to be used as a generic HTTP client and should be used only in cases where no service method exists (e.g. fetching an export link for a Google Drive file.)

Parameters:

  • method (Symbol)

    HTTP method as symbol (e.g. :get, :post, :put, ...)

  • url (String)

    URL to call

  • params (Hash<String,String>) (defaults to: nil)

    Optional hash of query parameters

  • body (#read) (defaults to: nil)

    Optional body for POST/PUT

  • download_dest (IO, String) (defaults to: nil)

    IO stream or filename to receive content download

  • options (Google::Apis::RequestOptions) (defaults to: nil)

    Request-specific options

Yields:

  • (result, err)

    Result & error if block supplied

Yield Parameters:

  • result (String)

    HTTP response body

  • err (StandardError)

    error object if request failed

Returns:

  • (String)

    HTTP response body



296
297
298
299
300
301
302
303
304
305
306
# File 'lib/google/apis/core/base_service.rb', line 296

def http(method, url, params: nil, body: nil, download_dest: nil, options: nil, &block)
  if download_dest
    command = DownloadCommand.new(method, url, body: body, client_version: client_version)
  else
    command = HttpCommand.new(method, url, body: body)
  end
  command.options = request_options.merge(options)
  apply_command_defaults(command)
  command.query.merge(Hash(params))
  execute_or_queue_command(command, &block)
end

#verify_universe_domain!Object

Verify that the universe domain setting matches the universe domain in the credentials, if present.

Raises:



343
344
345
346
347
348
349
350
351
# File 'lib/google/apis/core/base_service.rb', line 343

def verify_universe_domain!
  auth = authorization
  auth_universe_domain = auth.universe_domain if auth.respond_to? :universe_domain
  if auth_universe_domain && auth_universe_domain != universe_domain
    raise UniverseDomainError,
          "Universe domain is #{universe_domain} but credentials are in #{auth_universe_domain}"
  end
  true
end