From ceafbcc97f36e31b4986a38ca10aaac4edd0405d Mon Sep 17 00:00:00 2001 From: xhh Date: Thu, 25 Jun 2015 16:07:02 +0800 Subject: [PATCH] Add config option for file downloading folder. Log about file downloading --- .../resources/ruby/swagger/configuration.mustache | 7 +++++++ .../src/main/resources/ruby/swagger/response.mustache | 11 ++++++++--- .../src/main/resources/ruby/swagger_client.mustache | 3 ++- samples/client/petstore/ruby/lib/swagger_client.rb | 3 ++- .../ruby/lib/swagger_client/swagger/configuration.rb | 7 +++++++ .../ruby/lib/swagger_client/swagger/response.rb | 11 ++++++++--- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache b/modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache index e9a8af9c162..9400bdce6b7 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/swagger/configuration.mustache @@ -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' diff --git a/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache b/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache index 14e276ddf5d..076f93e104f 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache @@ -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 diff --git a/modules/swagger-codegen/src/main/resources/ruby/swagger_client.mustache b/modules/swagger-codegen/src/main/resources/ruby/swagger_client.mustache index 0be00aaec95..5bf57a642dd 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/swagger_client.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/swagger_client.mustache @@ -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 diff --git a/samples/client/petstore/ruby/lib/swagger_client.rb b/samples/client/petstore/ruby/lib/swagger_client.rb index 42380927f82..eff88be2ebd 100644 --- a/samples/client/petstore/ruby/lib/swagger_client.rb +++ b/samples/client/petstore/ruby/lib/swagger_client.rb @@ -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 diff --git a/samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb b/samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb index a2d4fe0e291..0b7836d989c 100644 --- a/samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb +++ b/samples/client/petstore/ruby/lib/swagger_client/swagger/configuration.rb @@ -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' diff --git a/samples/client/petstore/ruby/lib/swagger_client/swagger/response.rb b/samples/client/petstore/ruby/lib/swagger_client/swagger/response.rb index e9c8e3805a0..adabfa21397 100644 --- a/samples/client/petstore/ruby/lib/swagger_client/swagger/response.rb +++ b/samples/client/petstore/ruby/lib/swagger_client/swagger/response.rb @@ -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