File: handler.rb

package info (click to toggle)
ruby-aws-sdk-core 3.212.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,232 kB
  • sloc: ruby: 17,533; makefile: 4
file content (79 lines) | stat: -rw-r--r-- 2,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

module Aws
  module RpcV2
    class Handler < Seahorse::Client::Handler
      # @param [Seahorse::Client::RequestContext] context
      # @return [Seahorse::Client::Response]
      def call(context)
        build_request(context)
        response = with_metric { @handler.call(context) }
        response.on(200..299) { |resp| resp.data = parse_body(context) }
        response.on(200..599) { |_resp| apply_request_id(context) }
        response
      end

      private

      def with_metric(&block)
        Aws::Plugins::UserAgent.metric('PROTOCOL_RPC_V2_CBOR', &block)
      end

      def build_request(context)
        context.http_request.headers['Smithy-Protocol'] = 'rpc-v2-cbor'
        context.http_request.headers['X-Amzn-Query-Mode'] = 'true' if query_compatible?(context)
        context.http_request.http_method = 'POST'
        context.http_request.body = build_body(context)
        build_url(context)
      end

      def build_url(context)
        base = context.http_request.endpoint
        service_name = context.config.api.metadata['targetPrefix']
        base.path += "/service/#{service_name}/operation/#{context.operation.name}"
      end

      def build_body(context)
        Builder.new(context.operation.input).serialize(context.params)
      end

      def parse_body(context)
        cbor = context.http_response.body_contents
        if (rules = context.operation.output)
          if cbor.is_a?(Array)
            # an array of emitted events
            if cbor[0].respond_to?(:response)
              # initial response exists
              # it must be the first event arrived
              resp_struct = cbor.shift.response
            else
              resp_struct = context.operation.output.shape.struct_class.new
            end

            rules.shape.members.each do |name, ref|
              if ref.eventstream
                resp_struct.send("#{name}=", cbor.to_enum)
              end
            end
            resp_struct
          else
            Parser.new(
              rules,
              query_compatible: query_compatible?(context)
            ).parse(cbor)
          end
        else
          EmptyStructure.new
        end
      end

      def apply_request_id(context)
        context[:request_id] = context.http_response.headers['x-amzn-requestid']
      end

      def query_compatible?(context)
        context.config.api.metadata.key?('awsQueryCompatible')
      end
    end
  end
end