-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement protocol plugin and rpcv2 protocol (#264)
- Loading branch information
Showing
45 changed files
with
920 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'protocols/rpc_v2' | ||
|
||
module Smithy | ||
module Client | ||
# In Smithy, a protocol is a named set of rules that defines the | ||
# syntax and semantics of how a client and server communicate. To | ||
# use a {Smithy::Client}, a protocol is required to be set on | ||
# {Configuration}. | ||
# | ||
# We currently support the following protocols: | ||
# | ||
# - {RPCv2}, a RPC-based protocol over HTTP that sends requests | ||
# and responses with CBOR payloads. | ||
# | ||
# You could also create a custom protocol to pass into the client | ||
# configuration. The given protocol must provide the following | ||
# functionalities: | ||
# | ||
# - `build` - builds the request | ||
# - `parse` - parse the response | ||
# - `error` - extracts the error from response | ||
# | ||
# See {RPCv2} as an example implementation. | ||
module Protocols; end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# frozen_string_literal: true | ||
|
||
module Smithy | ||
module Client | ||
module Protocols | ||
# A RPC-based protocol over HTTP that sends requests | ||
# and responses with CBOR payloads. | ||
# | ||
# TODO: Refactor methods to handle eventstreams | ||
class RPCv2 | ||
# @api private | ||
SHAPE_ID = 'smithy.protocols#rpcv2Cbor' | ||
|
||
# @param options [Hash] Protocol options | ||
# @option options [Boolean] :query_compatible (nil) | ||
def initialize(options = {}) | ||
@query_compatible = options[:query_compatible] | ||
end | ||
|
||
# @api private | ||
def build(context) | ||
codec = Client::Codecs::CBOR.new(setting(context)) | ||
context.request.body = codec.serialize(context.params, context.operation.input) | ||
context.request.http_method = 'POST' | ||
apply_headers(context) | ||
build_url(context) | ||
end | ||
|
||
# @api private | ||
def parse(context) | ||
output_shape = context.operation.output | ||
codec = Client::Codecs::CBOR.new(setting(context)) | ||
codec.deserialize(context.response.body.read, output_shape) | ||
end | ||
|
||
# @api private | ||
# TODO: To implement after error handling | ||
def error(_context, _response); end | ||
|
||
private | ||
|
||
def apply_headers(context) | ||
context.request.headers['X-Amzn-Query-Mode'] = 'true' if query_compatible?(context) | ||
context.request.headers['Smithy-Protocol'] = 'rpc-v2-cbor' | ||
apple_content_type(context) | ||
apply_accept_header(context) | ||
# TODO: Implement Content-Length Plugin/Handler | ||
context.request.headers['Content-Length'] = context.request.body.size | ||
end | ||
|
||
def apply_accept_header(context) | ||
# TODO: Needs an update when streaming is handled | ||
context.request.headers['Accept'] = 'application/cbor' | ||
end | ||
|
||
def apple_content_type(context) | ||
return if context.operation.input == Model::Shapes::Prelude::Unit | ||
|
||
# TODO: Needs an update when streaming is handled | ||
context.request.headers['Content-Type'] = 'application/cbor' | ||
end | ||
|
||
def build_url(context) | ||
base = context.request.endpoint | ||
base.path += | ||
"/service/#{context.config.service.name}/operation/#{context.operation.name}" | ||
end | ||
|
||
def setting(context) | ||
{}.tap do |h| | ||
h[:query_compatible] = true if query_compatible?(context) | ||
end | ||
end | ||
|
||
def query_compatible?(context) | ||
@query_compatible || | ||
context.config.service.traits.one? { |k, _v| k == 'aws.protocols#awsQuery' } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Smithy | ||
module Client | ||
module Protocols | ||
# A RPC-based protocol over HTTP that sends requests | ||
# and responses with CBOR payloads. | ||
class RPCv2 | ||
def initialize: (?::Hash[untyped, untyped] options) -> void | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.