Update homepage in gemspec to point at Github.
[arvados.git] / lib / google / api_client / request.rb
1 # Copyright 2010 Google Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 require 'faraday'
16 require 'faraday/utils'
17 require 'faraday/request/multipart'
18 require 'multi_json'
19 require 'compat/multi_json'
20 require 'addressable/uri'
21 require 'stringio'
22 require 'google/api_client/discovery'
23 require 'google/api_client/logging'
24
25 module Google
26   class APIClient
27
28     ##
29     # Represents an API request.
30     class Request
31       include Google::APIClient::Logging
32       
33       MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
34
35       # @return [Hash] Request parameters
36       attr_reader :parameters
37       # @return [Hash] Additional HTTP headers
38       attr_reader :headers
39       # @return [Google::APIClient::Method] API method to invoke
40       attr_reader :api_method
41       # @return [Google::APIClient::UploadIO] File to upload
42       attr_accessor :media
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
48       attr_accessor :body
49
50       ##
51       # Build a request
52       #
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
62       #   OAuth credentials
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
77
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]
83
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]
88
89         if options[:media]
90           self.initialize_media_upload(options)
91         elsif options[:body]
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])
96         else
97           self.body = ''
98         end
99
100         unless self.api_method
101           self.http_method = options[:http_method] || 'GET'
102           self.uri = options[:uri]
103         end
104       end
105
106       # @!attribute [r] upload_type
107       # @return [String] protocol used for upload
108       def upload_type
109         return self.parameters['uploadType'] || self.parameters['upload_type']
110       end
111
112       # @!attribute http_method
113       # @return [Symbol] HTTP method if invoking a URI
114       def http_method
115         return @http_method ||= self.api_method.http_method.to_s.downcase.to_sym
116       end
117
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
123         else
124           raise TypeError,
125             "Expected String or Symbol, got #{new_http_method.class}."
126         end
127       end
128
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
132         else
133           raise TypeError,
134             "Expected Google::APIClient::Method, got #{new_api_method.class}."
135         end
136       end
137
138       # @!attribute uri
139       # @return [Addressable::URI] URI to send request
140       def uri
141         return @uri ||= self.api_method.generate_uri(self.parameters)
142       end
143
144       def uri=(new_uri)
145         @uri = Addressable::URI.parse(new_uri)
146         @parameters.update(@uri.query_values) unless @uri.query_values.nil?
147       end
148
149
150       # Transmits the request with the given connection
151       #
152       # @api private
153       #
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
158       #
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)
167
168         logger.debug { "#{self.class} Result: #{result.status} #{result.headers}" }
169
170         # Resumamble slightly different than other upload protocols in that it requires at least
171         # 2 requests.
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)
177           end
178         end
179         return result
180       end
181
182       # Convert to an HTTP request. Returns components in order of method, URI,
183       # request headers, and body
184       #
185       # @api private
186       #
187       # @return [Array<(Symbol, Addressable::URI, Hash, [#read,#to_str])>]
188       def to_http_request
189         request = (
190           if self.api_method
191             self.api_method.generate_request(self.parameters, self.body, self.headers)
192           elsif self.uri
193             unless self.parameters.empty?
194               self.uri.query = Addressable::URI.form_encode(self.parameters)
195             end
196             [self.http_method, self.uri.to_s, self.headers, self.body]
197           end)
198         return request
199       end
200
201       ##
202       # Hashified verison of the API request
203       #
204       # @return [Hash]
205       def to_hash
206         options = {}
207         if self.api_method
208           options[:api_method] = self.api_method
209           options[:parameters] = self.parameters
210         else
211           options[:http_method] = self.http_method
212           options[:uri] = self.uri
213         end
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
219         end
220         return options
221       end
222
223       ##
224       # Prepares the request for execution, building a hash of parts
225       # suitable for sending to Faraday::Connection.
226       #
227       # @api private
228       #
229       # @param [Faraday::Connection] connection
230       #   Connection for building the request
231       #
232       # @return [Hash]
233       #   Encoded request
234       def to_env(connection)
235         method, uri, headers, body = self.to_http_request
236         http_request = connection.build_request(method) do |req|
237           req.url(uri.to_s)
238           req.headers.update(headers)
239           req.body = body
240         end
241
242         if self.authorization.respond_to?(:generate_authenticated_request)
243           http_request = self.authorization.generate_authenticated_request(
244             :request => http_request,
245             :connection => connection
246           )
247         end
248
249         request_env = http_request.to_env(connection)
250       end
251
252       ##
253       # Convert HTTP response to an API Result
254       #
255       # @api private
256       #
257       # @param [Faraday::Response] response
258       #   HTTP response
259       #
260       # @return [Google::APIClient::Result]
261       #   Processed API response
262       def process_http_response(response)
263         Result.new(self, response)
264       end
265
266       protected
267
268       ##
269       # Adjust headers & body for media uploads
270       #
271       # @api private
272       #
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
286         when "media"
287           if options[:body] || options[:body_object]
288             raise ArgumentError, "Can not specify body & body object for simple uploads"
289           end
290           self.headers['Content-Type'] ||= self.media.content_type
291           self.body = self.media
292         when "multipart"
293           unless options[:body_object]
294             raise ArgumentError, "Multipart requested but no body object"
295           end
296           metadata = StringIO.new(serialize_body(options[:body_object]))
297           build_multipart([Faraday::UploadIO.new(metadata, 'application/json', 'file.json'), self.media])
298         when "resumable"
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])
305           else
306             self.body = ''
307           end
308         end
309       end
310
311       ##
312       # Assemble a multipart message from a set of parts
313       #
314       # @api private
315       #
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])
330       end
331
332       ##
333       # Serialize body object to JSON
334       #
335       # @api private
336       #
337       # @param [#to_json,#to_hash] body
338       #   object to serialize
339       #
340       # @return [String]
341       #   JSON
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.'
347       end
348
349     end
350   end
351 end