Sha256: 80611096bec501768b43d83b18a5e8d12acf058ad8fe16d3187f6d0f0d7eecb2

Contents?: true

Size: 1.71 KB

Versions: 27

Compression:

Stored size: 1.71 KB

Contents

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..299) do
              context.http_response.body = handler.send(:io, target)
            end

            context.http_response.on_success(200..299) 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

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
aws-sdk-core-2.1.4 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.1.3 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.1.2 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.1.1 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.1.0 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.48 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.47 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.46 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.45 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.44 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.43 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.42 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.41 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.40 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.39 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.38 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.37 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.36 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.35 lib/seahorse/client/plugins/response_target.rb
aws-sdk-core-2.0.34 lib/seahorse/client/plugins/response_target.rb