forked from loafle/openapi-generator-original
Support binary return format only in Ruby faraday client (#11974)
* support binary return format on in ruby faraday client * add option to return binary data * update condition
This commit is contained in:
@@ -71,32 +71,33 @@ module {{moduleName}}
|
||||
{{/isFaraday}}
|
||||
{{#isFaraday}}
|
||||
if return_type == 'File'
|
||||
content_disposition = response.headers['Content-Disposition']
|
||||
if content_disposition && content_disposition =~ /filename=/i
|
||||
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
||||
prefix = sanitize_filename(filename)
|
||||
if @config.return_binary_data == true
|
||||
# return byte stream
|
||||
encoding = body.encoding
|
||||
return @stream.join.force_encoding(encoding)
|
||||
else
|
||||
prefix = 'download-'
|
||||
# return file instead of binary data
|
||||
content_disposition = response.headers['Content-Disposition']
|
||||
if content_disposition && content_disposition =~ /filename=/i
|
||||
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
||||
prefix = sanitize_filename(filename)
|
||||
else
|
||||
prefix = 'download-'
|
||||
end
|
||||
prefix = prefix + '-' unless prefix.end_with?('-')
|
||||
encoding = body.encoding
|
||||
@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||
@tempfile.write(@stream.join.force_encoding(encoding))
|
||||
@tempfile.close
|
||||
@config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
|
||||
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||
"explicitly with `tempfile.delete`"
|
||||
return @tempfile
|
||||
end
|
||||
prefix = prefix + '-' unless prefix.end_with?('-')
|
||||
encoding = body.encoding
|
||||
@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||
@tempfile.write(@stream.join.force_encoding(encoding))
|
||||
@tempfile.close
|
||||
@config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
|
||||
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||
"explicitly with `tempfile.delete`"
|
||||
return @tempfile
|
||||
end
|
||||
{{/isFaraday}}
|
||||
|
||||
# return byte stream for Binary return type
|
||||
if return_type == 'Binary'
|
||||
encoding = body.encoding
|
||||
return @stream.join.force_encoding(encoding)
|
||||
end
|
||||
|
||||
return nil if body.nil? || body.empty?
|
||||
|
||||
# return response body directly for String return type
|
||||
|
||||
@@ -121,6 +121,8 @@ module {{moduleName}}
|
||||
@request_middlewares = []
|
||||
@response_middlewares = []
|
||||
@timeout = 60
|
||||
# return data as binary instead of file
|
||||
@return_binary_data = false
|
||||
{{/isFaraday}}
|
||||
{{^isFaraday}}
|
||||
@verify_ssl = true
|
||||
|
||||
@@ -201,29 +201,30 @@ module Petstore
|
||||
# handle file downloading - return the File instance processed in request callbacks
|
||||
# note that response body is empty when the file is written in chunks in request on_body callback
|
||||
if return_type == 'File'
|
||||
content_disposition = response.headers['Content-Disposition']
|
||||
if content_disposition && content_disposition =~ /filename=/i
|
||||
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
||||
prefix = sanitize_filename(filename)
|
||||
if @config.return_binary_data == true
|
||||
# return byte stream
|
||||
encoding = body.encoding
|
||||
return @stream.join.force_encoding(encoding)
|
||||
else
|
||||
prefix = 'download-'
|
||||
# return file instead of binary data
|
||||
content_disposition = response.headers['Content-Disposition']
|
||||
if content_disposition && content_disposition =~ /filename=/i
|
||||
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
||||
prefix = sanitize_filename(filename)
|
||||
else
|
||||
prefix = 'download-'
|
||||
end
|
||||
prefix = prefix + '-' unless prefix.end_with?('-')
|
||||
encoding = body.encoding
|
||||
@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||
@tempfile.write(@stream.join.force_encoding(encoding))
|
||||
@tempfile.close
|
||||
@config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
|
||||
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||
"explicitly with `tempfile.delete`"
|
||||
return @tempfile
|
||||
end
|
||||
prefix = prefix + '-' unless prefix.end_with?('-')
|
||||
encoding = body.encoding
|
||||
@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||
@tempfile.write(@stream.join.force_encoding(encoding))
|
||||
@tempfile.close
|
||||
@config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
|
||||
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
|
||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||
"explicitly with `tempfile.delete`"
|
||||
return @tempfile
|
||||
end
|
||||
|
||||
# return byte stream for Binary return type
|
||||
if return_type == 'Binary'
|
||||
encoding = body.encoding
|
||||
return @stream.join.force_encoding(encoding)
|
||||
end
|
||||
|
||||
return nil if body.nil? || body.empty?
|
||||
|
||||
@@ -156,6 +156,8 @@ module Petstore
|
||||
@request_middlewares = []
|
||||
@response_middlewares = []
|
||||
@timeout = 60
|
||||
# return data as binary instead of file
|
||||
@return_binary_data = false
|
||||
@debugging = false
|
||||
@inject_format = false
|
||||
@force_ending_format = false
|
||||
|
||||
@@ -217,12 +217,6 @@ module Petstore
|
||||
# note that response body is empty when the file is written in chunks in request on_body callback
|
||||
return @tempfile if return_type == 'File'
|
||||
|
||||
# return byte stream for Binary return type
|
||||
if return_type == 'Binary'
|
||||
encoding = body.encoding
|
||||
return @stream.join.force_encoding(encoding)
|
||||
end
|
||||
|
||||
return nil if body.nil? || body.empty?
|
||||
|
||||
# return response body directly for String return type
|
||||
|
||||
@@ -217,12 +217,6 @@ module XAuthIDAlias
|
||||
# note that response body is empty when the file is written in chunks in request on_body callback
|
||||
return @tempfile if return_type == 'File'
|
||||
|
||||
# return byte stream for Binary return type
|
||||
if return_type == 'Binary'
|
||||
encoding = body.encoding
|
||||
return @stream.join.force_encoding(encoding)
|
||||
end
|
||||
|
||||
return nil if body.nil? || body.empty?
|
||||
|
||||
# return response body directly for String return type
|
||||
|
||||
@@ -216,12 +216,6 @@ module DynamicServers
|
||||
# note that response body is empty when the file is written in chunks in request on_body callback
|
||||
return @tempfile if return_type == 'File'
|
||||
|
||||
# return byte stream for Binary return type
|
||||
if return_type == 'Binary'
|
||||
encoding = body.encoding
|
||||
return @stream.join.force_encoding(encoding)
|
||||
end
|
||||
|
||||
return nil if body.nil? || body.empty?
|
||||
|
||||
# return response body directly for String return type
|
||||
|
||||
@@ -216,12 +216,6 @@ module Petstore
|
||||
# note that response body is empty when the file is written in chunks in request on_body callback
|
||||
return @tempfile if return_type == 'File'
|
||||
|
||||
# return byte stream for Binary return type
|
||||
if return_type == 'Binary'
|
||||
encoding = body.encoding
|
||||
return @stream.join.force_encoding(encoding)
|
||||
end
|
||||
|
||||
return nil if body.nil? || body.empty?
|
||||
|
||||
# return response body directly for String return type
|
||||
|
||||
Reference in New Issue
Block a user