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
|
# frozen_string_literal: true
module Aws
module Plugins
# Provide support for `api_key` parameter for `api-gateway` protocol
# specific `api-gateway` protocol gems' user-agent
class ApiKey < Seahorse::Client::Plugin
option(:api_key,
default: nil,
doc_type: 'String',
docstring: <<-DOCS)
When provided, `x-api-key` header will be injected with the value provided.
DOCS
def add_handlers(handlers, config)
handlers.add(OptionHandler, step: :initialize)
handlers.add(ApiKeyHandler, step: :build, priority: 0)
end
# @api private
class OptionHandler < Seahorse::Client::Handler
def call(context)
if context.operation.require_apikey
if context.params.is_a?(Hash) && context.params[:api_key]
api_key = context.params.delete(:api_key)
end
api_key = context.config.api_key if api_key.nil?
context[:api_key] = api_key
end
@handler.call(context)
end
end
# @api private
class ApiKeyHandler < Seahorse::Client::Handler
def call(context)
if context[:api_key]
apply_api_key(context)
end
@handler.call(context)
end
private
def apply_api_key(context)
context.http_request.headers['x-api-key'] = context[:api_key]
end
end
end
end
end
|