Skip to content

Commit

Permalink
Merge branch 'decaf' into decaf_endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods committed Dec 23, 2024
2 parents 9a52cb1 + c5a6690 commit 3feea81
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 7 deletions.
1 change: 1 addition & 0 deletions gems/smithy-client/lib/smithy-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# plugins

require_relative 'smithy-client/plugins/logging'
require_relative 'smithy-client/plugins/raise_response_errors'
require_relative 'smithy-client/plugins/net_http'

# model
Expand Down
10 changes: 7 additions & 3 deletions gems/smithy-client/lib/smithy-client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class Base
# @api private
@plugins = PluginList.new(
[
# Plugins::NetHttp,
# Plugins::RaiseResponseErrors,
# Plugins::Endpoint,
# Plugins::NetHTTP
Plugins::RaiseResponseErrors
# Plugins::ResponseTarget,
# Plugins::RequestCallback
]
Expand Down Expand Up @@ -87,7 +88,10 @@ def context_for(operation_name, params)
operation: config.api.operation(operation_name),
client: self,
params: params,
config: config
config: config,
# TODO: these should be determined by the API
request: HTTP::Request.new,
response: HTTP::Response.new
)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Smithy
module Client
module Plugins
# @api private
class RaiseResponseErrors < Plugin
option(
:raise_response_errors,
default: true,
doc_type: 'Boolean',
docstring: <<~DOCS)
When `true`, response errors are raised. When `false`, the error is placed on the
output in the {Smithy::Client::Output#error error accessor}.
DOCS

# @api private
class Handler < Client::Handler
def call(context)
output = @handler.call(context)
raise output.error if output.error

output
end
end

def add_handlers(handlers, config)
handlers.add(Handler, step: :validate, priority: 95) if config.raise_response_errors
end
end
end
end
end
2 changes: 1 addition & 1 deletion gems/smithy-client/spec/smithy-client/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ module Client
client_class = Class.new(Base)
expected = [
# Plugins::NetHTTP
# Plugins::RaiseResponseErrors,
Plugins::RaiseResponseErrors
# Plugins::ResponseTarget,
# Plugins::RequestCallback
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module Smithy
module Client
module Plugins
describe RaiseResponseErrors do
let(:client_class) do
api = API.new
api.add_operation(:operation_name, Operation.new)
client_class = Class.new(Client::Base)
client_class.api = api
client_class.clear_plugins
client_class.add_plugin(RaiseResponseErrors)
client_class.add_plugin(DummySendPlugin)
client_class
end

it 'adds a :raise_response_errors option to config' do
client = client_class.new(raise_response_errors: false)
expect(client.config.raise_response_errors).to be(false)
end

it 'defaults :raise_response_errors to true' do
client = client_class.new
expect(client.config.raise_response_errors).to be(true)
end

it 'does not add the handler if :raise_response_errors is false' do
client = client_class.new(raise_response_errors: false)
expect(client.handlers).not_to include(Handler)
end

it 'returns output' do
client = client_class.new
output = client.operation_name
expect(output).to be_kind_of(Output)
end

it 'raises the response error when :raise_response_errors is true' do
error = StandardError.new('msg')
client = client_class.new(response_error: error)
expect { client.operation_name }.to raise_error(error)
end

it 'puts the error on the output when :raise_response_errors is false' do
error = StandardError.new('msg')
client = client_class.new(
raise_response_errors: false,
response_error: error
)
output = client.operation_name
expect(output.error).to be(error)
end
end
end
end
end
4 changes: 3 additions & 1 deletion gems/smithy-client/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class Handler < Smithy::Client::Handler
def call(context)
Smithy::Client::Output.new(
context: context,
data: context.config.response_data
data: context.config.response_data,
error: context.config.response_error
)
end
end

option(:response_data) { { result: 'success' } }
option(:response_error) { nil }
handler Handler, step: :send
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The endpoint provider used to resolve endpoints. Any object that responds to
<%- end -%>

# @api private
class Handler < Seahorse::Client::Handler
class Handler < Smithy::Client::Handler
def call(context)
params = EndpointParameters.create(context.config)
endpoint = context.config.endpoint_provider.resolve_endpoint(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(plan)
@model = plan.model
service = Vise::ServiceIndex.new(@model).service
@endpoint_rules = service.values.first['traits']['smithy.rules#endpointRuleSet']
@operations = Vice::OperationInded.new(@model).for(service)
@operations = Vise::OperationIndex.new(@model).for(service)
@parameters = @endpoint_rules['parameters']
.map { |id, data| EndpointParameter.new(id, data, @plan) }

Expand Down

0 comments on commit 3feea81

Please sign in to comment.