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 80 81 82 83 84 85 86 87 88
|
# frozen_string_literal: true
module Aws
module Json
class Handler < Seahorse::Client::Handler
CONTENT_TYPE = 'application/x-amz-json-%s'
# @param [Seahorse::Client::RequestContext] context
# @return [Seahorse::Client::Response]
def call(context)
build_request(context)
response = @handler.call(context)
response.on(200..299) { |resp| parse_response(resp) }
response.on(200..599) { |resp| apply_request_id(context) }
response
end
private
def build_request(context)
context.http_request.http_method = 'POST'
context.http_request.headers['Content-Type'] = content_type(context)
context.http_request.headers['X-Amz-Target'] = target(context)
context.http_request.body = build_body(context)
end
def build_body(context)
if simple_json?(context)
Json.dump(context.params)
else
Builder.new(context.operation.input).serialize(context.params)
end
end
def parse_response(response)
response.data = parse_body(response.context)
end
def parse_body(context)
if simple_json?(context)
Json.load(context.http_response.body_contents)
elsif rules = context.operation.output
json = context.http_response.body_contents
if json.is_a?(Array)
# an array of emitted events
if json[0].respond_to?(:response)
# initial response exists
# it must be the first event arrived
resp_struct = json.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}=", json.to_enum)
end
end
resp_struct
else
Parser.new(rules).parse(json == '' ? '{}' : json)
end
else
EmptyStructure.new
end
end
def content_type(context)
CONTENT_TYPE % [context.config.api.metadata['jsonVersion']]
end
def target(context)
prefix = context.config.api.metadata['targetPrefix']
"#{prefix}.#{context.operation.name}"
end
def apply_request_id(context)
context[:request_id] = context.http_response.headers['x-amzn-requestid']
end
def simple_json?(context)
context.config.simple_json
end
end
end
end
|