Add config option for file downloading folder. Log about file downloading

This commit is contained in:
xhh 2015-06-25 16:07:02 +08:00
parent f3a0f464f7
commit ceafbcc97f
6 changed files with 34 additions and 8 deletions

View File

@ -3,6 +3,13 @@ module {{moduleName}}
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
# 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..
def initialize
@format = 'json'

View File

@ -86,10 +86,13 @@ 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.
# 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.
#
# @see Configuration#temp_folder_path
# @return [File] the file downloaded
def download_file
tmp_file = Tempfile.new ''
tmp_file = Tempfile.new '', Swagger.configuration.temp_folder_path
content_disposition = raw.headers['Content-Disposition']
if content_disposition
filename = content_disposition[/filename="([^"]+)"/, 1]
@ -99,7 +102,9 @@ module {{moduleName}}
end
# close and delete temp file
tmp_file.close!
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)
end

View File

@ -22,5 +22,6 @@ require '{{importPath}}'
module {{moduleName}}
# Initialize the default configuration
Swagger.configuration ||= Swagger::Configuration.new
Swagger.configuration = Swagger::Configuration.new
Swagger.configure { |config| }
end

View File

@ -22,5 +22,6 @@ require 'swagger_client/api/store_api'
module SwaggerClient
# Initialize the default configuration
Swagger.configuration ||= Swagger::Configuration.new
Swagger.configuration = Swagger::Configuration.new
Swagger.configure { |config| }
end

View File

@ -3,6 +3,13 @@ module SwaggerClient
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
# 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..
def initialize
@format = 'json'

View File

@ -86,10 +86,13 @@ 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.
# 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.
#
# @see Configuration#temp_folder_path
# @return [File] the file downloaded
def download_file
tmp_file = Tempfile.new ''
tmp_file = Tempfile.new '', Swagger.configuration.temp_folder_path
content_disposition = raw.headers['Content-Disposition']
if content_disposition
filename = content_disposition[/filename="([^"]+)"/, 1]
@ -99,7 +102,9 @@ module SwaggerClient
end
# close and delete temp file
tmp_file.close!
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)
end