Skip to content

Ruby fix typhoeus api client multiple call with file return type #20615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# the data deserialized from response body (may be a Tempfile or nil), response status code and response headers.
def call_api(http_method, path, opts = {})
request = build_request(http_method, path, opts)
tempfile = download_file(request) if opts[:return_type] == 'File'
tempfile = nil
(download_file(request) { tempfile = _1 }) if opts[:return_type] == 'File'
response = request.run

if @config.debugging
Expand Down Expand Up @@ -145,17 +146,15 @@
chunk.force_encoding(encoding)
tempfile.write(chunk)
end
# run the request to ensure the tempfile is created successfully before returning it
request.run
if tempfile
request.on_complete do
if !tempfile
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
end
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`"
else
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
yield tempfile if block_given?
end

tempfile
end
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def self.default
# the data deserialized from response body (may be a Tempfile or nil), response status code and response headers.
def call_api(http_method, path, opts = {})
request = build_request(http_method, path, opts)
tempfile = download_file(request) if opts[:return_type] == 'File'
tempfile = nil
(download_file(request) { tempfile = _1 }) if opts[:return_type] == 'File'
response = request.run

if @config.debugging
Expand Down Expand Up @@ -188,19 +189,17 @@ def download_file(request)
chunk.force_encoding(encoding)
tempfile.write(chunk)
end
# run the request to ensure the tempfile is created successfully before returning it
request.run
if tempfile
request.on_complete do
if !tempfile
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
end
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`"
else
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
yield tempfile if block_given?
end

tempfile
end

# Check if the given MIME is a JSON MIME.
Expand Down
15 changes: 7 additions & 8 deletions samples/client/petstore/ruby-autoload/lib/petstore/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def self.default
# the data deserialized from response body (may be a Tempfile or nil), response status code and response headers.
def call_api(http_method, path, opts = {})
request = build_request(http_method, path, opts)
tempfile = download_file(request) if opts[:return_type] == 'File'
tempfile = nil
(download_file(request) { tempfile = _1 }) if opts[:return_type] == 'File'
response = request.run

if @config.debugging
Expand Down Expand Up @@ -188,19 +189,17 @@ def download_file(request)
chunk.force_encoding(encoding)
tempfile.write(chunk)
end
# run the request to ensure the tempfile is created successfully before returning it
request.run
if tempfile
request.on_complete do
if !tempfile
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
end
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`"
else
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
yield tempfile if block_given?
end

tempfile
end

# Check if the given MIME is a JSON MIME.
Expand Down
15 changes: 7 additions & 8 deletions samples/client/petstore/ruby/lib/petstore/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def self.default
# the data deserialized from response body (may be a Tempfile or nil), response status code and response headers.
def call_api(http_method, path, opts = {})
request = build_request(http_method, path, opts)
tempfile = download_file(request) if opts[:return_type] == 'File'
tempfile = nil
(download_file(request) { tempfile = _1 }) if opts[:return_type] == 'File'
response = request.run

if @config.debugging
Expand Down Expand Up @@ -188,19 +189,17 @@ def download_file(request)
chunk.force_encoding(encoding)
tempfile.write(chunk)
end
# run the request to ensure the tempfile is created successfully before returning it
request.run
if tempfile
request.on_complete do
if !tempfile
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
end
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`"
else
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
yield tempfile if block_given?
end

tempfile
end

# Check if the given MIME is a JSON MIME.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def self.default
# the data deserialized from response body (may be a Tempfile or nil), response status code and response headers.
def call_api(http_method, path, opts = {})
request = build_request(http_method, path, opts)
tempfile = download_file(request) if opts[:return_type] == 'File'
tempfile = nil
(download_file(request) { tempfile = _1 }) if opts[:return_type] == 'File'
response = request.run

if @config.debugging
Expand Down Expand Up @@ -188,19 +189,17 @@ def download_file(request)
chunk.force_encoding(encoding)
tempfile.write(chunk)
end
# run the request to ensure the tempfile is created successfully before returning it
request.run
if tempfile
request.on_complete do
if !tempfile
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
end
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`"
else
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
yield tempfile if block_given?
end

tempfile
end

# Check if the given MIME is a JSON MIME.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def self.default
# the data deserialized from response body (may be a Tempfile or nil), response status code and response headers.
def call_api(http_method, path, opts = {})
request = build_request(http_method, path, opts)
tempfile = download_file(request) if opts[:return_type] == 'File'
tempfile = nil
(download_file(request) { tempfile = _1 }) if opts[:return_type] == 'File'
response = request.run

if @config.debugging
Expand Down Expand Up @@ -187,19 +188,17 @@ def download_file(request)
chunk.force_encoding(encoding)
tempfile.write(chunk)
end
# run the request to ensure the tempfile is created successfully before returning it
request.run
if tempfile
request.on_complete do
if !tempfile
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
end
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`"
else
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
yield tempfile if block_given?
end

tempfile
end

# Check if the given MIME is a JSON MIME.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def self.default
# the data deserialized from response body (may be a Tempfile or nil), response status code and response headers.
def call_api(http_method, path, opts = {})
request = build_request(http_method, path, opts)
tempfile = download_file(request) if opts[:return_type] == 'File'
tempfile = nil
(download_file(request) { tempfile = _1 }) if opts[:return_type] == 'File'
response = request.run

if @config.debugging
Expand Down Expand Up @@ -187,19 +188,17 @@ def download_file(request)
chunk.force_encoding(encoding)
tempfile.write(chunk)
end
# run the request to ensure the tempfile is created successfully before returning it
request.run
if tempfile
request.on_complete do
if !tempfile
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
end
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`"
else
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
yield tempfile if block_given?
end

tempfile
end

# Check if the given MIME is a JSON MIME.
Expand Down
Loading