Merge branch 'master' into v0.10

This commit is contained in:
Steve Bazyl committed 2017-03-29 16:25:34 -07:00
commit c4d0ed5d3c
298 files changed
+167684 -38084

No files matched your search

+15 -5
View File
@@ -26,7 +26,11 @@ module Google
class ApiCommand < HttpCommand
JSON_CONTENT_TYPE = 'application/json'
FIELDS_PARAM = 'fields'
RATE_LIMIT_ERRORS = %w(rateLimitExceeded userRateLimitExceeded)
ERROR_REASON_MAPPING = {
'rateLimitExceeded' => Google::Apis::RateLimitError,
'userRateLimitExceeded' => Google::Apis::RateLimitError,
'projectNotLinked' => Google::Apis::ProjectNotLinkedError
}
# JSON serializer for request objects
# @return [Google::Apis::Core::JsonRepresentation]
@@ -94,10 +98,12 @@ module Google
error = parse_error(body)
if error
message = sprintf('%s: %s', error['reason'], error['message'])
raise Google::Apis::RateLimitError.new(message,
status_code: status,
header: header,
body: body) if RATE_LIMIT_ERRORS.include?(error['reason'])
raise ERROR_REASON_MAPPING[error['reason']].new(
message,
status_code: status,
header: header,
body: body
) if ERROR_REASON_MAPPING.key?(error['reason'])
end
super(status, header, body, message)
else
@@ -105,6 +111,10 @@ module Google
end
end
def allow_form_encoding?
request_representation.nil? && super
end
private
# Attempt to parse a JSON error message, returning the first found error
+17 -10
View File
@@ -40,11 +40,12 @@ module Google
# True (default) if results should be cached so multiple iterations can be used.
# @param [Symbol] items
# Name of the field in the result containing the items. Defaults to :items
def initialize(service, max: nil, items: :items, cache: true, &block)
def initialize(service, max: nil, items: :items, cache: true, response_page_token: :next_page_token, &block)
@service = service
@block = block
@max = max
@items_field = items
@response_page_token_field = response_page_token
if cache
@result_cache = Hash.new do |h, k|
h[k] = @block.call(k, @service)
@@ -62,14 +63,20 @@ module Google
loop do
@last_result = @fetch_proc.call(page_token)
items = @last_result.send(@items_field)
for item in items
item_count = item_count + 1
break if @max && item_count > @max
yield item
end unless items.nil?
if items.kind_of?(Array)
for item in items
item_count = item_count + 1
break if @max && item_count > @max
yield item
end
elsif items
# yield singular non-nil items (for genomics API)
yield items
end
break if @max && item_count >= @max
break if @last_result.next_page_token.nil? || @last_result.next_page_token == page_token
page_token = @last_result.next_page_token
next_page_token = @last_result.send(@response_page_token_field)
break if next_page_token.nil? || next_page_token == page_token
page_token = next_page_token
end
end
end
@@ -263,9 +270,9 @@ module Google
# @example Retrieve all files,
# file_list = service.fetch_all { |token, s| s.list_files(page_token: token) }
# file_list.each { |f| ... }
def fetch_all(max: nil, items: :items, cache: true, &block)
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, &block)
return PagedResults.new(self, max: max, items: items, cache: cache, response_page_token: response_page_token, &block)
end
protected
+2 -1
View File
@@ -22,6 +22,7 @@ module Google
# Streaming/resumable media download support
class DownloadCommand < ApiCommand
RANGE_HEADER = 'Range'
OK_STATUS = [200, 201, 206]
# File or IO to write content to
# @return [String, File, #write]
@@ -79,7 +80,7 @@ module Google
header: request_header,
follow_redirect: true) do |res, chunk|
status = res.http_header.status_code.to_i
if [200, 201, 206].include?(status)
if OK_STATUS.include?(status)
if check_if_rewind_needed && status != 206
# Oh no! Requested a chunk, but received the entire content
# Attempt to rewind the stream
+5 -1
View File
@@ -143,7 +143,7 @@ module Google
self.url = url.expand(params) if url.is_a?(Addressable::Template)
url.query_values = query.merge(url.query_values || {})
if [:post, :put].include?(method) && body.nil?
if allow_form_encoding?
@form_encoded = true
self.body = Addressable::URI.form_encode(url.query_values(Array))
self.header['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
@@ -310,6 +310,10 @@ module Google
req_header.update(header)
end
def allow_form_encoding?
[:post, :put].include?(method) && body.nil?
end
private
def form_encoded?
+4
View File
@@ -61,6 +61,10 @@ module Google
class RateLimitError < Error
end
# A 403 HTTP error occurred.
class ProjectNotLinkedError < Error
end
# A 401 HTTP error occurred.
class AuthorizationError < Error
end
+1 -1
View File
@@ -15,7 +15,7 @@
module Google
module Apis
# Client library version
VERSION = '0.10.0'
VERSION = '0.11.0'
# Current operating system
# @private