Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Content Type plugin #3008

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def compute_plugins(options)
def default_plugins
{
'Seahorse::Client::Plugins::ContentLength' => "#{seahorse_plugins}/content_length.rb",
'Seahorse::Client::Plugins::ContentType' => "#{seahorse_plugins}/content_type.rb",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this being default, should we relocate the setting of content-type for other protocols like json here?

'Aws::Plugins::CredentialsConfiguration' => "#{core_plugins}/credentials_configuration.rb",
'Aws::Plugins::Logging' => "#{core_plugins}/logging.rb",
'Aws::Plugins::ParamConverter' => "#{core_plugins}/param_converter.rb",
Expand Down
4 changes: 2 additions & 2 deletions build_tools/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class ServiceEnumerator
MANIFEST_PATH = File.expand_path('../../services.json', __FILE__)

# Minimum `aws-sdk-core` version for new gem builds
MINIMUM_CORE_VERSION = "3.191.0"
MINIMUM_CORE_VERSION = "3.191.7"

# Minimum `aws-sdk-core` version for new S3 gem builds
MINIMUM_CORE_VERSION_S3 = "3.192.0"
MINIMUM_CORE_VERSION_S3 = "3.191.7"

EVENTSTREAM_PLUGIN = "Aws::Plugins::EventStreamConfiguration"

Expand Down
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Unreleased Changes
------------------

* Feature - Update serializing/deserializing for all protocols to align with Smithy protocol-tests.
* Issue - Update serializing/deserializing for all protocols to align with Smithy protocol-tests.
* Issue - Allow `nil` values in lists and maps.
* Issue - Populate headers for XML and JSON error responses.
* Issue - Support fractional seconds when parsing `DateTime` timestamps.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ module Plugins
module Protocols
class RestJson < Seahorse::Client::Plugin
handler(Rest::Handler)
# Rest::Handler will set a default JSON body, so size can be checked
# if this handler is run after serialization.
handler(Rest::ContentTypeHandler, priority: 30)
handler(Json::ErrorHandler, step: :sign)
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Plugins
module Protocols
class RestXml < Seahorse::Client::Plugin
handler(Rest::Handler)
handler(Rest::ContentTypeHandler)
handler(Xml::ErrorHandler, step: :sign)
end
end
Expand Down
1 change: 0 additions & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require_relative 'rest/request/endpoint'
require_relative 'rest/request/headers'
require_relative 'rest/request/querystring_builder'
require_relative 'rest/request/content_type'
require_relative 'rest/response/body'
require_relative 'rest/response/headers'
require_relative 'rest/response/parser'
Expand Down
51 changes: 0 additions & 51 deletions gems/aws-sdk-core/lib/aws-sdk-core/rest/request/content_type.rb

This file was deleted.

58 changes: 58 additions & 0 deletions gems/aws-sdk-core/lib/seahorse/client/plugins/content_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

module Seahorse
module Client
module Plugins
class ContentType < Plugin

# @api private
class Handler < Client::Handler
def call(context)
body = context.http_request.body

if (payload = context.operation.input[:payload_member])
case payload.shape
when Seahorse::Model::Shapes::BlobShape
context.http_request.headers['Content-Type'] ||=
'application/octet-stream'
when Seahorse::Model::Shapes::StringShape
context.http_request.headers['Content-Type'] ||=
'text/plain'
else
apply_default_content_type(context)
end
elsif !body.respond_to?(:size) || non_empty_body?(body)
apply_default_content_type(context)
end

@handler.call(context)
end

private

def non_empty_body?(body)
body.respond_to?(:size) && body.size.positive?
end

# content-type defaults as noted here:
# rest-json: https://smithy.io/2.0/aws/protocols/aws-restxml-protocol.html#content-type
# rest-xml: https://smithy.io/2.0/aws/protocols/aws-restxml-protocol.html#content-type
def apply_default_content_type(context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this belongs in Seahorse - this is AWS specific logic per protocol. I'd lean instead towards a generic base class which the protocols can extend and use.

protocol = context.config.api.metadata['protocol']
case protocol
when 'rest-json'
context.http_request.headers['Content-Type'] ||=
'application/json'
when 'rest-xml'
context.http_request.headers['Content-Type'] ||=
'application/xml'
end
end
end

handler(Handler, step: :sign, priority: 0)

end
end
end
end
2 changes: 1 addition & 1 deletion gems/aws-sdk-s3/lib/aws-sdk-s3/presigner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def sign_but_dont_send(
req.handlers.remove(Aws::S3::Plugins::S3Signer::LegacyHandler)
req.handlers.remove(Aws::Plugins::Sign::Handler)
req.handlers.remove(Seahorse::Client::Plugins::ContentLength::Handler)
req.handlers.remove(Aws::Rest::ContentTypeHandler)
req.handlers.remove(Seahorse::Client::Plugins::ContentType::Handler)

req.handle(step: :send) do |context|
# if an endpoint was not provided, force secure or insecure
Expand Down
Loading