mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 03:18:53 +00:00
Support file downloading in Ruby generator
This commit is contained in:
parent
11e56f5c9c
commit
f3a0f464f7
@ -61,6 +61,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("List", "Array");
|
||||
typeMapping.put("map", "Hash");
|
||||
typeMapping.put("object", "Object");
|
||||
typeMapping.put("file", "File");
|
||||
|
||||
// remove modelPackage and apiPackage added by default
|
||||
cliOptions.clear();
|
||||
|
@ -3,6 +3,7 @@ module {{moduleName}}
|
||||
class Response
|
||||
require 'json'
|
||||
require 'date'
|
||||
require 'tempfile'
|
||||
|
||||
attr_accessor :raw
|
||||
|
||||
@ -31,8 +32,11 @@ module {{moduleName}}
|
||||
def deserialize(return_type)
|
||||
return nil if body.blank?
|
||||
|
||||
# handle file downloading - save response body into a tmp file and return the File instance
|
||||
return download_file if return_type == 'File'
|
||||
|
||||
# ensuring a default content type
|
||||
content_type = raw.headers_hash['Content-Type'] || 'application/json'
|
||||
content_type = raw.headers['Content-Type'] || 'application/json'
|
||||
|
||||
unless content_type.start_with?('application/json')
|
||||
fail "Content-Type is not supported: #{content_type}"
|
||||
@ -82,6 +86,23 @@ module {{moduleName}}
|
||||
end
|
||||
end
|
||||
|
||||
# Save response body into a file in tmp folder, using the filename from the
|
||||
# "Content-Disposition" header if provided, otherwise a random filename.
|
||||
def download_file
|
||||
tmp_file = Tempfile.new ''
|
||||
content_disposition = raw.headers['Content-Disposition']
|
||||
if content_disposition
|
||||
filename = content_disposition[/filename="([^"]+)"/, 1]
|
||||
path = File.join File.dirname(tmp_file), filename
|
||||
else
|
||||
path = tmp_file.path
|
||||
end
|
||||
# close and delete temp file
|
||||
tmp_file.close!
|
||||
File.open(path, 'w') { |file| file.write(raw.body) }
|
||||
return File.new(path)
|
||||
end
|
||||
|
||||
# `headers_hash` is a Typhoeus-specific extension of Hash,
|
||||
# so simplify it back into a regular old Hash.
|
||||
def headers
|
||||
|
@ -286,7 +286,7 @@ module SwaggerClient
|
||||
# @param pet_id ID of pet to update
|
||||
# @param [Hash] opts the optional parameters
|
||||
# @option opts [String] :additional_metadata Additional data to pass to server
|
||||
# @option opts [file] :file file to upload
|
||||
# @option opts [File] :file file to upload
|
||||
# @return [nil]
|
||||
def self.upload_file(pet_id, opts = {})
|
||||
|
||||
|
@ -3,6 +3,7 @@ module SwaggerClient
|
||||
class Response
|
||||
require 'json'
|
||||
require 'date'
|
||||
require 'tempfile'
|
||||
|
||||
attr_accessor :raw
|
||||
|
||||
@ -31,8 +32,11 @@ module SwaggerClient
|
||||
def deserialize(return_type)
|
||||
return nil if body.blank?
|
||||
|
||||
# handle file downloading - save response body into a tmp file and return the File instance
|
||||
return download_file if return_type == 'File'
|
||||
|
||||
# ensuring a default content type
|
||||
content_type = raw.headers_hash['Content-Type'] || 'application/json'
|
||||
content_type = raw.headers['Content-Type'] || 'application/json'
|
||||
|
||||
unless content_type.start_with?('application/json')
|
||||
fail "Content-Type is not supported: #{content_type}"
|
||||
@ -82,6 +86,23 @@ module SwaggerClient
|
||||
end
|
||||
end
|
||||
|
||||
# Save response body into a file in tmp folder, using the filename from the
|
||||
# "Content-Disposition" header if provided, otherwise a random filename.
|
||||
def download_file
|
||||
tmp_file = Tempfile.new ''
|
||||
content_disposition = raw.headers['Content-Disposition']
|
||||
if content_disposition
|
||||
filename = content_disposition[/filename="([^"]+)"/, 1]
|
||||
path = File.join File.dirname(tmp_file), filename
|
||||
else
|
||||
path = tmp_file.path
|
||||
end
|
||||
# close and delete temp file
|
||||
tmp_file.close!
|
||||
File.open(path, 'w') { |file| file.write(raw.body) }
|
||||
return File.new(path)
|
||||
end
|
||||
|
||||
# `headers_hash` is a Typhoeus-specific extension of Hash,
|
||||
# so simplify it back into a regular old Hash.
|
||||
def headers
|
||||
|
Loading…
Reference in New Issue
Block a user