class Aws::Rest::ContentTypeHandler

NOTE: headers could be already populated if specified on input shape

Public Instance Methods

call(context) click to toggle source
# File lib/aws-sdk-core/rest/content_type_handler.rb, line 7
def call(context)
  if eventstream?(context)
    context.http_request.headers['Content-Type'] ||=
      'application/vnd.amazon.eventstream'
  elsif (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 = context.http_request.body) &&
        (!body.respond_to?(:size) || non_empty_body?(body))
    apply_default_content_type(context)
  end

  @handler.call(context)
end

Private Instance Methods

apply_default_content_type(context) click to toggle source

content-type defaults as noted here: rest-json: smithy.io/2.0/aws/protocols/aws-restxml-protocol.html#content-type rest-xml: smithy.io/2.0/aws/protocols/aws-restxml-protocol.html#content-type

# File lib/aws-sdk-core/rest/content_type_handler.rb, line 46
def apply_default_content_type(context)
  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'
  else raise "Unsupported protocol #{protocol}"
  end
end
eventstream?(context) click to toggle source
# File lib/aws-sdk-core/rest/content_type_handler.rb, line 36
def eventstream?(context)
  context.operation.input.shape.members.each do |_, ref|
    return true if ref.eventstream
  end
  false
end
non_empty_body?(body) click to toggle source
# File lib/aws-sdk-core/rest/content_type_handler.rb, line 32
def non_empty_body?(body)
  body.respond_to?(:size) && body.size.positive?
end