Skip to content
Merged
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
@@ -0,0 +1,2 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
27 changes: 27 additions & 0 deletions packages/smithy-aws-core/src/smithy_aws_core/aio/protocols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Final

from smithy_aws_core.traits import RestJson1Trait
from smithy_http.aio.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: Final = "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)
)
117 changes: 117 additions & 0 deletions packages/smithy-http/src/smithy_http/aio/protocols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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
Copy link
Contributor

Choose a reason for hiding this comment

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

It'll be under headers in the properties bag. Not necessary right now though.

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)
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: 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)
28 changes: 27 additions & 1 deletion packages/smithy-http/src/smithy_http/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
TimestampFormatTrait,
EndpointTrait,
HTTPErrorTrait,
MediaTypeTrait,
StreamingTrait,
)
from smithy_core.shapes import ShapeType
from smithy_core.utils import serialize_float
Expand Down Expand Up @@ -83,22 +85,33 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
if self._endpoint_trait is not None:
host_prefix = self._endpoint_trait.host_prefix

content_type = self._payload_codec.media_type

if (payload_member := self._get_payload_member(schema)) is not None:
if payload_member.shape_type in (ShapeType.BLOB, ShapeType.STRING):
content_type = (
"application/octet-stream"
if payload_member.shape_type is ShapeType.BLOB
else "text/plain"
)
payload_serializer = RawPayloadSerializer()
binding_serializer = HTTPRequestBindingSerializer(
payload_serializer, self._http_trait.path, host_prefix
)
yield binding_serializer
payload = payload_serializer.payload
else:
if (media_type := payload_member.get_trait(MediaTypeTrait)) is not None:
content_type = media_type.value
payload = BytesIO()
payload_serializer = self._payload_codec.create_serializer(payload)
binding_serializer = HTTPRequestBindingSerializer(
payload_serializer, self._http_trait.path, host_prefix
)
yield binding_serializer
else:
if self._get_eventstreaming_member(schema) is not None:
content_type = "application/vnd.amazon.eventstream"
payload = BytesIO()
payload_serializer = self._payload_codec.create_serializer(payload)
with payload_serializer.begin_struct(schema) as body_serializer:
Expand All @@ -112,6 +125,10 @@ def begin_struct(self, schema: Schema) -> Iterator[ShapeSerializer]:
) is not None and not iscoroutinefunction(seek):
seek(0)

# TODO: conditional on empty-ness and based on the protocol
headers = binding_serializer.header_serializer.headers
headers.append(("content-type", content_type))

self.result = _HTTPRequest(
method=self._http_trait.method,
destination=URI(
Expand All @@ -122,7 +139,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 All @@ -132,6 +149,15 @@ def _get_payload_member(self, schema: Schema) -> Schema | None:
return member
return None

def _get_eventstreaming_member(self, schema: Schema) -> Schema | None:
for member in schema.members.values():
if (
member.get_trait(StreamingTrait) is not None
and member.shape_type is ShapeType.UNION
):
return member
return None


class HTTPRequestBindingSerializer(InterceptingSerializer):
"""Delegates HTTP request bindings to binding-location-specific serializers."""
Expand Down
25 changes: 21 additions & 4 deletions 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 @@ -1572,11 +1572,16 @@ def payload_cases() -> list[HTTPMessageTestCase]:
),
HTTPMessageTestCase(
HTTPStringPayload(payload="foo"),
HTTPMessage(body=b"foo"),
HTTPMessage(
fields=tuples_to_fields([("content-type", "text/plain")]), body=b"foo"
),
),
HTTPMessageTestCase(
HTTPBlobPayload(payload=b"\xde\xad\xbe\xef"),
HTTPMessage(body=b"\xde\xad\xbe\xef"),
HTTPMessage(
fields=tuples_to_fields([("content-type", "application/octet-stream")]),
body=b"\xde\xad\xbe\xef",
),
),
HTTPMessageTestCase(
HTTPStructuredPayload(payload=HTTPStringPayload(payload="foo")),
Expand All @@ -1589,7 +1594,10 @@ def async_streaming_payload_cases() -> list[HTTPMessageTestCase]:
return [
HTTPMessageTestCase(
HTTPStreamingPayload(payload=AsyncBytesReader(b"\xde\xad\xbe\xef")),
HTTPMessage(body=AsyncBytesReader(b"\xde\xad\xbe\xef")),
HTTPMessage(
fields=tuples_to_fields([("content-type", "application/octet-stream")]),
body=AsyncBytesReader(b"\xde\xad\xbe\xef"),
),
),
]

Expand All @@ -1604,6 +1612,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 +1633,10 @@ 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,
# but if the field is already set in the case, don't override it
if expected.fields.get(CONTENT_TYPE_FIELD.name) is None:
expected.fields.set_field(CONTENT_TYPE_FIELD)
assert actual.fields == expected.fields

if case.request.body:
Expand All @@ -1647,6 +1661,9 @@ async def test_serialize_http_response(case: HTTPMessageTestCase) -> None:
expected = case.request

assert actual is not None
# Remove content-type from expected, we're re-using the request cases for brevity
if expected.fields.get(CONTENT_TYPE_FIELD.name) is not None:
del expected.fields[CONTENT_TYPE_FIELD.name]
assert actual.fields == expected.fields
assert actual.status == expected.status

Expand Down