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
|
# frozen_string_literal: true
module Aws
module Plugins
# @api private
class EndpointPattern < Seahorse::Client::Plugin
option(:disable_host_prefix_injection,
default: false,
doc_type: 'Boolean',
docstring: <<-DOCS
Set to true to disable SDK automatically adding host prefix
to default service endpoint when available.
DOCS
)
def add_handlers(handlers, config)
if config.regional_endpoint && !config.disable_host_prefix_injection
handlers.add(Handler, priority: 90)
end
end
class Handler < Seahorse::Client::Handler
def call(context)
endpoint_trait = context.operation.endpoint_pattern
if endpoint_trait && !endpoint_trait.empty?
_apply_endpoint_trait(context, endpoint_trait)
end
@handler.call(context)
end
private
def _apply_endpoint_trait(context, trait)
# currently only support host pattern
ori_host = context.http_request.endpoint.host
if pattern = trait['hostPrefix']
host_prefix = pattern.gsub(/\{.+?\}/) do |label|
label = label.delete("{}")
_replace_label_value(
ori_host, label, context.operation.input, context.params)
end
context.http_request.endpoint.host = host_prefix + context.http_request.endpoint.host
end
end
def _replace_label_value(ori, label, input_ref, params)
name = nil
input_ref.shape.members.each do |m_name, ref|
if ref['hostLabel'] && ref['hostLabelName'] == label
name = m_name
end
end
if name.nil? || params[name].nil?
raise Errors::MissingEndpointHostLabelValue.new(name)
end
params[name]
end
end
end
end
end
|