Sha256: d803779452c917a2d887e6098ccb878bc1b3c9ae25e5b5ed4beb0d3458ccdb5d

Contents?: true

Size: 1.97 KB

Versions: 33

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Aws
  module Rest
    module Response
      class Body

        include Seahorse::Model::Shapes

        # @param [Class] parser_class
        # @param [Seahorse::Model::ShapeRef] rules
        def initialize(parser_class, rules)
          @parser_class = parser_class
          @rules = rules
        end

        # @param [IO] body
        # @param [Hash, Struct] data
        def apply(body, data)
          if event_stream?
            data[@rules[:payload]] = parse_eventstream(body)
          elsif streaming?
            # empty blob payloads are omitted
            data[@rules[:payload]] = body unless empty_blob_payload?(body)
          elsif @rules[:payload]
            data[@rules[:payload]] = parse(body.read, @rules[:payload_member])
          elsif !@rules.shape.member_names.empty?
            parse(body.read, @rules, data)
          end
        end

        private

        def empty_blob_payload?(body)
          true if non_streaming_blob_payload? && empty_body?(body)
        end

        def non_streaming_blob_payload?
          @rules[:payload_member].shape.is_a?(BlobShape) &&
            !@rules[:payload_member]['streaming']
        end

        def empty_body?(body)
          body.respond_to?(:size) && body.size.zero?
        end

        def event_stream?
          @rules[:payload] && @rules[:payload_member].eventstream
        end

        def streaming?
          @rules[:payload] && (
            BlobShape === @rules[:payload_member].shape ||
            StringShape === @rules[:payload_member].shape
          )
        end

        def parse(body, rules, target = nil)
          @parser_class.new(rules).parse(body, target) if body.size > 0
        end

        def parse_eventstream(body)
          # body contains an array of parsed event when they arrive
          @rules[:payload_member].shape.struct_class.new do |payload|
            body.each { |event| payload << event }
          end
        end

      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
aws-sdk-core-3.214.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.213.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.212.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.211.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.210.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.209.1 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.209.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.208.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.207.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.206.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.205.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.204.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.203.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.202.2 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.202.1 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.202.0 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.201.5 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.201.4 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.201.3 lib/aws-sdk-core/rest/response/body.rb
aws-sdk-core-3.201.2 lib/aws-sdk-core/rest/response/body.rb