1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 require 'faraday/utils'
18 require 'compat/multi_json'
19 require 'addressable/uri'
21 require 'google/api_client/discovery'
27 MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
29 attr_reader :connection, :parameters, :api_method, :headers
30 attr_accessor :media, :authorization, :body
32 def initialize(options={})
34 self.connection = options[:connection] || Faraday.default_connection
35 self.authorization = options[:authorization]
36 self.api_method = options[:api_method]
38 @parameters = Hash[options[:parameters] || {}]
39 # These parameters are handled differently because they're not
40 # parameters to the API method, but rather to the API system.
41 self.parameters['key'] ||= options[:key] if options[:key]
42 self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip]
44 @headers = Faraday::Utils::Headers.new
45 self.headers.merge!(options[:headers]) if options[:headers]
48 self.initialize_media_upload(options)
50 self.body = options[:body]
51 elsif options[:body_object]
52 self.headers['Content-Type'] ||= 'application/json'
53 self.body = serialize_body(options[:body_object])
58 unless self.api_method
59 self.http_method = options[:http_method] || 'GET'
60 self.uri = options[:uri]
61 unless self.parameters.empty?
62 self.uri.query = Addressable::URI.form_encode(self.parameters)
67 def initialize_media_upload(options)
68 self.media = options[:media]
71 if options[:body] || options[:body_object]
72 raise ArgumentError, "Can not specify body & body object for simple uploads"
74 self.headers['Content-Type'] ||= self.media.content_type
75 self.body = self.media
77 unless options[:body_object]
78 raise ArgumentError, "Multipart requested but no body object"
80 metadata = StringIO.new(serialize_body(options[:body_object]))
81 build_multipart([Faraday::UploadIO.new(metadata, 'application/json', 'file.json'), self.media])
83 file_length = self.media.length
84 self.headers['X-Upload-Content-Type'] = self.media.content_type
85 self.headers['X-Upload-Content-Length'] = file_length.to_s
86 if options[:body_object]
87 self.headers['Content-Type'] ||= 'application/json'
88 self.body = serialize_body(options[:body_object])
95 def build_multipart(parts, mime_type = 'multipart/related', boundary = MULTIPART_BOUNDARY)
97 :request_headers => {'Content-Type' => "#{mime_type};boundary=#{boundary}"},
98 :request => { :boundary => boundary }
100 multipart = Faraday::Request::Multipart.new
101 self.body = multipart.create_multipart(env, parts.map {|part| [nil, part]})
102 self.headers.update(env[:request_headers])
105 def serialize_body(body)
106 return body.to_json if body.respond_to?(:to_json)
107 return MultiJson.dump(options[:body_object].to_hash) if body.respond_to?(:to_hash)
108 raise TypeError, 'Could not convert body object to JSON.' +
109 'Must respond to :to_json or :to_hash.'
113 return self.parameters['uploadType'] || self.parameters['upload_type']
116 def connection=(new_connection)
117 if new_connection.kind_of?(Faraday::Connection)
118 @connection = new_connection
121 "Expected Faraday::Connection, got #{new_connection.class}."
125 def api_method=(new_api_method)
126 if new_api_method.kind_of?(Google::APIClient::Method) ||
127 new_api_method == nil
128 @api_method = new_api_method
131 "Expected Google::APIClient::Method, got #{new_api_method.class}."
136 return @http_method ||= self.api_method.http_method.to_s.downcase.to_sym
139 def http_method=(new_http_method)
140 if new_http_method.kind_of?(Symbol)
141 @http_method = new_http_method.to_s.downcase.to_sym
142 elsif new_http_method.respond_to?(:to_str)
143 @http_method = new_http_method.to_s.downcase.to_sym
146 "Expected String or Symbol, got #{new_http_method.class}."
151 return @uri ||= self.api_method.generate_uri(self.parameters)
155 @uri = Addressable::URI.parse(new_uri)
161 self.connection.build_request(self.http_method) do |req|
162 req.url(self.uri.to_str)
163 req.headers.update(self.headers)
167 self.api_method.generate_request(
168 self.parameters, self.body, self.headers, :connection => self.connection
172 if self.authorization.respond_to?(:generate_authenticated_request)
173 request = self.authorization.generate_authenticated_request(
175 :connection => self.connection
184 options[:api_method] = self.api_method
185 options[:parameters] = self.parameters
187 options[:http_method] = self.http_method
188 options[:uri] = self.uri
190 options[:headers] = self.headers
191 options[:body] = self.body
192 options[:connection] = self.connection
193 options[:media] = self.media
194 unless self.authorization.nil?
195 options[:authorization] = self.authorization
200 def process_response(response)
201 Result.new(self, response)
205 class Reference < Request