override/api_client.rb in clever-ruby-1.2.5 vs override/api_client.rb in clever-ruby-2.0.0
- old
+ new
@@ -1,13 +1,14 @@
=begin
#Clever API
#The Clever API
-OpenAPI spec version: 1.2.0
+OpenAPI spec version: 2.0.0
Generated by: https://github.com/swagger-api/swagger-codegen.git
+Swagger Codegen version: 2.3.0-SNAPSHOT
=end
require 'date'
require 'json'
@@ -120,11 +121,13 @@
if @config.debugging
@config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
end
end
- Typhoeus::Request.new(url, req_opts)
+ request = Typhoeus::Request.new(url, req_opts)
+ download_file(request) if opts[:return_type] == 'File'
+ request
end
# Check if the given MIME is a JSON MIME.
# JSON MIME examples:
# application/json
@@ -141,18 +144,20 @@
#
# @param [Response] response HTTP response
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
def deserialize(response, return_type)
body = response.body
+
+ # handle file downloading - return the File instance processed in request callbacks
+ # note that response body is empty when the file is written in chunks in request on_body callback
+ return @tempfile if return_type == 'File'
+
return nil if body.nil? || body.empty?
# return response body directly for String return type
return body if return_type == 'String'
- # handle file downloading - save response body into a tmp file and return the File instance
- return download_file(response) if return_type == 'File'
-
# ensuring a default content type
content_type = response.headers['Content-Type'] || 'application/json'
fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
@@ -205,11 +210,11 @@
end
when 'EventsResponse'
Clever.const_get(return_type).new.tap do |model|
resp = model.build_from_hash data
resps = []
- data[:data].each do |eventResponse|
+ data[:data].each do |eventResponse|
singleResp = parse_event_response(eventResponse[:data])
resps.push(singleResp)
end
resp.data = resps
return resp
@@ -236,34 +241,42 @@
end
end
# Save response body into a file in (the defined) temporary folder, using the filename
# from the "Content-Disposition" header if provided, otherwise a random filename.
+ # The response body is written to the file in chunks in order to handle files which
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
+ # process can use.
#
# @see Configuration#temp_folder_path
- # @return [Tempfile] the file downloaded
- def download_file(response)
- content_disposition = response.headers['Content-Disposition']
- if content_disposition and content_disposition =~ /filename=/i
- filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
- prefix = sanitize_filename(filename)
- else
- prefix = 'download-'
- end
- prefix = prefix + '-' unless prefix.end_with?('-')
-
+ def download_file(request)
tempfile = nil
- encoding = response.body.encoding
- Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
- file.write(response.body)
- tempfile = file
+ encoding = nil
+ request.on_headers do |response|
+ content_disposition = response.headers['Content-Disposition']
+ if content_disposition and content_disposition =~ /filename=/i
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
+ prefix = sanitize_filename(filename)
+ else
+ prefix = 'download-'
+ end
+ prefix = prefix + '-' unless prefix.end_with?('-')
+ encoding = response.body.encoding
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
+ @tempfile = tempfile
end
- @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
- "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
- "will be deleted automatically with GC. It's also recommended to delete the temp file "\
- "explicitly with `tempfile.delete`"
- tempfile
+ request.on_body do |chunk|
+ chunk.force_encoding(encoding)
+ tempfile.write(chunk)
+ end
+ request.on_complete do |response|
+ tempfile.close
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
+ "explicitly with `tempfile.delete`"
+ end
end
# Sanitize filename by removing path.
# e.g. ../../sun.gif becomes sun.gif
#
@@ -290,10 +303,10 @@
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
header_params['Content-Type'] == 'multipart/form-data'
data = {}
form_params.each do |key, value|
case value
- when File, Array, nil
+ when ::File, ::Array, nil
# let typhoeus handle File, Array and nil parameters
data[key] = value
else
data[key] = value.to_s
end