From f3a0f464f792e04694c606c9303f813101471752 Mon Sep 17 00:00:00 2001 From: xhh Date: Thu, 25 Jun 2015 15:35:48 +0800 Subject: [PATCH] Support file downloading in Ruby generator --- .../codegen/languages/RubyClientCodegen.java | 1 + .../resources/ruby/swagger/response.mustache | 23 ++++++++++++++++++- .../ruby/lib/swagger_client/api/pet_api.rb | 2 +- .../lib/swagger_client/swagger/response.rb | 23 ++++++++++++++++++- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java index c3c7871068..3e1929b748 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java @@ -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(); 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 b621110935..14e276ddf5 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/swagger/response.mustache @@ -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 diff --git a/samples/client/petstore/ruby/lib/swagger_client/api/pet_api.rb b/samples/client/petstore/ruby/lib/swagger_client/api/pet_api.rb index 4a421faef8..f20f89bc58 100644 --- a/samples/client/petstore/ruby/lib/swagger_client/api/pet_api.rb +++ b/samples/client/petstore/ruby/lib/swagger_client/api/pet_api.rb @@ -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 = {}) 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 f560006de6..e9c8e3805a 100644 --- a/samples/client/petstore/ruby/lib/swagger_client/swagger/response.rb +++ b/samples/client/petstore/ruby/lib/swagger_client/swagger/response.rb @@ -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