2015-04-17 00:28:38 +00:00
|
|
|
# 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
|
|
|
|
# Part of a multipart request for holding JSON data
|
|
|
|
#
|
|
|
|
# @private
|
|
|
|
class JsonPart
|
|
|
|
|
|
|
|
# @param [String] value
|
|
|
|
# JSON content
|
2016-08-17 20:51:09 +00:00
|
|
|
# @param [Hash] header
|
|
|
|
# Additional headers
|
|
|
|
def initialize(value, header = {})
|
|
|
|
@value = value
|
|
|
|
@header = header
|
2015-04-17 00:28:38 +00:00
|
|
|
end
|
|
|
|
|
2016-08-17 20:51:09 +00:00
|
|
|
def to_io(boundary)
|
2015-04-17 00:28:38 +00:00
|
|
|
part = ''
|
|
|
|
part << "--#{boundary}\r\n"
|
|
|
|
part << "Content-Type: application/json\r\n"
|
2016-08-17 20:51:09 +00:00
|
|
|
@header.each do |(k, v)|
|
|
|
|
part << "#{k}: #{v}\r\n"
|
|
|
|
end
|
2015-04-17 00:28:38 +00:00
|
|
|
part << "\r\n"
|
2016-08-17 20:51:09 +00:00
|
|
|
part << "#{@value}\r\n"
|
|
|
|
StringIO.new(part)
|
2015-04-17 00:28:38 +00:00
|
|
|
end
|
2016-08-17 20:51:09 +00:00
|
|
|
|
2015-04-17 00:28:38 +00:00
|
|
|
end
|
|
|
|
|
2016-08-17 20:51:09 +00:00
|
|
|
# Part of a multipart request for holding arbitrary content.
|
2015-04-17 00:28:38 +00:00
|
|
|
#
|
|
|
|
# @private
|
|
|
|
class FilePart
|
2016-08-17 20:51:09 +00:00
|
|
|
# @param [IO] io
|
2015-04-17 00:28:38 +00:00
|
|
|
# IO stream
|
|
|
|
# @param [Hash] header
|
|
|
|
# Additional headers
|
2016-08-17 20:51:09 +00:00
|
|
|
def initialize(io, header = {})
|
|
|
|
@io = io
|
|
|
|
@header = header
|
|
|
|
@length = io.respond_to?(:size) ? io.size : nil
|
2015-04-17 00:28:38 +00:00
|
|
|
end
|
|
|
|
|
2016-08-17 20:51:09 +00:00
|
|
|
def to_io(boundary)
|
|
|
|
head = ''
|
|
|
|
head << "--#{boundary}\r\n"
|
|
|
|
@header.each do |(k, v)|
|
|
|
|
head << "#{k}: #{v}\r\n"
|
2015-10-19 22:36:24 +00:00
|
|
|
end
|
2016-08-17 20:51:09 +00:00
|
|
|
head << "Content-Length: #{@length}\r\n" unless @length.nil?
|
|
|
|
head << "Content-Transfer-Encoding: binary\r\n"
|
|
|
|
head << "\r\n"
|
|
|
|
Google::Apis::Core::CompositeIO.new(StringIO.new(head), @io, StringIO.new("\r\n"))
|
2015-04-17 00:28:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Helper for building multipart requests
|
|
|
|
class Multipart
|
|
|
|
MULTIPART_RELATED = 'multipart/related'
|
|
|
|
|
|
|
|
# @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 = []
|
2016-08-17 20:51:09 +00:00
|
|
|
@boundary = boundary || Digest::SHA1.hexdigest(SecureRandom.random_bytes(8))
|
|
|
|
@content_type = "#{content_type}; boundary=#{@boundary}"
|
2015-04-17 00:28:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Append JSON data part
|
|
|
|
#
|
|
|
|
# @param [String] body
|
|
|
|
# JSON text
|
2015-10-19 22:36:24 +00:00
|
|
|
# @param [String] content_id
|
|
|
|
# Optional unique ID of this part
|
2015-04-17 00:28:38 +00:00
|
|
|
# @return [self]
|
2015-10-19 22:36:24 +00:00
|
|
|
def add_json(body, content_id: nil)
|
2016-08-17 20:51:09 +00:00
|
|
|
header = {}
|
|
|
|
header['Content-ID'] = content_id unless content_id.nil?
|
|
|
|
@parts << Google::Apis::Core::JsonPart.new(body, header).to_io(@boundary)
|
2015-04-17 00:28:38 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# Append arbitrary data as a part
|
|
|
|
#
|
2016-08-17 20:51:09 +00:00
|
|
|
# @param [IO] upload_io
|
2015-04-17 00:28:38 +00:00
|
|
|
# IO stream
|
2015-10-19 22:36:24 +00:00
|
|
|
# @param [String] content_id
|
|
|
|
# Optional unique ID of this part
|
2015-04-17 00:28:38 +00:00
|
|
|
# @return [self]
|
2016-08-17 20:51:09 +00:00
|
|
|
def add_upload(upload_io, content_type: nil, content_id: nil)
|
|
|
|
header = {
|
|
|
|
'Content-Type' => content_type || 'application/octet-stream'
|
|
|
|
}
|
|
|
|
header['Content-Id'] = content_id unless content_id.nil?
|
|
|
|
@parts << Google::Apis::Core::FilePart.new(upload_io,
|
|
|
|
header).to_io(@boundary)
|
2015-04-17 00:28:38 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# Assemble the multipart requests
|
|
|
|
#
|
|
|
|
# @return [IO]
|
|
|
|
# IO stream
|
|
|
|
def assemble
|
2016-08-17 20:51:09 +00:00
|
|
|
@parts << StringIO.new("--#{@boundary}--\r\n\r\n")
|
|
|
|
Google::Apis::Core::CompositeIO.new(*@parts)
|
2015-04-17 00:28:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-17 20:51:09 +00:00
|
|
|
end
|