Import refactored library. See MIGRATING.MD for details
This commit is contained in:
1 parent
9b7809174d
commit
f584a2cec8
373 files changed
+255715
-474
No files matched your search
@@ -0,0 +1,128 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'active_support/inflector'
|
||||
require 'addressable/uri'
|
||||
require 'addressable/template'
|
||||
require 'google/apis/core/http_command'
|
||||
require 'google/apis/errors'
|
||||
require 'multi_json'
|
||||
require 'retriable'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Command for executing most basic API request with JSON requests/responses
|
||||
class ApiCommand < HttpCommand
|
||||
JSON_CONTENT_TYPE = 'application/json'
|
||||
FIELDS_PARAM = 'fields'
|
||||
RATE_LIMIT_ERRORS = %w(rateLimitExceeded userRateLimitExceeded)
|
||||
|
||||
# JSON serializer for request objects
|
||||
# @return [Google::Apis::Core::JsonRepresentation]
|
||||
attr_accessor :request_representation
|
||||
|
||||
# Request body to serialize
|
||||
# @return [Object]
|
||||
attr_accessor :request_object
|
||||
|
||||
# JSON serializer for response objects
|
||||
# @return [Google::Apis::Core::JsonRepresentation]
|
||||
attr_accessor :response_representation
|
||||
|
||||
# Class to instantiate when de-serializing responses
|
||||
# @return [Object]
|
||||
attr_accessor :response_class
|
||||
|
||||
# Serialize the request body
|
||||
#
|
||||
# @return [void]
|
||||
def prepare!
|
||||
query[FIELDS_PARAM] = normalize_fields_param(query[FIELDS_PARAM]) if query.key?(FIELDS_PARAM)
|
||||
if request_representation && request_object
|
||||
header[:content_type] ||= JSON_CONTENT_TYPE
|
||||
self.body = request_representation.new(request_object).to_json(skip_undefined: true)
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
# Deserialize the response body if present
|
||||
#
|
||||
# @param [String] content_type
|
||||
# Content type of body
|
||||
# @param [String, #read] body
|
||||
# Response body
|
||||
# @return [Object]
|
||||
# Response object
|
||||
# noinspection RubyUnusedLocalVariable
|
||||
def decode_response_body(content_type, body)
|
||||
return super unless response_representation
|
||||
return super if content_type.nil?
|
||||
return nil unless content_type.start_with?(JSON_CONTENT_TYPE)
|
||||
instance = response_class.new
|
||||
response_representation.new(instance).from_json(body, unwrap: response_class)
|
||||
instance
|
||||
end
|
||||
|
||||
# Check the response and raise error if needed
|
||||
#
|
||||
# @param [Fixnum] status
|
||||
# HTTP status code of response
|
||||
# @param [String] body
|
||||
# HTTP response body
|
||||
# @return [void]
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def check_status(status, body = nil)
|
||||
case status
|
||||
when 400, 402...500
|
||||
error = parse_error(body)
|
||||
if error
|
||||
logger.debug { error }
|
||||
fail Google::Apis::RateLimitError, error if RATE_LIMIT_ERRORS.include?(error['reason'])
|
||||
end
|
||||
super(status, error)
|
||||
else
|
||||
super(status, body)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Attempt to parse a JSON error message, returning the first found error
|
||||
# @param [String] body
|
||||
# HTTP response body
|
||||
# @return [Hash]
|
||||
def parse_error(body)
|
||||
hash = MultiJson.load(body)
|
||||
hash['error']['errors'].first
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
|
||||
# Convert field names from ruby conventions to original names in JSON
|
||||
#
|
||||
# @param [String] fields
|
||||
# Value of 'fields' param
|
||||
# @return [String]
|
||||
# Updated header value
|
||||
def normalize_fields_param(fields)
|
||||
# TODO: Generate map of parameter names during code gen. Small possibility that camelization fails
|
||||
fields.gsub(/:/, '').gsub(/[\w_]+/) { |str| ActiveSupport::Inflector.camelize(str, false) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,314 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'addressable/uri'
|
||||
require 'addressable/template'
|
||||
require 'google/apis/version'
|
||||
require 'google/apis/core/api_command'
|
||||
require 'google/apis/core/batch'
|
||||
require 'google/apis/core/upload'
|
||||
require 'google/apis/core/download'
|
||||
require 'google/apis/options'
|
||||
require 'googleauth'
|
||||
require 'hurley'
|
||||
require 'hurley/addressable'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Base service for all APIs. Not to be used directly.
|
||||
#
|
||||
class BaseService
|
||||
# Root URL (host/port) for the API
|
||||
# @return [Addressable::URI]
|
||||
attr_accessor :root_url
|
||||
|
||||
# Additional path prefix for all API methods
|
||||
# @return [Addressable::URI]
|
||||
attr_accessor :base_path
|
||||
|
||||
# Alternate path prefix for media uploads
|
||||
# @return [Addressable::URI]
|
||||
attr_accessor :upload_path
|
||||
|
||||
# Alternate path prefix for all batch methods
|
||||
# @return [Addressable::URI]
|
||||
attr_accessor :batch_path
|
||||
|
||||
# HTTP client
|
||||
# @return [Hurley::Client]
|
||||
attr_accessor :client
|
||||
|
||||
# General settings
|
||||
# @return [Google::Apis::ClientOptions]
|
||||
attr_accessor :client_options
|
||||
|
||||
# Default options for all requests
|
||||
# @return [Google::Apis::RequestOptions]
|
||||
attr_accessor :request_options
|
||||
|
||||
# @param [String,Addressable::URI] root_url
|
||||
# Root URL for the API
|
||||
# @param [String,Addressable::URI] base_path
|
||||
# Additional path prefix for all API methods
|
||||
# @api private
|
||||
def initialize(root_url, base_path)
|
||||
self.root_url = root_url
|
||||
self.base_path = base_path
|
||||
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
|
||||
|
||||
# @!attribute [rw] authorization
|
||||
# @return [Signet::OAuth2::Client]
|
||||
# OAuth2 credentials
|
||||
def authorization=(authorization)
|
||||
request_options.authorization = authorization
|
||||
end
|
||||
|
||||
def authorization
|
||||
request_options.authorization
|
||||
end
|
||||
|
||||
# TODO: with(options) method
|
||||
|
||||
# Perform a batch request. Calls made within the block are sent in a single network
|
||||
# request to the server.
|
||||
#
|
||||
# @example
|
||||
# service.batch do |service|
|
||||
# service.get_item(id1) do |res, err|
|
||||
# # process response for 1st call
|
||||
# end
|
||||
# # ...
|
||||
# service.get_item(idN) do |res, err|
|
||||
# # process response for Nth call
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# @param [Hash, Google::Apis::RequestOptions] options
|
||||
# Request-specific options
|
||||
# @yield [self]
|
||||
# @return [void]
|
||||
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
|
||||
Thread.current[:google_api_batch] = batch_command
|
||||
yield self
|
||||
ensure
|
||||
Thread.current[:google_api_batch] = nil
|
||||
end
|
||||
batch_command.execute(client)
|
||||
end
|
||||
|
||||
# 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.
|
||||
#
|
||||
# @example
|
||||
# service.batch do |service|
|
||||
# service.insert_item(upload_source: 'file1.txt') do |res, err|
|
||||
# # process response for 1st call
|
||||
# end
|
||||
# # ...
|
||||
# service.insert_item(upload_source: 'fileN.txt') do |res, err|
|
||||
# # process response for Nth call
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# @param [Hash, Google::Apis::RequestOptions] options
|
||||
# Request-specific options
|
||||
# @yield [self]
|
||||
# @return [void]
|
||||
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
|
||||
Thread.current[:google_api_batch] = batch_command
|
||||
yield self
|
||||
ensure
|
||||
Thread.current[:google_api_batch] = nil
|
||||
end
|
||||
batch_command.execute(client)
|
||||
end
|
||||
|
||||
# Get the current HTTP client
|
||||
# @return [Hurley::Client]
|
||||
def client
|
||||
@client ||= new_client
|
||||
end
|
||||
|
||||
|
||||
# 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.)
|
||||
#
|
||||
# @param [Symbol] method
|
||||
# HTTP method as symbol (e.g. :get, :post, :put, ...)
|
||||
# @param [String] url
|
||||
# URL to call
|
||||
# @param [Hash<String,String] params
|
||||
# Optional hash of query parameters
|
||||
# @param [#read] body
|
||||
# Optional body for POST/PUT
|
||||
# @param [IO, String] download_dest
|
||||
# IO stream or filename to receive content download
|
||||
# @param [Google::Apis::RequestOptions] options
|
||||
# Request-specific options
|
||||
#
|
||||
# @yield [result, err] Result & error if block supplied
|
||||
# @yieldparam result [String] HTTP response body
|
||||
# @yieldparam err [StandardError] error object if request failed
|
||||
#
|
||||
# @return [String] HTTP response body
|
||||
def http(method, url, params: nil, body: nil, download_dest: nil, options: nil, &block)
|
||||
if download_dest
|
||||
command = DownloadCommand.new(method, url, body: body)
|
||||
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
|
||||
|
||||
protected
|
||||
|
||||
# Create a new upload command.
|
||||
#
|
||||
# @param [symbol] method
|
||||
# HTTP method for uploading (typically :put or :post)
|
||||
# @param [String] path
|
||||
# Additional path to upload endpoint, appended to API base path
|
||||
# @param [Hash, Google::Apis::RequestOptions] options
|
||||
# Request-specific options
|
||||
# @return [Google::Apis::Core::UploadCommand]
|
||||
def make_upload_command(method, path, options)
|
||||
template = Addressable::Template.new(root_url + upload_path + path)
|
||||
if batch?
|
||||
command = MultipartUploadCommand.new(method, template)
|
||||
else
|
||||
command = ResumableUploadCommand.new(method, template)
|
||||
end
|
||||
command.options = request_options.merge(options)
|
||||
apply_command_defaults(command)
|
||||
command
|
||||
end
|
||||
|
||||
# Create a new download command.
|
||||
#
|
||||
# @param [symbol] method
|
||||
# HTTP method for uploading (typically :get)
|
||||
# @param [String] path
|
||||
# Additional path to download endpoint, appended to API base path
|
||||
# @param [Hash, Google::Apis::RequestOptions] options
|
||||
# Request-specific options
|
||||
# @return [Google::Apis::Core::DownloadCommand]
|
||||
def make_download_command(method, path, options)
|
||||
template = Addressable::Template.new(root_url + base_path + path)
|
||||
command = DownloadCommand.new(method, template)
|
||||
command.options = request_options.merge(options)
|
||||
command.query['alt'] = 'media'
|
||||
apply_command_defaults(command)
|
||||
command
|
||||
end
|
||||
|
||||
# Create a new command.
|
||||
#
|
||||
# @param [symbol] method
|
||||
# HTTP method (:get, :post, :delete, etc...)
|
||||
# @param [String] path
|
||||
# Additional path, appended to API base path
|
||||
# @param [Hash, Google::Apis::RequestOptions] options
|
||||
# Request-specific options
|
||||
# @return [Google::Apis::Core::DownloadCommand]
|
||||
def make_simple_command(method, path, options)
|
||||
template = Addressable::Template.new(root_url + base_path + path)
|
||||
command = ApiCommand.new(method, template)
|
||||
command.options = request_options.merge(options)
|
||||
apply_command_defaults(command)
|
||||
command
|
||||
end
|
||||
|
||||
# Execute the request. If a batch is in progress, the request is added to the batch instead.
|
||||
#
|
||||
# @param [Google::Apis::Core::HttpCommand] command
|
||||
# Command to execute
|
||||
# @return [Object] response object if command executed and no callback supplied
|
||||
# @yield [result, err] Result & error if block supplied
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def execute_or_queue_command(command, &callback)
|
||||
batch_command = current_batch
|
||||
if batch_command
|
||||
batch_command.add(command, &callback)
|
||||
nil
|
||||
else
|
||||
command.execute(client, &callback)
|
||||
end
|
||||
end
|
||||
|
||||
# Update commands with service-specific options. To be implemented by subclasses
|
||||
# @param [Google::Apis::Core::HttpCommand] _command
|
||||
def apply_command_defaults(_command)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Get the current batch context
|
||||
#
|
||||
# @return [Google:Apis::Core::BatchRequest]
|
||||
def current_batch
|
||||
Thread.current[:google_api_batch]
|
||||
end
|
||||
|
||||
# Check if a batch is in progress
|
||||
# @return [Boolean]
|
||||
def batch?
|
||||
!current_batch.nil?
|
||||
end
|
||||
|
||||
# Create a new HTTP client
|
||||
# @return [Hurley::Client]
|
||||
def new_client
|
||||
client = Hurley::Client.new
|
||||
client.request_options.timeout = request_options.timeout_sec
|
||||
client.request_options.proxy = client_options.proxy_url
|
||||
client.request_options.query_class = Hurley::Query::Flat
|
||||
client.ssl_options.ca_file = File.join(Google::Apis::ROOT, 'lib', 'cacerts.pem')
|
||||
client.header[:user_agent] = user_agent
|
||||
client
|
||||
end
|
||||
|
||||
# Build the user agent header
|
||||
# @return [String]
|
||||
def user_agent
|
||||
sprintf('%s/%s google-api-ruby-client/%s %s (gzip)',
|
||||
client_options.application_name,
|
||||
client_options.application_version,
|
||||
Google::Apis::VERSION,
|
||||
Google::Apis::OS_VERSION)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,222 @@
|
||||
# Copyright 2015 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.
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'hurley'
|
||||
require 'google/apis/core/multipart'
|
||||
require 'google/apis/core/http_command'
|
||||
require 'google/apis/core/upload'
|
||||
require 'google/apis/core/download'
|
||||
require 'addressable/uri'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Wrapper request for batching multiple calls in a single server request
|
||||
class BatchCommand < HttpCommand
|
||||
BATCH_BOUNDARY = 'RubyApiBatchRequest'.freeze
|
||||
MULTIPART_MIXED = 'multipart/mixed'
|
||||
|
||||
# @param [symbol] method
|
||||
# HTTP method
|
||||
# @param [String,Addressable::URI, Addressable::Template] url
|
||||
# HTTP URL or template
|
||||
def initialize(method, url)
|
||||
super(method, url)
|
||||
@calls = []
|
||||
end
|
||||
|
||||
##
|
||||
# Add a new call to the batch request.
|
||||
#
|
||||
# @param [Google::Apis::Core::HttpCommand] call API Request to add
|
||||
# @yield [result, err] Result & error when response available
|
||||
# @return [Google::Apis::Core::BatchCommand] self
|
||||
def add(call, &block)
|
||||
ensure_valid_command(call)
|
||||
@calls << [call, block]
|
||||
self
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
##
|
||||
# Deconstruct the batch response and process the individual results
|
||||
#
|
||||
# @param [String] content_type
|
||||
# Content type of body
|
||||
# @param [String, #read] body
|
||||
# Response body
|
||||
# @return [Object]
|
||||
# Response object
|
||||
def decode_response_body(content_type, body)
|
||||
m = /.*boundary=(.+)/.match(content_type)
|
||||
if m
|
||||
parts = split_parts(body, m[1])
|
||||
deserializer = CallDeserializer.new
|
||||
parts.each_index do |index|
|
||||
call, callback = @calls[index]
|
||||
begin
|
||||
result = call.process_response(*deserializer.to_http_response(parts[index])) unless call.nil?
|
||||
success(result, &callback)
|
||||
rescue => e
|
||||
error(e, &callback)
|
||||
end
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def split_parts(body, boundary)
|
||||
parts = body.split(/\r?\n?--#{Regexp.escape(boundary)}/)
|
||||
parts[1...-1]
|
||||
end
|
||||
|
||||
# Encode the batch request
|
||||
# @return [void]
|
||||
# @raise [Google::Apis::BatchError] if batch is empty
|
||||
def prepare!
|
||||
fail BatchError, 'Cannot make an empty batch request' if @calls.empty?
|
||||
|
||||
serializer = CallSerializer.new
|
||||
multipart = Multipart.new(boundary: BATCH_BOUNDARY, content_type: MULTIPART_MIXED)
|
||||
@calls.each do |(call, _)|
|
||||
io = serializer.to_upload_io(call)
|
||||
multipart.add_upload(io)
|
||||
end
|
||||
self.body = multipart.assemble
|
||||
|
||||
header[:content_type] = multipart.content_type
|
||||
header[:content_length] = "#{body.length}"
|
||||
super
|
||||
end
|
||||
|
||||
def ensure_valid_command(command)
|
||||
if command.is_a?(Google::Apis::Core::BaseUploadCommand) || command.is_a?(Google::Apis::Core::DownloadCommand)
|
||||
fail Google::Apis::ClientError, 'Can not include media requests in batch'
|
||||
end
|
||||
fail Google::Apis::ClientError, 'Invalid command object' unless command.is_a?(HttpCommand)
|
||||
end
|
||||
end
|
||||
|
||||
# Wrapper request for batching multiple uploads in a single server request
|
||||
class BatchUploadCommand < BatchCommand
|
||||
def ensure_valid_command(command)
|
||||
fail Google::Apis::ClientError, 'Can only include upload commands in batch' \
|
||||
unless command.is_a?(Google::Apis::Core::BaseUploadCommand)
|
||||
end
|
||||
|
||||
def prepare!
|
||||
header['X-Goog-Upload-Protocol'] = 'batch'
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
# Serializes a command for embedding in a multipart batch request
|
||||
# @private
|
||||
class CallSerializer
|
||||
HTTP_CONTENT_TYPE = 'application/http'
|
||||
|
||||
##
|
||||
# Serialize a single batched call for assembling the multipart message
|
||||
#
|
||||
# @param [Google::Apis::Core::HttpCommand] call
|
||||
# the call to serialize.
|
||||
# @return [Hurley::UploadIO]
|
||||
# the serialized request
|
||||
def to_upload_io(call)
|
||||
call.prepare!
|
||||
parts = []
|
||||
parts << build_head(call)
|
||||
parts << build_body(call) unless call.body.nil?
|
||||
length = parts.inject(0) { |a, e| a + e.length }
|
||||
Hurley::UploadIO.new(Hurley::CompositeReadIO.new(length, *parts),
|
||||
HTTP_CONTENT_TYPE,
|
||||
'ruby-api-request')
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def build_head(call)
|
||||
request_head = "#{call.method.to_s.upcase} #{Addressable::URI.parse(call.url).request_uri} HTTP/1.1"
|
||||
call.header.each do |key, value|
|
||||
request_head << sprintf("\r\n%s: %s", key, value)
|
||||
end
|
||||
request_head << sprintf("\r\nHost: %s", call.url.host)
|
||||
request_head << "\r\n\r\n"
|
||||
StringIO.new(request_head)
|
||||
end
|
||||
|
||||
def build_body(call)
|
||||
return nil if call.body.nil?
|
||||
return call.body if call.body.respond_to?(:read)
|
||||
StringIO.new(call.body)
|
||||
end
|
||||
end
|
||||
|
||||
# Deconstructs a raw HTTP response part
|
||||
# @private
|
||||
class CallDeserializer
|
||||
# Convert a single batched response into a BatchedCallResponse object.
|
||||
#
|
||||
# @param [String] call_response
|
||||
# the response to parse.
|
||||
# @return [Array<(Fixnum, Hurley::Header, String)>]
|
||||
# Status, header, and response body.
|
||||
def to_http_response(call_response)
|
||||
_, outer_body = split_header_and_body(call_response)
|
||||
status_line, payload = outer_body.split(/\n/, 2)
|
||||
_, status = status_line.split(' ', 3)
|
||||
|
||||
header, body = split_header_and_body(payload)
|
||||
[status.to_i, header, body]
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Auxiliary method to split the header from the body in an HTTP response.
|
||||
#
|
||||
# @param [String] response
|
||||
# the response to parse.
|
||||
# @return [Array<(Hurley::Header, String)>]
|
||||
# the header and the body, separately.
|
||||
def split_header_and_body(response)
|
||||
header = Hurley::Header.new
|
||||
payload = response.lstrip
|
||||
while payload
|
||||
line, payload = payload.split(/\n/, 2)
|
||||
line.sub!(/\s+\z/, '')
|
||||
break if line.empty?
|
||||
match = /\A([^:]+):\s*/.match(line)
|
||||
fail BatchError, sprintf('Invalid header line in response: %s', line) if match.nil?
|
||||
header[match[1]] = match.post_match
|
||||
end
|
||||
[header, payload]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,94 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'google/apis/core/multipart'
|
||||
require 'google/apis/core/api_command'
|
||||
require 'google/apis/errors'
|
||||
require 'addressable/uri'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Streaming/resumable media download support
|
||||
class DownloadCommand < ApiCommand
|
||||
RANGE_HEADER = 'range'
|
||||
|
||||
# File or IO to write content to
|
||||
# @return [String, File, #write]
|
||||
attr_accessor :download_dest
|
||||
|
||||
# Ensure the download destination is a writable stream.
|
||||
#
|
||||
# @return [void]
|
||||
def prepare!
|
||||
@state = :start
|
||||
@download_url = nil
|
||||
@offset = 0
|
||||
if download_dest.respond_to?(:write)
|
||||
@download_io = download_dest
|
||||
@close_io_on_finish = false
|
||||
elsif download_dest.is_a?(String)
|
||||
@download_io = File.open(download_dest, 'wb')
|
||||
@close_io_on_finish = true
|
||||
else
|
||||
@download_io = StringIO.new('', 'wb')
|
||||
@close_io_on_finish = false
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
# Close IO stream when command done. Only closes the stream if it was opened by the command.
|
||||
def release!
|
||||
@download_io.close if @close_io_on_finish
|
||||
end
|
||||
|
||||
# Execute the upload request once. Overrides the default implementation to handle streaming/chunking
|
||||
# of file content.
|
||||
#
|
||||
# @private
|
||||
# @param [Hurley::Client] client
|
||||
# HTTP client
|
||||
# @yield [result, err] Result or error if block supplied
|
||||
# @return [Object]
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def execute_once(client, &block)
|
||||
client.get(@download_url || url) do |req|
|
||||
apply_request_options(req)
|
||||
if @offset > 0
|
||||
logger.debug { sprintf('Resuming download from offset %d', @offset) }
|
||||
req.header[RANGE_HEADER] = sprintf('bytes=%d-', @offset)
|
||||
end
|
||||
req.on_body do |res, chunk|
|
||||
check_status(res.status_code, chunk) unless res.status_code.nil?
|
||||
logger.debug { sprintf('Writing chunk (%d bytes)', chunk.length) }
|
||||
@offset += chunk.length
|
||||
@download_io.write(chunk)
|
||||
@download_io.flush
|
||||
end
|
||||
end
|
||||
if @close_io_on_finish
|
||||
result = nil
|
||||
else
|
||||
result = @download_io
|
||||
end
|
||||
success(result, &block)
|
||||
rescue => e
|
||||
error(e, rethrow: true, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,44 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Adds to_hash to objects
|
||||
module Hashable
|
||||
# Convert object to hash representation
|
||||
#
|
||||
# @return [Hash]
|
||||
def to_h
|
||||
Hash[instance_variables.map { |k| [k[1..-1].to_sym, Hashable.process_value(instance_variable_get(k))] }]
|
||||
end
|
||||
|
||||
# Recursively serialize an object
|
||||
#
|
||||
# @param [Object] val
|
||||
# @return [Hash]
|
||||
def self.process_value(val)
|
||||
case val
|
||||
when Hash
|
||||
Hash[val.map {|k, v| [k.to_sym, Hashable.process_value(v)] }]
|
||||
when Array
|
||||
val.map{ |v| Hashable.process_value(v) }
|
||||
else
|
||||
val.respond_to?(:to_h) ? val.to_h : val
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,275 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'addressable/uri'
|
||||
require 'addressable/template'
|
||||
require 'google/apis/options'
|
||||
require 'google/apis/errors'
|
||||
require 'retriable'
|
||||
require 'hurley'
|
||||
require 'hurley/addressable'
|
||||
require 'google/apis/core/logging'
|
||||
require 'pp'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Command for HTTP request/response.
|
||||
class HttpCommand
|
||||
include Logging
|
||||
|
||||
RETRIABLE_ERRORS = [Google::Apis::ServerError, Google::Apis::RateLimitError, Google::Apis::TransmissionError]
|
||||
|
||||
# Request options
|
||||
# @return [Google::Apis::RequestOptions]
|
||||
attr_accessor :options
|
||||
|
||||
# HTTP request URL
|
||||
# @return [String, Addressable::URI]
|
||||
attr_accessor :url
|
||||
|
||||
# HTTP headers
|
||||
# @return [Hurley::Header]
|
||||
attr_accessor :header
|
||||
|
||||
# Request body
|
||||
# @return [#read]
|
||||
attr_accessor :body
|
||||
|
||||
# HTTP method
|
||||
# @return [symbol]
|
||||
attr_accessor :method
|
||||
|
||||
# HTTP Client
|
||||
# @return [Hurley::Client]
|
||||
attr_accessor :connection
|
||||
|
||||
# Query params
|
||||
# @return [Hash]
|
||||
attr_accessor :query
|
||||
|
||||
# Path params for URL Template
|
||||
# @return [Hash]
|
||||
attr_accessor :params
|
||||
|
||||
# @param [symbol] method
|
||||
# HTTP method
|
||||
# @param [String,Addressable::URI, Addressable::Template] url
|
||||
# HTTP URL or template
|
||||
# @param [String, #read] body
|
||||
# Request body
|
||||
def initialize(method, url, body: nil)
|
||||
self.options = Google::Apis::RequestOptions.default.dup
|
||||
self.url = url
|
||||
self.url = Addressable::Template.new(url) if url.is_a?(String)
|
||||
self.method = method
|
||||
self.header = Hurley::Header.new
|
||||
self.body = body
|
||||
self.query = {}
|
||||
self.params = {}
|
||||
end
|
||||
|
||||
# Execute the command, retrying as necessary
|
||||
#
|
||||
# @param [Hurley::Client] client
|
||||
# HTTP client
|
||||
# @yield [result, err] Result or error if block supplied
|
||||
# @return [Object]
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def execute(client)
|
||||
prepare!
|
||||
proc = block_given? ? Proc.new : nil
|
||||
begin
|
||||
Retriable.retriable tries: options.retries + 1,
|
||||
base_interval: 1,
|
||||
multiplier: 2,
|
||||
on: RETRIABLE_ERRORS do |try|
|
||||
# This 2nd level retriable only catches auth errors, and supports 1 retry, which allows
|
||||
# auth to be re-attempted without having to retry all sorts of other failures like
|
||||
# NotFound, etc
|
||||
auth_tries = (try == 1 && authorization_refreshable? ? 2 : 1)
|
||||
Retriable.retriable tries: auth_tries,
|
||||
on: [Google::Apis::AuthorizationError],
|
||||
on_retry: proc { |*| refresh_authorization } do
|
||||
return execute_once(client, &proc)
|
||||
end
|
||||
end
|
||||
rescue => e
|
||||
raise e if proc.nil?
|
||||
end
|
||||
ensure
|
||||
release!
|
||||
end
|
||||
|
||||
# Refresh the authorization authorization after a 401 error
|
||||
#
|
||||
# @private
|
||||
# @return [void]
|
||||
def refresh_authorization
|
||||
# Handled implicitly by auth lib, here in case need to override
|
||||
logger.debug('Retrying after authentication failure')
|
||||
end
|
||||
|
||||
# Check if attached credentials can be automatically refreshed
|
||||
# @return [Boolean]
|
||||
def authorization_refreshable?
|
||||
options.authorization.respond_to?(:apply!)
|
||||
end
|
||||
|
||||
# Prepare the request (e.g. calculate headers, serialize data, etc) before sending
|
||||
#
|
||||
# @private
|
||||
# @return [void]
|
||||
def prepare!
|
||||
header.update(options.header) if options && options.header
|
||||
self.url = url.expand(params) if url.is_a?(Addressable::Template)
|
||||
url.query_values = query
|
||||
end
|
||||
|
||||
# Release any resources used by this command
|
||||
# @private
|
||||
# @return [void]
|
||||
def release!
|
||||
end
|
||||
|
||||
# Check the response and either decode body or raise error
|
||||
#
|
||||
# @param [Fixnum] status
|
||||
# HTTP status code of response
|
||||
# @param [Hurley::Header] header
|
||||
# Response headers
|
||||
# @param [String, #read] body
|
||||
# Response body
|
||||
# @return [Object]
|
||||
# Response object
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def process_response(status, header, body)
|
||||
check_status(status, body)
|
||||
decode_response_body(header[:content_type], body)
|
||||
end
|
||||
|
||||
# Check the response and raise error if needed
|
||||
#
|
||||
# @param [Fixnum] status
|
||||
# HTTP status code of response
|
||||
# @param [String] body
|
||||
# HTTP response body
|
||||
# @return [void]
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def check_status(status, body = nil)
|
||||
# TODO: 304 Not Modified depends on context...
|
||||
case status
|
||||
when 200...300
|
||||
nil
|
||||
when 301, 302, 303, 307
|
||||
fail Google::Apis::RedirectError, header[:location]
|
||||
when 401
|
||||
fail Google::Apis::AuthorizationError, body
|
||||
when 304, 400, 402...500
|
||||
fail Google::Apis::ClientError, body
|
||||
when 500...600
|
||||
fail Google::Apis::ServerError, body
|
||||
else
|
||||
logger.warn(sprintf('Encountered unexpected status code %s', status))
|
||||
fail Google::Apis::TransmissionError, body
|
||||
end
|
||||
end
|
||||
|
||||
# Process the actual response body. Intended to be overridden by subclasses
|
||||
#
|
||||
# @param [String] _content_type
|
||||
# Content type of body
|
||||
# @param [String, #read] body
|
||||
# Response body
|
||||
# @return [Object]
|
||||
def decode_response_body(_content_type, body)
|
||||
body
|
||||
end
|
||||
|
||||
# Process a success response
|
||||
# @param [Object] result
|
||||
# Result object
|
||||
# @return [Object] result if no block given
|
||||
# @yield [result, nil] if block given
|
||||
def success(result, &block)
|
||||
logger.debug { sprintf('Success - %s', PP.pp(result, '')) }
|
||||
block.call(result, nil) if block_given?
|
||||
result
|
||||
end
|
||||
|
||||
# Process an error response
|
||||
# @param [StandardError] err
|
||||
# Error object
|
||||
# @param [Boolean] rethrow
|
||||
# True if error should be raised again after handling
|
||||
# @return [void]
|
||||
# @yield [nil, err] if block given
|
||||
# @raise [StandardError] if no block
|
||||
def error(err, rethrow: false, &block)
|
||||
logger.debug { sprintf('Error - %s', PP.pp(err, '')) }
|
||||
err = Google::Apis::TransmissionError.new(err) if err.is_a?(Hurley::ClientError)
|
||||
block.call(nil, err) if block_given?
|
||||
fail err if rethrow || block.nil?
|
||||
end
|
||||
|
||||
# Execute the command once.
|
||||
#
|
||||
# @private
|
||||
# @param [Hurley::Client] client
|
||||
# HTTP client
|
||||
# @yield [result, err] Result or error if block supplied
|
||||
# @return [Object]
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def execute_once(client, &block)
|
||||
body.rewind if body.respond_to?(:rewind)
|
||||
begin
|
||||
logger.debug { sprintf('Sending HTTP %s %s', method, url) }
|
||||
response = client.send(method, url, body) do |req|
|
||||
apply_request_options(req)
|
||||
end
|
||||
logger.debug { response.status_code }
|
||||
logger.debug { response.inspect }
|
||||
response = process_response(response.status_code, response.header, response.body)
|
||||
success(response, &block)
|
||||
rescue => e
|
||||
logger.debug { sprintf('Caught error %s', e) }
|
||||
error(e, rethrow: true, &block)
|
||||
end
|
||||
end
|
||||
|
||||
# Update the request with any specified options.
|
||||
# @param [Hurley::Request] req
|
||||
# HTTP request
|
||||
# @return [void]
|
||||
def apply_request_options(req)
|
||||
if options.authorization.respond_to?(:apply!)
|
||||
options.authorization.apply!(req.header)
|
||||
elsif options.authorization.is_a?(String)
|
||||
req.header[:authorization] = sprintf('Bearer %s', options.authorization)
|
||||
end
|
||||
req.header.update(header)
|
||||
req.options.timeout = options.timeout_sec
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,122 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'representable/json'
|
||||
require 'representable/json/hash'
|
||||
require 'representable/coercion'
|
||||
require 'base64'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Support for serializing hashes + propery value/nil/unset tracking
|
||||
# To be included in representers as a feature.
|
||||
# @private
|
||||
module JsonRepresentationSupport
|
||||
def self.included(base)
|
||||
base.extend(JsonSupport)
|
||||
end
|
||||
|
||||
# @private
|
||||
module JsonSupport
|
||||
# Returns a customized getter function for Representable. Allows
|
||||
# indifferent hash/attribute access.
|
||||
#
|
||||
# @param [String] name Property name
|
||||
# @return [Proc]
|
||||
def getter_fn(name)
|
||||
ivar_name = "@#{name}".to_sym
|
||||
lambda do |_|
|
||||
if respond_to?(:[])
|
||||
self[name] || instance_variable_get(ivar_name)
|
||||
else
|
||||
instance_variable_get(ivar_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a customized function for Representable that checks whether or not
|
||||
# an attribute should be serialized. Allows proper patch semantics by distinguishing
|
||||
# between nil & unset values
|
||||
#
|
||||
# @param [String] name Property name
|
||||
# @return [Proc]
|
||||
def if_fn(name)
|
||||
ivar_name = "@#{name}".to_sym
|
||||
lambda do |opts|
|
||||
if opts[:skip_undefined]
|
||||
if respond_to?(:key?)
|
||||
self.key?(name) || instance_variable_defined?(ivar_name)
|
||||
else
|
||||
instance_variable_defined?(ivar_name)
|
||||
end
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def set_default_options(name, options)
|
||||
if options[:base64]
|
||||
options[:render_filter] = ->(value, _doc, *_args) { Base64.urlsafe_encode64(value) }
|
||||
options[:parse_filter] = ->(fragment, _doc, *_args) { Base64.urlsafe_decode64(fragment) }
|
||||
end
|
||||
options[:render_nil] = true
|
||||
options[:getter] = getter_fn(name)
|
||||
options[:if] = if_fn(name)
|
||||
end
|
||||
|
||||
# Define a single value property
|
||||
#
|
||||
# @param [String] name
|
||||
# Property name
|
||||
# @param [Hash] options
|
||||
def property(name, options = {})
|
||||
set_default_options(name, options)
|
||||
super(name, options)
|
||||
end
|
||||
|
||||
# Define a collection property
|
||||
#
|
||||
# @param [String] name
|
||||
# Property name
|
||||
# @param [Hash] options
|
||||
def collection(name, options = {})
|
||||
set_default_options(name, options)
|
||||
super(name, options)
|
||||
end
|
||||
|
||||
# Define a hash property
|
||||
#
|
||||
# @param [String] name
|
||||
# Property name
|
||||
# @param [Hash] options
|
||||
def hash(name, options)
|
||||
set_default_options(name, options)
|
||||
super(name, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Base decorator for JSON representers
|
||||
#
|
||||
# @see https://github.com/apotonick/representable
|
||||
class JsonRepresentation < Representable::Decorator
|
||||
include Representable::JSON
|
||||
include Representable::Coercion
|
||||
feature JsonRepresentationSupport
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,30 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'google/apis'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Logging support
|
||||
module Logging
|
||||
# Get the logger instance
|
||||
# @return [Logger]
|
||||
def logger
|
||||
Google::Apis.logger
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,173 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'hurley'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Part of a multipart request for holding JSON data
|
||||
#
|
||||
# @private
|
||||
class JsonPart
|
||||
include Hurley::Multipart::Part
|
||||
|
||||
# @return [Fixnum]
|
||||
# Length of part
|
||||
attr_reader :length
|
||||
|
||||
# @param [String] boundary
|
||||
# Multipart boundary
|
||||
# @param [String] value
|
||||
# JSON content
|
||||
def initialize(boundary, value)
|
||||
@part = build_part(boundary, value)
|
||||
@length = @part.bytesize
|
||||
@io = StringIO.new(@part)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Format the part
|
||||
#
|
||||
# @param [String] boundary
|
||||
# Multipart boundary
|
||||
# @param [String] value
|
||||
# JSON content
|
||||
# @return [String]
|
||||
def build_part(boundary, value)
|
||||
part = ''
|
||||
part << "--#{boundary}\r\n"
|
||||
part << "Content-Type: application/json\r\n"
|
||||
part << "\r\n"
|
||||
part << "#{value}\r\n"
|
||||
end
|
||||
end
|
||||
|
||||
# Part of a multipart request for holding arbitrary content. Modified
|
||||
# from Hurley::Multipart::FilePart to remove Content-Disposition
|
||||
#
|
||||
# @private
|
||||
class FilePart
|
||||
include Hurley::Multipart::Part
|
||||
|
||||
# @return [Fixnum]
|
||||
# Length of part
|
||||
attr_reader :length
|
||||
|
||||
# @param [String] boundary
|
||||
# Multipart boundary
|
||||
# @param [Google::Apis::Core::UploadIO] io
|
||||
# IO stream
|
||||
# @param [Hash] header
|
||||
# Additional headers
|
||||
def initialize(boundary, io, header = {})
|
||||
file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path)
|
||||
|
||||
@head = build_head(boundary, io.content_type, file_length,
|
||||
io.respond_to?(:opts) ? io.opts.merge(header) : header)
|
||||
|
||||
@length = @head.bytesize + file_length + FOOT.length
|
||||
@io = Hurley::CompositeReadIO.new(@length, StringIO.new(@head), io, StringIO.new(FOOT))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Construct the header for the part
|
||||
#
|
||||
# @param [String] boundary
|
||||
# Multipart boundary
|
||||
# @param [String] type
|
||||
# Content type for the part
|
||||
# @param [Fixnum] content_len
|
||||
# Length of the part
|
||||
# @param [Hash] header
|
||||
# Headers for the part
|
||||
def build_head(boundary, type, content_len, header)
|
||||
sprintf(HEAD_FORMAT,
|
||||
boundary,
|
||||
content_len.to_i,
|
||||
header[:content_type] || type,
|
||||
header[:content_transfer_encoding] || DEFAULT_TR_ENCODING)
|
||||
end
|
||||
|
||||
DEFAULT_TR_ENCODING = 'binary'.freeze
|
||||
FOOT = "\r\n".freeze
|
||||
HEAD_FORMAT = <<-END
|
||||
--%s\r
|
||||
Content-Length: %d\r
|
||||
Content-Type: %s\r
|
||||
Content-Transfer-Encoding: %s\r
|
||||
\r
|
||||
END
|
||||
end
|
||||
|
||||
# Helper for building multipart requests
|
||||
class Multipart
|
||||
MULTIPART_RELATED = 'multipart/related'
|
||||
DEFAULT_BOUNDARY = 'RubyApiClientMultiPart'
|
||||
|
||||
# @return [String]
|
||||
# Content type header
|
||||
attr_reader :content_type
|
||||
|
||||
# @param [String] content_type
|
||||
# Content type for the multipart request
|
||||
# @param [String] boundary
|
||||
# Part delimiter
|
||||
|
||||
def initialize(content_type: MULTIPART_RELATED, boundary: nil)
|
||||
@parts = []
|
||||
@boundary = boundary || DEFAULT_BOUNDARY
|
||||
@content_type = "#{content_type}; boundary=#{boundary}"
|
||||
end
|
||||
|
||||
# Append JSON data part
|
||||
#
|
||||
# @param [String] body
|
||||
# JSON text
|
||||
# @return [self]
|
||||
def add_json(body)
|
||||
@parts << Google::Apis::Core::JsonPart.new(@boundary, body)
|
||||
self
|
||||
end
|
||||
|
||||
# Append arbitrary data as a part
|
||||
#
|
||||
# @param [Google::Apis::Core::UploadIO] upload_io
|
||||
# IO stream
|
||||
# @return [self]
|
||||
def add_upload(upload_io)
|
||||
@parts << Google::Apis::Core::FilePart.new(@boundary, upload_io)
|
||||
self
|
||||
end
|
||||
|
||||
# Assemble the multipart requests
|
||||
#
|
||||
# @return [IO]
|
||||
# IO stream
|
||||
def assemble
|
||||
@parts << Hurley::Multipart::EpiloguePart.new(@boundary)
|
||||
ios = []
|
||||
len = 0
|
||||
@parts.each do |part|
|
||||
len += part.length
|
||||
ios << part.to_io
|
||||
end
|
||||
Hurley::CompositeReadIO.new(len, *ios)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,275 @@
|
||||
# Copyright 2015 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.
|
||||
|
||||
require 'google/apis/core/multipart'
|
||||
require 'google/apis/core/http_command'
|
||||
require 'google/apis/core/api_command'
|
||||
require 'google/apis/errors'
|
||||
require 'addressable/uri'
|
||||
require 'mime-types'
|
||||
|
||||
module Google
|
||||
module Apis
|
||||
module Core
|
||||
# Extension of Hurley's UploadIO to add length accessor
|
||||
class UploadIO < Hurley::UploadIO
|
||||
OCTET_STREAM_CONTENT_TYPE = 'application/octet-stream'
|
||||
|
||||
# Get the length of the stream
|
||||
# @return [Fixnum]
|
||||
def length
|
||||
io.respond_to?(:length) ? io.length : File.size(local_path)
|
||||
end
|
||||
|
||||
# Create a new instance given a file path
|
||||
# @param [String, File] file_name
|
||||
# Path to file
|
||||
# @param [String] content_type
|
||||
# Optional content type. If nil, will attempt to auto-detect
|
||||
# @return [Google::Apis::Core::UploadIO]
|
||||
def self.from_file(file_name, content_type: nil)
|
||||
if content_type.nil?
|
||||
type = MIME::Types.of(file_name)
|
||||
content_type = type.first.content_type unless type.nil? || type.empty?
|
||||
end
|
||||
new(file_name, content_type || OCTET_STREAM_CONTENT_TYPE)
|
||||
end
|
||||
|
||||
# Wraps an IO stream in UploadIO
|
||||
# @param [#read] io
|
||||
# IO to wrap
|
||||
# @param [String] content_type
|
||||
# Optional content type.
|
||||
# @return [Google::Apis::Core::UploadIO]
|
||||
def self.from_io(io, content_type: OCTET_STREAM_CONTENT_TYPE)
|
||||
new(io, content_type)
|
||||
end
|
||||
end
|
||||
|
||||
# Base upload command. Not intended to be used directly
|
||||
# @private
|
||||
class BaseUploadCommand < ApiCommand
|
||||
UPLOAD_PROTOCOL_HEADER = 'X-Goog-Upload-Protocol'
|
||||
UPLOAD_CONTENT_TYPE_HEADER = 'X-Goog-Upload-Header-Content-Type'
|
||||
UPLOAD_CONTENT_LENGTH = 'X-Goog-Upload-Header-Content-Length'
|
||||
|
||||
# File name or IO containing the content to upload
|
||||
# @return [String, File, #read]
|
||||
attr_accessor :upload_source
|
||||
|
||||
# Content type of the upload material
|
||||
# @return [String]
|
||||
attr_accessor :upload_content_type
|
||||
|
||||
# Content, as UploadIO
|
||||
# @return [Google::Apis::Core::UploadIO]
|
||||
attr_accessor :upload_io
|
||||
|
||||
# Ensure the content is readable and wrapped in an {{Google::Apis::Core::UploadIO}} instance.
|
||||
#
|
||||
# @return [void]
|
||||
# @raise [Google::Apis::ClientError] if upload source is invalid
|
||||
def prepare!
|
||||
super
|
||||
if upload_source.is_a?(IO) || upload_source.is_a?(StringIO)
|
||||
self.upload_io = UploadIO.from_io(upload_source, content_type: upload_content_type)
|
||||
@close_io_on_finish = false
|
||||
elsif upload_source.is_a?(String)
|
||||
self.upload_io = UploadIO.from_file(upload_source, content_type: upload_content_type)
|
||||
@close_io_on_finish = true
|
||||
else
|
||||
fail Google::Apis::ClientError, 'Invalid upload source'
|
||||
end
|
||||
end
|
||||
|
||||
# Close IO stream when command done. Only closes the stream if it was opened by the command.
|
||||
def release!
|
||||
upload_io.close if @close_io_on_finish
|
||||
end
|
||||
end
|
||||
|
||||
# Implementation of the raw upload protocol
|
||||
class RawUploadCommand < BaseUploadCommand
|
||||
RAW_PROTOCOL = 'raw'
|
||||
|
||||
# Ensure the content is readable and wrapped in an {{Google::Apis::Core::UploadIO}} instance.
|
||||
#
|
||||
# @return [void]
|
||||
# @raise [Google::Apis::ClientError] if upload source is invalid
|
||||
def prepare!
|
||||
super
|
||||
self.body = upload_io
|
||||
header[UPLOAD_PROTOCOL_HEADER] = RAW_PROTOCOL
|
||||
header[UPLOAD_CONTENT_TYPE_HEADER] = upload_io.content_type
|
||||
end
|
||||
end
|
||||
|
||||
# Implementation of the multipart upload protocol
|
||||
class MultipartUploadCommand < BaseUploadCommand
|
||||
UPLOAD_BOUNDARY = 'RubyApiClientUpload'
|
||||
MULTIPART_PROTOCOL = 'multipart'
|
||||
MULTIPART_RELATED = 'multipart/related'
|
||||
|
||||
# Encode the multipart request
|
||||
#
|
||||
# @return [void]
|
||||
# @raise [Google::Apis::ClientError] if upload source is invalid
|
||||
def prepare!
|
||||
super
|
||||
@multipart = Multipart.new(boundary: UPLOAD_BOUNDARY, content_type: MULTIPART_RELATED)
|
||||
@multipart.add_json(body)
|
||||
@multipart.add_upload(upload_io)
|
||||
self.body = @multipart.assemble
|
||||
header[:content_type] = @multipart.content_type
|
||||
header[UPLOAD_PROTOCOL_HEADER] = MULTIPART_PROTOCOL
|
||||
end
|
||||
end
|
||||
|
||||
# Implementation of the resumable upload protocol
|
||||
class ResumableUploadCommand < BaseUploadCommand
|
||||
UPLOAD_COMMAND_HEADER = 'X-Goog-Upload-Command'
|
||||
UPLOAD_OFFSET_HEADER = 'X-Goog-Upload-Offset'
|
||||
BYTES_RECEIVED_HEADER = 'X-Goog-Upload-Size-Received'
|
||||
UPLOAD_URL_HEADER = 'X-Goog-Upload-URL'
|
||||
UPLOAD_STATUS_HEADER = 'X-Goog-Upload-Status'
|
||||
STATUS_ACTIVE = 'active'
|
||||
STATUS_FINAL = 'final'
|
||||
STATUS_CANCELLED = 'cancelled'
|
||||
RESUMABLE = 'resumable'
|
||||
START_COMMAND = 'start'
|
||||
QUERY_COMMAND = 'query'
|
||||
UPLOAD_COMMAND = 'upload, finalize'
|
||||
|
||||
# Reset upload to initial state.
|
||||
#
|
||||
# @return [void]
|
||||
# @raise [Google::Apis::ClientError] if upload source is invalid
|
||||
def prepare!
|
||||
@state = :start
|
||||
@upload_url = nil
|
||||
@offset = 0
|
||||
super
|
||||
end
|
||||
|
||||
# Check the to see if the upload is complete or needs to be resumed.
|
||||
#
|
||||
# @param [Fixnum] status
|
||||
# HTTP status code of response
|
||||
# @param [Hurley::Header] header
|
||||
# Response headers
|
||||
# @param [String, #read] body
|
||||
# Response body
|
||||
# @return [Object]
|
||||
# Response object
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def process_response(status, header, body)
|
||||
@offset = Integer(header[BYTES_RECEIVED_HEADER]) if header.key?(BYTES_RECEIVED_HEADER)
|
||||
@upload_url = header[UPLOAD_URL_HEADER] if header.key?(UPLOAD_URL_HEADER)
|
||||
|
||||
upload_status = header[UPLOAD_STATUS_HEADER]
|
||||
logger.debug { sprintf('Upload status %s', upload_status) }
|
||||
if upload_status == STATUS_ACTIVE
|
||||
@state = :active
|
||||
elsif upload_status == STATUS_FINAL
|
||||
@state = :final
|
||||
elsif upload_status == STATUS_CANCELLED
|
||||
@state = :cancelled
|
||||
fail Google::Apis::ClientError, body
|
||||
end
|
||||
super(status, header, body)
|
||||
end
|
||||
|
||||
# Send the start command to initiate the upload
|
||||
#
|
||||
# @param [Hurley::Client] client
|
||||
# HTTP client
|
||||
# @return [Hurley::Response]
|
||||
# @raise [Google::Apis::ServerError] Unable to send the request
|
||||
def send_start_command(client)
|
||||
logger.debug { sprintf('Sending upload start command to %s', url) }
|
||||
client.send(method, url, body) do |req|
|
||||
apply_request_options(req)
|
||||
req.header[UPLOAD_PROTOCOL_HEADER] = RESUMABLE
|
||||
req.header[UPLOAD_COMMAND_HEADER] = START_COMMAND
|
||||
req.header[UPLOAD_CONTENT_LENGTH] = upload_io.length.to_s
|
||||
req.header[UPLOAD_CONTENT_TYPE_HEADER] = upload_io.content_type
|
||||
end
|
||||
rescue => e
|
||||
raise Google::Apis::ServerError, e.message
|
||||
end
|
||||
|
||||
# Query for the status of an incomplete upload
|
||||
#
|
||||
# @param [Hurley::Client] client
|
||||
# HTTP client
|
||||
# @return [Hurley::Response]
|
||||
# @raise [Google::Apis::ServerError] Unable to send the request
|
||||
def send_query_command(client)
|
||||
logger.debug { sprintf('Sending upload query command to %s', @upload_url) }
|
||||
client.post(@upload_url, nil) do |req|
|
||||
apply_request_options(req)
|
||||
req.header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND
|
||||
end
|
||||
end
|
||||
|
||||
# Send the actual content
|
||||
#
|
||||
# @param [Hurley::Client] client
|
||||
# HTTP client
|
||||
# @return [Hurley::Response]
|
||||
# @raise [Google::Apis::ServerError] Unable to send the request
|
||||
def send_upload_command(client)
|
||||
logger.debug { sprintf('Sending upload command to %s', @upload_url) }
|
||||
content = upload_io
|
||||
content.pos = @offset
|
||||
client.post(@upload_url, content) do |req|
|
||||
apply_request_options(req)
|
||||
req.header[UPLOAD_COMMAND_HEADER] = UPLOAD_COMMAND
|
||||
req.header[UPLOAD_OFFSET_HEADER] = @offset.to_s
|
||||
end
|
||||
end
|
||||
|
||||
# Execute the upload request once. This will typically perform two HTTP requests -- one to initiate or query
|
||||
# for the status of the upload, the second to send the (remaining) content.
|
||||
#
|
||||
# @private
|
||||
# @param [Hurley::Client] client
|
||||
# HTTP client
|
||||
# @yield [result, err] Result or error if block supplied
|
||||
# @return [Object]
|
||||
# @raise [Google::Apis::ServerError] An error occurred on the server and the request can be retried
|
||||
# @raise [Google::Apis::ClientError] The request is invalid and should not be retried without modification
|
||||
# @raise [Google::Apis::AuthorizationError] Authorization is required
|
||||
def execute_once(client, &block)
|
||||
if @state == :start
|
||||
response = send_start_command(client)
|
||||
else
|
||||
response = send_query_command(client)
|
||||
end
|
||||
result = process_response(response.status_code, response.header, response.body)
|
||||
if @state == :active
|
||||
response = send_upload_command(client)
|
||||
result = process_response(response.status_code, response.header, response.body)
|
||||
end
|
||||
|
||||
success(result, &block) if @state == :final
|
||||
rescue => e
|
||||
error(e, rethrow: true, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user