Continue internal shuffling...
[arvados.git] / lib / google / api_client / batch.rb
1 # Copyright 2012 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 'addressable/uri'
16 require 'google/api_client/reference'
17 require 'uuidtools'
18
19 module Google
20   class APIClient
21
22     # Helper class to contain a response to an individual batched call.
23     class BatchedCallResponse
24       attr_reader :call_id
25       attr_accessor :status, :headers, :body
26
27       def initialize(call_id, status = nil, headers = nil, body = nil)
28         @call_id, @status, @headers, @body = call_id, status, headers, body
29       end
30     end
31     
32     ##
33     # Wraps multiple API calls into a single over-the-wire HTTP request.
34     class BatchRequest < Request
35       BATCH_BOUNDARY = "-----------RubyApiBatchRequest".freeze
36
37       attr_reader :calls, :callbacks
38
39       ##
40       # Creates a new batch request.
41       #
42       # @param [Hash] options
43       #   Set of options for this request
44       # @param [Proc] block
45       #   Callback for every call's response. Won't be called if a call defined
46       #   a callback of its own.
47       #
48       # @return [Google::APIClient::BatchRequest] The constructed object.
49       def initialize(options = {}, &block)
50         @calls = []
51         @global_callback = block if block_given?
52         @last_auto_id = 0
53         
54         # TODO(sgomes): Use SecureRandom.uuid, drop UUIDTools when we drop 1.8
55         @base_id = UUIDTools::UUID.random_create.to_s
56
57         options[:uri] ||= 'https://www.googleapis.com/batch'
58         options[:http_method] ||= 'POST'
59
60         super options
61       end
62
63       ##
64       # Add a new call to the batch request.
65       # Each call must have its own call ID; if not provided, one will
66       # automatically be generated, avoiding collisions. If duplicate call IDs
67       # are provided, an error will be thrown.
68       #
69       # @param [Hash, Google::APIClient::Request] call: the call to be added.
70       # @param [String] call_id: the ID to be used for this call. Must be unique
71       # @param [Proc] block: callback for this call's response.
72       #
73       # @return [Google::APIClient::BatchRequest] The BatchRequest, for chaining
74       def add(call, call_id = nil, &block)
75         unless call.kind_of?(Google::APIClient::Reference)
76           call = Google::APIClient::Reference.new(call)
77         end
78         call_id ||= new_id
79         if @calls.assoc(call_id)
80           raise BatchError,
81               'A call with this ID already exists: %s' % call_id
82         end
83         callback = block_given? ? block : @global_callback
84         @calls << [call_id, call, callback]        
85         return self
86       end
87
88       ##
89       # Processes the HTTP response to the batch request, issuing callbacks.
90       #
91       # @param [Faraday::Response] response: the HTTP response.
92       def process_http_response(response)
93         content_type = find_header('Content-Type', response.headers)
94         boundary = /.*boundary=(.+)/.match(content_type)[1]
95         parts = response.body.split(/--#{Regexp.escape(boundary)}/)
96         parts = parts[1...-1]
97         parts.each do |part|
98           call_response = deserialize_call_response(part)
99           _, call, callback = @calls.assoc(call_response.call_id)
100           result = Google::APIClient::Result.new(call, call_response)
101           callback.call(result) if callback
102         end
103       end
104
105       ##
106       # Return the request body for the BatchRequest's HTTP request.
107       #
108       # @return [String] The request body.
109       def to_http_request
110         if @calls.nil? || @calls.empty?
111           raise BatchError, 'Cannot make an empty batch request'
112         end
113         parts = @calls.map {|(call_id, call, callback)| serialize_call(call_id, call)}
114         build_multipart(parts, 'multipart/mixed', BATCH_BOUNDARY)
115         super
116       end
117       
118       
119       protected
120
121       ##
122       # Helper method to find a header from its name, regardless of case.
123       #
124       # @param [String] name: The name of the header to find.
125       # @param [Hash] headers: The hash of headers and their values.
126       #
127       # @return [String] The value of the desired header.
128       def find_header(name, headers)
129         _, header = headers.detect do |h, v|
130           h.downcase == name.downcase
131         end
132         return header
133       end
134
135       ##
136       # Create a new call ID. Uses an auto-incrementing, conflict-avoiding ID.
137       #
138       # @return [String] the new, unique ID.
139       def new_id
140         @last_auto_id += 1
141         while @calls.assoc(@last_auto_id)
142           @last_auto_id += 1
143         end
144         return @last_auto_id.to_s
145       end
146
147   
148
149       ##
150       # Convert a Content-ID header value to an id. Presumes the Content-ID
151       # header conforms to the format that id_to_header() returns.
152       #
153       # @param [String] header: Content-ID header value.
154       #
155       # @return [String] The extracted ID value.
156       def header_to_id(header)
157         if !header.start_with?('<') || !header.end_with?('>') ||
158             !header.include?('+')
159           raise BatchError, 'Invalid value for Content-ID: "%s"' % header
160         end
161
162         base, call_id = header[1...-1].split('+')
163         return Addressable::URI.unencode(call_id)
164       end
165
166       ##
167       # Auxiliary method to split the headers from the body in an HTTP response.
168       #
169       # @param [String] response: the response to parse.
170       #
171       # @return [Array<Hash>, String] The headers and the body, separately.
172       def split_headers_and_body(response)
173         headers = {}
174         payload = response.lstrip
175         while payload
176           line, payload = payload.split("\n", 2)
177           line.sub!(/\s+\z/, '')
178           break if line.empty?
179           match = /\A([^:]+):\s*/.match(line)
180           if match
181             headers[match[1]] = match.post_match
182           else
183             raise BatchError, 'Invalid header line in response: %s' % line
184           end
185         end
186         return headers, payload
187       end
188
189       ##
190       # Convert a single batched response into a BatchedCallResponse object.
191       #
192       # @param [Google::APIClient::Reference] response:
193       #   the request to deserialize.
194       #
195       # @return [BatchedCallResponse] The parsed and converted response.
196       def deserialize_call_response(call_response)
197         outer_headers, outer_body = split_headers_and_body(call_response)
198         status_line, payload = outer_body.split("\n", 2)
199         protocol, status, reason = status_line.split(' ', 3)
200
201         headers, body = split_headers_and_body(payload)
202         content_id = find_header('Content-ID', outer_headers)
203         call_id = header_to_id(content_id)
204         return BatchedCallResponse.new(call_id, status.to_i, headers, body)
205       end
206
207       ##
208       # Convert a single batched call into a string.
209       #
210       # @param [Google::APIClient::Reference] call: the call to serialize.
211       #
212       # @return [StringIO] The request as a string in application/http format.
213       def serialize_call(call_id, call)
214         call.api_client = self.api_client
215         method, uri, headers, body = call.to_http_request
216         request = "#{method.to_s.upcase} #{Addressable::URI.parse(uri).path} HTTP/1.1"
217         headers.each do |header, value|
218           request << "\r\n%s: %s" % [header, value]
219         end
220         if body
221           # TODO - CompositeIO if body is a stream 
222           request << "\r\n\r\n"
223           if body.respond_to?(:read)
224             request << body.read
225           else
226             request << body.to_s
227           end
228         end
229         Faraday::UploadIO.new(StringIO.new(request), 'application/http', 'ruby-api-request', 'Content-ID' => id_to_header(call_id))
230       end
231       
232       ##
233       # Convert an id to a Content-ID header value.
234       #
235       # @param [String] call_id: identifier of individual call.
236       #
237       # @return [String]
238       #   A Content-ID header with the call_id encoded into it. A UUID is
239       #   prepended to the value because Content-ID headers are supposed to be
240       #   universally unique.
241       def id_to_header(call_id)
242         return '<%s+%s>' % [@base_id, Addressable::URI.encode(call_id)]
243       end
244       
245     end
246   end
247 end