Skip to content

Commit 515c882

Browse files
authored
Ruby fix typhoeus api client multiple call with file return type (#20615)
* [Ruby] fix Typhoeus api client multiple call with file return type * update samples
1 parent c5fb60b commit 515c882

File tree

7 files changed

+49
-56
lines changed

7 files changed

+49
-56
lines changed

modules/openapi-generator/src/main/resources/ruby-client/api_client_typhoeus_partial.mustache

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# the data deserialized from response body (may be a Tempfile or nil), response status code and response headers.
55
def call_api(http_method, path, opts = {})
66
request = build_request(http_method, path, opts)
7-
tempfile = download_file(request) if opts[:return_type] == 'File'
7+
tempfile = nil
8+
(download_file(request) { tempfile = _1 }) if opts[:return_type] == 'File'
89
response = request.run
910

1011
if @config.debugging
@@ -145,17 +146,15 @@
145146
chunk.force_encoding(encoding)
146147
tempfile.write(chunk)
147148
end
148-
# run the request to ensure the tempfile is created successfully before returning it
149-
request.run
150-
if tempfile
149+
request.on_complete do
150+
if !tempfile
151+
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
152+
end
151153
tempfile.close
152154
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
153155
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
154156
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
155157
"explicitly with `tempfile.delete`"
156-
else
157-
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
158+
yield tempfile if block_given?
158159
end
159-
160-
tempfile
161160
end

samples/client/echo_api/ruby-typhoeus/lib/openapi_client/api_client.rb

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

5556
if @config.debugging
@@ -188,19 +189,17 @@ def download_file(request)
188189
chunk.force_encoding(encoding)
189190
tempfile.write(chunk)
190191
end
191-
# run the request to ensure the tempfile is created successfully before returning it
192-
request.run
193-
if tempfile
192+
request.on_complete do
193+
if !tempfile
194+
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
195+
end
194196
tempfile.close
195197
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
196198
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
197199
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
198200
"explicitly with `tempfile.delete`"
199-
else
200-
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
201+
yield tempfile if block_given?
201202
end
202-
203-
tempfile
204203
end
205204

206205
# Check if the given MIME is a JSON MIME.

samples/client/petstore/ruby-autoload/lib/petstore/api_client.rb

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

5556
if @config.debugging
@@ -188,19 +189,17 @@ def download_file(request)
188189
chunk.force_encoding(encoding)
189190
tempfile.write(chunk)
190191
end
191-
# run the request to ensure the tempfile is created successfully before returning it
192-
request.run
193-
if tempfile
192+
request.on_complete do
193+
if !tempfile
194+
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
195+
end
194196
tempfile.close
195197
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
196198
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
197199
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
198200
"explicitly with `tempfile.delete`"
199-
else
200-
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
201+
yield tempfile if block_given?
201202
end
202-
203-
tempfile
204203
end
205204

206205
# Check if the given MIME is a JSON MIME.

samples/client/petstore/ruby/lib/petstore/api_client.rb

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

5556
if @config.debugging
@@ -188,19 +189,17 @@ def download_file(request)
188189
chunk.force_encoding(encoding)
189190
tempfile.write(chunk)
190191
end
191-
# run the request to ensure the tempfile is created successfully before returning it
192-
request.run
193-
if tempfile
192+
request.on_complete do
193+
if !tempfile
194+
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
195+
end
194196
tempfile.close
195197
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
196198
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
197199
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
198200
"explicitly with `tempfile.delete`"
199-
else
200-
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
201+
yield tempfile if block_given?
201202
end
202-
203-
tempfile
204203
end
205204

206205
# Check if the given MIME is a JSON MIME.

samples/openapi3/client/extensions/x-auth-id-alias/ruby-client/lib/x_auth_id_alias/api_client.rb

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

5556
if @config.debugging
@@ -188,19 +189,17 @@ def download_file(request)
188189
chunk.force_encoding(encoding)
189190
tempfile.write(chunk)
190191
end
191-
# run the request to ensure the tempfile is created successfully before returning it
192-
request.run
193-
if tempfile
192+
request.on_complete do
193+
if !tempfile
194+
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
195+
end
194196
tempfile.close
195197
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
196198
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
197199
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
198200
"explicitly with `tempfile.delete`"
199-
else
200-
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
201+
yield tempfile if block_given?
201202
end
202-
203-
tempfile
204203
end
205204

206205
# Check if the given MIME is a JSON MIME.

samples/openapi3/client/features/dynamic-servers/ruby/lib/dynamic_servers/api_client.rb

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

5556
if @config.debugging
@@ -187,19 +188,17 @@ def download_file(request)
187188
chunk.force_encoding(encoding)
188189
tempfile.write(chunk)
189190
end
190-
# run the request to ensure the tempfile is created successfully before returning it
191-
request.run
192-
if tempfile
191+
request.on_complete do
192+
if !tempfile
193+
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
194+
end
193195
tempfile.close
194196
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
195197
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
196198
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
197199
"explicitly with `tempfile.delete`"
198-
else
199-
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
200+
yield tempfile if block_given?
200201
end
201-
202-
tempfile
203202
end
204203

205204
# Check if the given MIME is a JSON MIME.

samples/openapi3/client/features/generate-alias-as-model/ruby-client/lib/petstore/api_client.rb

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

5556
if @config.debugging
@@ -187,19 +188,17 @@ def download_file(request)
187188
chunk.force_encoding(encoding)
188189
tempfile.write(chunk)
189190
end
190-
# run the request to ensure the tempfile is created successfully before returning it
191-
request.run
192-
if tempfile
191+
request.on_complete do
192+
if !tempfile
193+
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
194+
end
193195
tempfile.close
194196
@config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
195197
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
196198
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
197199
"explicitly with `tempfile.delete`"
198-
else
199-
fail ApiError.new("Failed to create the tempfile based on the HTTP response from the server: #{request.inspect}")
200+
yield tempfile if block_given?
200201
end
201-
202-
tempfile
203202
end
204203

205204
# Check if the given MIME is a JSON MIME.

0 commit comments

Comments
 (0)