2011-07-29 22:07:04 +00:00
|
|
|
# Copyright 2010 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.
|
|
|
|
|
2012-01-27 14:57:57 +00:00
|
|
|
require 'faraday'
|
|
|
|
require 'faraday/utils'
|
2012-01-05 10:05:56 +00:00
|
|
|
require 'multi_json'
|
2012-08-30 09:15:59 +00:00
|
|
|
require 'compat/multi_json'
|
2011-07-29 22:07:04 +00:00
|
|
|
require 'addressable/uri'
|
2012-01-27 14:57:57 +00:00
|
|
|
require 'stringio'
|
2011-07-29 22:07:04 +00:00
|
|
|
require 'google/api_client/discovery'
|
|
|
|
|
2012-06-07 00:27:20 +00:00
|
|
|
# TODO - needs some serious cleanup
|
2011-12-02 13:43:55 +00:00
|
|
|
|
2011-07-29 22:07:04 +00:00
|
|
|
module Google
|
|
|
|
class APIClient
|
|
|
|
class Reference
|
2012-03-02 01:23:36 +00:00
|
|
|
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
|
2012-07-21 10:50:48 +00:00
|
|
|
|
2011-07-29 22:07:04 +00:00
|
|
|
def initialize(options={})
|
|
|
|
|
2012-01-27 14:57:57 +00:00
|
|
|
self.connection = options[:connection] || Faraday.default_connection
|
2012-06-07 00:27:20 +00:00
|
|
|
self.authorization = options[:authorization]
|
2011-07-29 22:07:04 +00:00
|
|
|
self.api_method = options[:api_method]
|
2012-09-05 21:56:52 +00:00
|
|
|
|
2011-07-29 22:07:04 +00:00
|
|
|
self.parameters = options[:parameters] || {}
|
2011-09-21 07:51:51 +00:00
|
|
|
# These parameters are handled differently because they're not
|
|
|
|
# parameters to the API method, but rather to the API system.
|
2012-09-05 21:56:52 +00:00
|
|
|
self.parameters['key'] ||= options[:key] if options[:key]
|
|
|
|
self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip]
|
|
|
|
|
2012-03-02 01:23:36 +00:00
|
|
|
self.headers = options[:headers] || {}
|
|
|
|
if options[:media]
|
2012-09-05 21:56:52 +00:00
|
|
|
self.initialize_media_upload
|
2012-03-02 01:23:36 +00:00
|
|
|
elsif options[:body]
|
2011-07-29 22:07:04 +00:00
|
|
|
self.body = options[:body]
|
2011-08-18 01:42:03 +00:00
|
|
|
elsif options[:body_object]
|
2012-03-02 01:23:36 +00:00
|
|
|
self.headers['Content-Type'] ||= 'application/json'
|
|
|
|
self.body = serialize_body(options[:body_object])
|
2011-07-29 22:07:04 +00:00
|
|
|
else
|
2012-01-27 14:57:57 +00:00
|
|
|
self.body = ''
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
unless self.api_method
|
|
|
|
self.http_method = options[:http_method] || 'GET'
|
|
|
|
self.uri = options[:uri]
|
2011-09-22 10:49:46 +00:00
|
|
|
unless self.parameters.empty?
|
2012-09-05 21:56:52 +00:00
|
|
|
self.uri.query = Addressable::URI.form_encode(self.parameters)
|
2011-09-22 10:49:46 +00:00
|
|
|
end
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
end
|
2012-07-21 10:50:48 +00:00
|
|
|
|
2012-09-05 21:56:52 +00:00
|
|
|
def initialize_media_upload
|
|
|
|
self.media = options[:media]
|
|
|
|
case self.upload_type
|
|
|
|
when "media"
|
|
|
|
if options[:body] || options[:body_object]
|
|
|
|
raise ArgumentError, "Can not specify body & body object for simple uploads"
|
|
|
|
end
|
|
|
|
self.headers['Content-Type'] ||= self.media.content_type
|
|
|
|
self.body = self.media
|
|
|
|
when "multipart"
|
|
|
|
unless options[:body_object]
|
|
|
|
raise ArgumentError, "Multipart requested but no body object"
|
|
|
|
end
|
|
|
|
metadata = StringIO.new(serialize_body(options[:body_object]))
|
|
|
|
env = {
|
|
|
|
:request_headers => {'Content-Type' => "multipart/related;boundary=#{MULTIPART_BOUNDARY}"},
|
|
|
|
:request => { :boundary => MULTIPART_BOUNDARY }
|
|
|
|
}
|
|
|
|
multipart = Faraday::Request::Multipart.new
|
|
|
|
self.body = multipart.create_multipart(env, [
|
|
|
|
[nil,Faraday::UploadIO.new(metadata, 'application/json', 'file.json')],
|
|
|
|
[nil, self.media]])
|
|
|
|
self.headers.update(env[:request_headers])
|
|
|
|
when "resumable"
|
|
|
|
file_length = self.media.length
|
|
|
|
self.headers['X-Upload-Content-Type'] = self.media.content_type
|
|
|
|
self.headers['X-Upload-Content-Length'] = file_length.to_s
|
|
|
|
if options[:body_object]
|
|
|
|
self.headers['Content-Type'] ||= 'application/json'
|
|
|
|
self.body = serialize_body(options[:body_object])
|
|
|
|
else
|
|
|
|
self.body = ''
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Invalid uploadType for media"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-02 01:23:36 +00:00
|
|
|
def serialize_body(body)
|
|
|
|
return body.to_json if body.respond_to?(:to_json)
|
2012-04-24 11:39:25 +00:00
|
|
|
return MultiJson.dump(options[:body_object].to_hash) if body.respond_to?(:to_hash)
|
2012-03-02 01:23:36 +00:00
|
|
|
raise TypeError, 'Could not convert body object to JSON.' +
|
|
|
|
'Must respond to :to_json or :to_hash.'
|
|
|
|
end
|
2012-07-21 10:50:48 +00:00
|
|
|
|
2012-03-02 01:23:36 +00:00
|
|
|
def media
|
|
|
|
return @media
|
|
|
|
end
|
2012-07-21 10:50:48 +00:00
|
|
|
|
2012-03-02 01:23:36 +00:00
|
|
|
def media=(media)
|
|
|
|
@media = (media)
|
|
|
|
end
|
2012-09-05 21:56:52 +00:00
|
|
|
|
|
|
|
def upload_type
|
|
|
|
return self.parameters['uploadType'] || self.parameters['upload_type']
|
|
|
|
end
|
2012-07-21 10:50:48 +00:00
|
|
|
|
2012-06-07 00:27:20 +00:00
|
|
|
def authorization
|
|
|
|
return @authorization
|
|
|
|
end
|
2012-07-21 10:50:48 +00:00
|
|
|
|
2012-06-07 00:27:20 +00:00
|
|
|
def authorization=(new_authorization)
|
|
|
|
@authorization = new_authorization
|
|
|
|
end
|
2012-07-21 10:50:48 +00:00
|
|
|
|
2012-01-27 14:57:57 +00:00
|
|
|
def connection
|
|
|
|
return @connection
|
|
|
|
end
|
|
|
|
|
|
|
|
def connection=(new_connection)
|
|
|
|
if new_connection.kind_of?(Faraday::Connection)
|
|
|
|
@connection = new_connection
|
|
|
|
else
|
|
|
|
raise TypeError,
|
|
|
|
"Expected Faraday::Connection, got #{new_connection.class}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-29 22:07:04 +00:00
|
|
|
def api_method
|
|
|
|
return @api_method
|
|
|
|
end
|
|
|
|
|
|
|
|
def api_method=(new_api_method)
|
|
|
|
if new_api_method.kind_of?(Google::APIClient::Method) ||
|
|
|
|
new_api_method == nil
|
|
|
|
@api_method = new_api_method
|
|
|
|
else
|
|
|
|
raise TypeError,
|
|
|
|
"Expected Google::APIClient::Method, got #{new_api_method.class}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parameters
|
|
|
|
return @parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parameters=(new_parameters)
|
2012-09-05 21:56:52 +00:00
|
|
|
@parameters = Hash[new_parameters]
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
return @body
|
|
|
|
end
|
|
|
|
|
|
|
|
def body=(new_body)
|
2012-09-05 21:56:52 +00:00
|
|
|
@body = new_body
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def headers
|
2012-03-02 01:23:36 +00:00
|
|
|
return @headers ||= {}
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def headers=(new_headers)
|
|
|
|
if new_headers.kind_of?(Array) || new_headers.kind_of?(Hash)
|
|
|
|
@headers = new_headers
|
|
|
|
else
|
|
|
|
raise TypeError, "Expected Hash or Array, got #{new_headers.class}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def http_method
|
|
|
|
return @http_method ||= self.api_method.http_method
|
|
|
|
end
|
|
|
|
|
|
|
|
def http_method=(new_http_method)
|
|
|
|
if new_http_method.kind_of?(Symbol)
|
2012-09-05 21:56:52 +00:00
|
|
|
@http_method = new_http_method.to_s.downcase.to_sym
|
2011-07-29 22:07:04 +00:00
|
|
|
elsif new_http_method.respond_to?(:to_str)
|
2012-09-05 21:56:52 +00:00
|
|
|
@http_method = new_http_method.to_s.downcase.to_sym
|
2011-07-29 22:07:04 +00:00
|
|
|
else
|
|
|
|
raise TypeError,
|
|
|
|
"Expected String or Symbol, got #{new_http_method.class}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uri
|
|
|
|
return @uri ||= self.api_method.generate_uri(self.parameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
def uri=(new_uri)
|
|
|
|
@uri = Addressable::URI.parse(new_uri)
|
|
|
|
end
|
|
|
|
|
2012-09-05 21:56:52 +00:00
|
|
|
def to_http_request
|
|
|
|
request = (
|
|
|
|
if self.api_method
|
|
|
|
self.api_method.generate_request(
|
|
|
|
self.parameters, self.body, self.headers, :connection => self.connection
|
|
|
|
)
|
|
|
|
else
|
|
|
|
self.connection.build_request(self.http_method) do |req|
|
|
|
|
req.url(self.uri.to_str)
|
|
|
|
req.headers.update(self.headers)
|
|
|
|
req.body = self.body
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
if self.authorization.respond_to?(:generate_authenticated_request)
|
|
|
|
request = self.authorization.generate_authenticated_request(
|
|
|
|
:request => request,
|
2012-07-21 10:50:48 +00:00
|
|
|
:connection => self.connection
|
2011-07-29 22:07:04 +00:00
|
|
|
)
|
|
|
|
end
|
2012-09-05 21:56:52 +00:00
|
|
|
return request
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_hash
|
|
|
|
options = {}
|
|
|
|
if self.api_method
|
|
|
|
options[:api_method] = self.api_method
|
|
|
|
options[:parameters] = self.parameters
|
|
|
|
else
|
|
|
|
options[:http_method] = self.http_method
|
|
|
|
options[:uri] = self.uri
|
|
|
|
end
|
|
|
|
options[:headers] = self.headers
|
|
|
|
options[:body] = self.body
|
2012-01-27 14:57:57 +00:00
|
|
|
options[:connection] = self.connection
|
2012-07-31 21:16:26 +00:00
|
|
|
unless self.authorization.nil?
|
|
|
|
options[:authorization] = self.authorization
|
|
|
|
end
|
2011-07-29 22:07:04 +00:00
|
|
|
return options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|