Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed an issue with block reads and non-200 responses.
- Loading branch information
1 parent
231063a
commit ba932c1
Showing
10 changed files
with
97 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
aws-sdk-core/lib/seahorse/client/plugins/response_target.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module Seahorse | ||
module Client | ||
module Plugins | ||
# @api private | ||
class ResponseTarget < Plugin | ||
|
||
# This handler is responsible for replacing the HTTP response body IO | ||
# object with custom targets, such as a block, or a file. It is important | ||
# to not write data to the custom target in the case of a non-success | ||
# response. We do not want to write an XML error message to someone's | ||
# file. | ||
class Handler < Client::Handler | ||
|
||
def call(context) | ||
target = context.params.delete(:response_target) | ||
target ||= context[:response_target] | ||
add_event_listeners(context, target) if target | ||
@handler.call(context) | ||
end | ||
|
||
private | ||
|
||
def add_event_listeners(context, target) | ||
handler = self | ||
context.http_response.on_headers(200) do | ||
context.http_response.body = handler.send(:io, target) | ||
end | ||
|
||
context.http_response.on_success(200) do | ||
body = context.http_response.body | ||
if ManagedFile === body && body.open? | ||
body.close | ||
end | ||
end | ||
|
||
context.http_response.on_error do | ||
body = context.http_response.body | ||
File.unlink(body) if ManagedFile === body | ||
context.http_response.body = StringIO.new | ||
end | ||
end | ||
|
||
def io(target) | ||
case target | ||
when Proc then BlockIO.new(&target) | ||
when String, Pathname then ManagedFile.new(target, 'w+b') | ||
else target | ||
end | ||
end | ||
|
||
end | ||
|
||
handler(Handler, step: :initialize, priority: 90) | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters