Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 27 additions & 0 deletions packages/smithy-aws-core/src/smithy_aws_core/protocols/restjson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Literal

from smithy_aws_core.traits import RestJson1Trait
from smithy_core.protocols import HttpBindingClientProtocol
from smithy_core.codecs import Codec
from smithy_core.shapes import ShapeID
from smithy_json import JSONCodec


class RestJsonClientProtocol(HttpBindingClientProtocol):
"""An implementation of the aws.protocols#restJson1 protocol."""

_id: ShapeID = RestJson1Trait.id
_codec: JSONCodec = JSONCodec()
_contentType: Literal["application/json"] = "application/json"

@property
def id(self) -> ShapeID:
return self._id

@property
def payload_codec(self) -> Codec:
return self._codec

@property
def content_type(self) -> str:
return self._contentType
44 changes: 44 additions & 0 deletions packages/smithy-aws-core/src/smithy_aws_core/traits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# This ruff check warns against using the assert statement, which can be stripped out
# when running Python with certain (common) optimization settings. Assert is used here
# for trait values. Since these are always generated, we can be fairly confident that
# they're correct regardless, so it's okay if the checks are stripped out.
# ruff: noqa: S101

from dataclasses import dataclass, field
from typing import Mapping, Sequence

from smithy_core.shapes import ShapeID
from smithy_core.traits import Trait, DocumentValue, DynamicTrait


@dataclass(init=False, frozen=True)
class RestJson1Trait(Trait, id=ShapeID("aws.protocols#restJson1")):
http: Sequence[str] = field(
repr=False, hash=False, compare=False, default_factory=tuple
)
event_stream_http: Sequence[str] = field(
repr=False, hash=False, compare=False, default_factory=tuple
)

def __init__(self, value: DocumentValue | DynamicTrait = None):
super().__init__(value)
assert isinstance(self.document_value, Mapping)

http_versions = self.document_value["http"]
assert isinstance(http_versions, Sequence)
for val in http_versions:
assert isinstance(val, str)
object.__setattr__(self, "http", tuple(http_versions))
event_stream_http_versions = self.document_value.get("eventStreamHttp")
if not event_stream_http_versions:
object.__setattr__(self, "event_stream_http", self.http)
else:
assert isinstance(event_stream_http_versions, Sequence)
for val in event_stream_http_versions:
assert isinstance(val, str)
object.__setattr__(
self, "event_stream_http", tuple(event_stream_http_versions)
)
119 changes: 119 additions & 0 deletions packages/smithy-core/src/smithy_core/protocols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import os
from inspect import iscoroutinefunction
from io import BytesIO

from smithy_core.aio.interfaces import ClientProtocol
from smithy_core.codecs import Codec
from smithy_core.deserializers import DeserializeableShape
from smithy_core.documents import TypeRegistry
from smithy_core.exceptions import ExpectationNotMetException
from smithy_core.interfaces import Endpoint, TypedProperties, URI
from smithy_core.schemas import APIOperation
from smithy_core.serializers import SerializeableShape
from smithy_core.traits import HTTPTrait, EndpointTrait
from smithy_http.aio.interfaces import HTTPRequest, HTTPResponse
from smithy_http.deserializers import HTTPResponseDeserializer
from smithy_http.serializers import HTTPRequestSerializer


class HttpClientProtocol(ClientProtocol[HTTPRequest, HTTPResponse]):
"""An HTTP-based protocol."""

def set_service_endpoint(
self,
*,
request: HTTPRequest,
endpoint: Endpoint,
) -> HTTPRequest:
uri = endpoint.uri
uri_builder = request.destination

if uri.scheme:
uri_builder.scheme = uri.scheme
if uri.host:
uri_builder.host = uri.host
if uri.port and uri.port > -1:
uri_builder.port = uri.port
if uri.path:
uri_builder.path = os.path.join(uri.path, uri_builder.path or "")
# TODO: merge headers from the endpoint properties bag
return request


class HttpBindingClientProtocol(HttpClientProtocol):
"""An HTTP-based protocol that uses HTTP binding traits."""

@property
def payload_codec(self) -> Codec:
"""The codec used for the serde of input and output payloads."""
...

@property
def content_type(self) -> str:
"""The media type of the http payload."""
...

def serialize_request[
OperationInput: "SerializeableShape",
OperationOutput: "DeserializeableShape",
](
self,
*,
operation: APIOperation[OperationInput, OperationOutput],
input: OperationInput,
endpoint: URI,
context: TypedProperties,
) -> HTTPRequest:
# TODO(optimization): request binding cache like done in SJ
serializer = HTTPRequestSerializer(
payload_codec=self.payload_codec,
http_trait=operation.schema.expect_trait(HTTPTrait),
endpoint_trait=operation.schema.get_trait(EndpointTrait),
)

input.serialize(
serializer=serializer
) # TODO: ensure serializer adds content-type
request = serializer.result

if request is None:
raise ExpectationNotMetException(
"Expected request to be serialized, but was None"
)

return request

async def deserialize_response[
OperationInput: "SerializeableShape",
OperationOutput: "DeserializeableShape",
](
self,
*,
operation: APIOperation[OperationInput, OperationOutput],
request: HTTPRequest,
response: HTTPResponse,
error_registry: TypeRegistry,
context: TypedProperties,
) -> OperationOutput:
if not (200 <= response.status <= 299): # TODO: extract to utility
# TODO: implement error serde from type registry
raise NotImplementedError

body = response.body

# if body is not streaming and is async, we have to buffer it
if not operation.output_stream_member:
if (
read := getattr(body, "read", None)
) is not None and iscoroutinefunction(read):
body = BytesIO(await read())

# TODO(optimization): response binding cache like done in SJ
deserializer = HTTPResponseDeserializer(
payload_codec=self.payload_codec,
http_trait=operation.schema.expect_trait(HTTPTrait),
response=response,
body=body, # type: ignore
)

return operation.output.deserialize(deserializer)
6 changes: 5 additions & 1 deletion packages/smithy-http/src/smithy_http/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
) is not None and not iscoroutinefunction(seek):
seek(0)

# TODO: conditional on empty-ness and a param of the protocol?
headers = binding_serializer.header_serializer.headers
headers.append(("content-type", self._payload_codec.media_type))

self.result = _HTTPRequest(
method=self._http_trait.method,
destination=URI(
Expand All @@ -122,7 +126,7 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
prefix=self._http_trait.query or "",
),
),
fields=tuples_to_fields(binding_serializer.header_serializer.headers),
fields=tuples_to_fields(headers),
body=payload,
)

Expand Down
6 changes: 5 additions & 1 deletion packages/smithy-http/tests/unit/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from smithy_http.deserializers import HTTPResponseDeserializer
from smithy_json import JSONCodec
from smithy_http.aio import HTTPResponse as _HTTPResponse
from smithy_http import tuples_to_fields, Fields
from smithy_http import tuples_to_fields, Field, Fields
from smithy_http.serializers import HTTPRequestSerializer, HTTPResponseSerializer

# TODO: empty header prefix, query map
Expand Down Expand Up @@ -1604,6 +1604,8 @@ def async_streaming_payload_cases() -> list[HTTPMessageTestCase]:
+ async_streaming_payload_cases()
)

CONTENT_TYPE_FIELD = Field(name="content-type", values=["application/json"])


@pytest.mark.parametrize("case", REQUEST_SER_CASES)
async def test_serialize_http_request(case: HTTPMessageTestCase) -> None:
Expand All @@ -1623,6 +1625,8 @@ async def test_serialize_http_request(case: HTTPMessageTestCase) -> None:
actual_query = actual.destination.query or ""
expected_query = case.request.destination.query or ""
assert actual_query == expected_query
# set the content-type field here, otherwise cases would have to duplicate it everywhere
expected.fields.set_field(CONTENT_TYPE_FIELD)
assert actual.fields == expected.fields

if case.request.body:
Expand Down