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'
17 require 'faraday/request/multipart'
19 require 'compat/multi_json'
20 require 'addressable/uri'
22 require 'google/api_client/discovery'
23 require 'google/api_client/logging'
29 # Represents an API request.
31 include Google::APIClient::Logging
33 MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
35 # @return [Hash] Request parameters
36 attr_reader :parameters
37 # @return [Hash] Additional HTTP headers
39 # @return [Google::APIClient::Method] API method to invoke
40 attr_reader :api_method
41 # @return [Google::APIClient::UploadIO] File to upload
43 # @return [#generated_authenticated_request] User credentials
44 attr_accessor :authorization
45 # @return [TrueClass,FalseClass] True if request should include credentials
46 attr_accessor :authenticated
47 # @return [#read, #to_str] Request body
53 # @param [Hash] options
54 # @option options [Hash, Array] :parameters
55 # Request parameters for the API method.
56 # @option options [Google::APIClient::Method] :api_method
57 # API method to invoke. Either :api_method or :uri must be specified
58 # @option options [TrueClass, FalseClass] :authenticated
59 # True if request should include credentials. Implicitly true if
60 # unspecified and :authorization present
61 # @option options [#generate_signed_request] :authorization
63 # @option options [Google::APIClient::UploadIO] :media
64 # File to upload, if media upload request
65 # @option options [#to_json, #to_hash] :body_object
66 # Main body of the API request. Typically hash or object that can
67 # be serialized to JSON
68 # @option options [#read, #to_str] :body
69 # Raw body to send in POST/PUT requests
70 # @option options [String, Addressable::URI] :uri
71 # URI to request. Either :api_method or :uri must be specified
72 # @option options [String, Symbol] :http_method
73 # HTTP method when requesting a URI
74 def initialize(options={})
75 @parameters = Faraday::Utils::ParamsHash.new
76 @headers = Faraday::Utils::Headers.new
78 self.parameters.merge!(options[:parameters]) unless options[:parameters].nil?
79 self.headers.merge!(options[:headers]) unless options[:headers].nil?
80 self.api_method = options[:api_method]
81 self.authenticated = options[:authenticated]
82 self.authorization = options[:authorization]
84 # These parameters are handled differently because they're not
85 # parameters to the API method, but rather to the API system.
86 self.parameters['key'] ||= options[:key] if options[:key]
87 self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip]
90 self.initialize_media_upload(options)
92 self.body = options[:body]
93 elsif options[:body_object]
94 self.headers['Content-Type'] ||= 'application/json'
95 self.body = serialize_body(options[:body_object])
100 unless self.api_method
101 self.http_method = options[:http_method] || 'GET'
102 self.uri = options[:uri]
106 # @!attribute [r] upload_type
107 # @return [String] protocol used for upload
109 return self.parameters['uploadType'] || self.parameters['upload_type']
112 # @!attribute http_method
113 # @return [Symbol] HTTP method if invoking a URI
115 return @http_method ||= self.api_method.http_method.to_s.downcase.to_sym
118 def http_method=(new_http_method)
119 if new_http_method.kind_of?(Symbol)
120 @http_method = new_http_method.to_s.downcase.to_sym
121 elsif new_http_method.respond_to?(:to_str)
122 @http_method = new_http_method.to_s.downcase.to_sym
125 "Expected String or Symbol, got #{new_http_method.class}."
129 def api_method=(new_api_method)
130 if new_api_method.nil? || new_api_method.kind_of?(Google::APIClient::Method)
131 @api_method = new_api_method
134 "Expected Google::APIClient::Method, got #{new_api_method.class}."
139 # @return [Addressable::URI] URI to send request
141 return @uri ||= self.api_method.generate_uri(self.parameters)
145 @uri = Addressable::URI.parse(new_uri)
146 @parameters.update(@uri.query_values) unless @uri.query_values.nil?
150 # Transmits the request with the given connection
154 # @param [Faraday::Connection] connection
155 # the connection to transmit with
156 # @param [TrueValue,FalseValue] is_retry
157 # True if request has been previous sent
159 # @return [Google::APIClient::Result]
160 # result of API request
161 def send(connection, is_retry = false)
162 self.body.rewind if is_retry && self.body.respond_to?(:rewind)
163 env = self.to_env(connection)
164 logger.debug { "#{self.class} Sending API request #{env[:method]} #{env[:url].to_s} #{env[:request_headers]}" }
165 http_response = connection.app.call(env)
166 result = self.process_http_response(http_response)
168 logger.debug { "#{self.class} Result: #{result.status} #{result.headers}" }
170 # Resumamble slightly different than other upload protocols in that it requires at least
172 if result.status == 200 && self.upload_type == 'resumable'
173 upload = result.resumable_upload
174 unless upload.complete?
175 logger.debug { "#{self.class} Sending upload body" }
176 result = upload.send(connection)
182 # Convert to an HTTP request. Returns components in order of method, URI,
183 # request headers, and body
187 # @return [Array<(Symbol, Addressable::URI, Hash, [#read,#to_str])>]
191 self.api_method.generate_request(self.parameters, self.body, self.headers)
193 unless self.parameters.empty?
194 self.uri.query = Addressable::URI.form_encode(self.parameters)
196 [self.http_method, self.uri.to_s, self.headers, self.body]
202 # Hashified verison of the API request
208 options[:api_method] = self.api_method
209 options[:parameters] = self.parameters
211 options[:http_method] = self.http_method
212 options[:uri] = self.uri
214 options[:headers] = self.headers
215 options[:body] = self.body
216 options[:media] = self.media
217 unless self.authorization.nil?
218 options[:authorization] = self.authorization
224 # Prepares the request for execution, building a hash of parts
225 # suitable for sending to Faraday::Connection.
229 # @param [Faraday::Connection] connection
230 # Connection for building the request
234 def to_env(connection)
235 method, uri, headers, body = self.to_http_request
236 http_request = connection.build_request(method) do |req|
238 req.headers.update(headers)
242 if self.authorization.respond_to?(:generate_authenticated_request)
243 http_request = self.authorization.generate_authenticated_request(
244 :request => http_request,
245 :connection => connection
249 request_env = http_request.to_env(connection)
253 # Convert HTTP response to an API Result
257 # @param [Faraday::Response] response
260 # @return [Google::APIClient::Result]
261 # Processed API response
262 def process_http_response(response)
263 Result.new(self, response)
269 # Adjust headers & body for media uploads
273 # @param [Hash] options
274 # @option options [Hash, Array] :parameters
275 # Request parameters for the API method.
276 # @option options [Google::APIClient::UploadIO] :media
277 # File to upload, if media upload request
278 # @option options [#to_json, #to_hash] :body_object
279 # Main body of the API request. Typically hash or object that can
280 # be serialized to JSON
281 # @option options [#read, #to_str] :body
282 # Raw body to send in POST/PUT requests
283 def initialize_media_upload(options)
284 self.media = options[:media]
285 case self.upload_type
287 if options[:body] || options[:body_object]
288 raise ArgumentError, "Can not specify body & body object for simple uploads"
290 self.headers['Content-Type'] ||= self.media.content_type
291 self.body = self.media
293 unless options[:body_object]
294 raise ArgumentError, "Multipart requested but no body object"
296 metadata = StringIO.new(serialize_body(options[:body_object]))
297 build_multipart([Faraday::UploadIO.new(metadata, 'application/json', 'file.json'), self.media])
299 file_length = self.media.length
300 self.headers['X-Upload-Content-Type'] = self.media.content_type
301 self.headers['X-Upload-Content-Length'] = file_length.to_s
302 if options[:body_object]
303 self.headers['Content-Type'] ||= 'application/json'
304 self.body = serialize_body(options[:body_object])
312 # Assemble a multipart message from a set of parts
316 # @param [Array<[#read,#to_str]>] parts
317 # Array of parts to encode.
318 # @param [String] mime_type
319 # MIME type of the message
320 # @param [String] boundary
321 # Boundary for separating each part of the message
322 def build_multipart(parts, mime_type = 'multipart/related', boundary = MULTIPART_BOUNDARY)
323 env = Faraday::Env.new
324 env.request = Faraday::RequestOptions.new
325 env.request.boundary = boundary
326 env.request_headers = {'Content-Type' => "#{mime_type};boundary=#{boundary}"}
327 multipart = Faraday::Request::Multipart.new
328 self.body = multipart.create_multipart(env, parts.map {|part| [nil, part]})
329 self.headers.update(env[:request_headers])
333 # Serialize body object to JSON
337 # @param [#to_json,#to_hash] body
338 # object to serialize
342 def serialize_body(body)
343 return body.to_json if body.respond_to?(:to_json)
344 return MultiJson.dump(body.to_hash) if body.respond_to?(:to_hash)
345 raise TypeError, 'Could not convert body object to JSON.' +
346 'Must respond to :to_json or :to_hash.'