forked from loafle/openapi-generator-original
Add config option for file downloading folder. Log about file downloading
This commit is contained in:
parent
f3a0f464f7
commit
ceafbcc97f
@ -3,6 +3,13 @@ module {{moduleName}}
|
|||||||
class Configuration
|
class Configuration
|
||||||
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
|
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
|
||||||
|
|
||||||
|
# Defines the temporary folder to store downloaded files
|
||||||
|
# (for API endpoints that have file response).
|
||||||
|
# Default to use `Tempfile`.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
attr_accessor :temp_folder_path
|
||||||
|
|
||||||
# Defaults go in here..
|
# Defaults go in here..
|
||||||
def initialize
|
def initialize
|
||||||
@format = 'json'
|
@format = 'json'
|
||||||
|
@ -86,10 +86,13 @@ module {{moduleName}}
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Save response body into a file in tmp folder, using the filename from the
|
# Save response body into a file in (the defined) temporary folder, using the filename
|
||||||
# "Content-Disposition" header if provided, otherwise a random filename.
|
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
||||||
|
#
|
||||||
|
# @see Configuration#temp_folder_path
|
||||||
|
# @return [File] the file downloaded
|
||||||
def download_file
|
def download_file
|
||||||
tmp_file = Tempfile.new ''
|
tmp_file = Tempfile.new '', Swagger.configuration.temp_folder_path
|
||||||
content_disposition = raw.headers['Content-Disposition']
|
content_disposition = raw.headers['Content-Disposition']
|
||||||
if content_disposition
|
if content_disposition
|
||||||
filename = content_disposition[/filename="([^"]+)"/, 1]
|
filename = content_disposition[/filename="([^"]+)"/, 1]
|
||||||
@ -99,7 +102,9 @@ module {{moduleName}}
|
|||||||
end
|
end
|
||||||
# close and delete temp file
|
# close and delete temp file
|
||||||
tmp_file.close!
|
tmp_file.close!
|
||||||
|
|
||||||
File.open(path, 'w') { |file| file.write(raw.body) }
|
File.open(path, 'w') { |file| file.write(raw.body) }
|
||||||
|
Swagger.logger.info "File written to #{path}. Please move the file to a proper folder for further processing and delete the temp afterwards"
|
||||||
return File.new(path)
|
return File.new(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -22,5 +22,6 @@ require '{{importPath}}'
|
|||||||
|
|
||||||
module {{moduleName}}
|
module {{moduleName}}
|
||||||
# Initialize the default configuration
|
# Initialize the default configuration
|
||||||
Swagger.configuration ||= Swagger::Configuration.new
|
Swagger.configuration = Swagger::Configuration.new
|
||||||
|
Swagger.configure { |config| }
|
||||||
end
|
end
|
||||||
|
@ -22,5 +22,6 @@ require 'swagger_client/api/store_api'
|
|||||||
|
|
||||||
module SwaggerClient
|
module SwaggerClient
|
||||||
# Initialize the default configuration
|
# Initialize the default configuration
|
||||||
Swagger.configuration ||= Swagger::Configuration.new
|
Swagger.configuration = Swagger::Configuration.new
|
||||||
|
Swagger.configure { |config| }
|
||||||
end
|
end
|
||||||
|
@ -3,6 +3,13 @@ module SwaggerClient
|
|||||||
class Configuration
|
class Configuration
|
||||||
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
|
attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
|
||||||
|
|
||||||
|
# Defines the temporary folder to store downloaded files
|
||||||
|
# (for API endpoints that have file response).
|
||||||
|
# Default to use `Tempfile`.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
attr_accessor :temp_folder_path
|
||||||
|
|
||||||
# Defaults go in here..
|
# Defaults go in here..
|
||||||
def initialize
|
def initialize
|
||||||
@format = 'json'
|
@format = 'json'
|
||||||
|
@ -86,10 +86,13 @@ module SwaggerClient
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Save response body into a file in tmp folder, using the filename from the
|
# Save response body into a file in (the defined) temporary folder, using the filename
|
||||||
# "Content-Disposition" header if provided, otherwise a random filename.
|
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
||||||
|
#
|
||||||
|
# @see Configuration#temp_folder_path
|
||||||
|
# @return [File] the file downloaded
|
||||||
def download_file
|
def download_file
|
||||||
tmp_file = Tempfile.new ''
|
tmp_file = Tempfile.new '', Swagger.configuration.temp_folder_path
|
||||||
content_disposition = raw.headers['Content-Disposition']
|
content_disposition = raw.headers['Content-Disposition']
|
||||||
if content_disposition
|
if content_disposition
|
||||||
filename = content_disposition[/filename="([^"]+)"/, 1]
|
filename = content_disposition[/filename="([^"]+)"/, 1]
|
||||||
@ -99,7 +102,9 @@ module SwaggerClient
|
|||||||
end
|
end
|
||||||
# close and delete temp file
|
# close and delete temp file
|
||||||
tmp_file.close!
|
tmp_file.close!
|
||||||
|
|
||||||
File.open(path, 'w') { |file| file.write(raw.body) }
|
File.open(path, 'w') { |file| file.write(raw.body) }
|
||||||
|
Swagger.logger.info "File written to #{path}. Please move the file to a proper folder for further processing and delete the temp afterwards"
|
||||||
return File.new(path)
|
return File.new(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user