Skip to content

Commit 76d57fb

Browse files
committed
update protocolutils to new http interfaces
1 parent 4646525 commit 76d57fb

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

python-packages/smithy-python/smithy_python/protocolutils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from typing import NamedTuple
33

4-
from .interfaces.http import Response
4+
from .interfaces.http import HTTPResponse
55
from .types import Document
66
from .utils import expect_type
77

@@ -31,7 +31,7 @@ class RestJsonErrorInfo(NamedTuple):
3131

3232

3333
async def parse_rest_json_error_info(
34-
http_response: Response, check_body: bool = True
34+
http_response: HTTPResponse, check_body: bool = True
3535
) -> RestJsonErrorInfo:
3636
"""Parses generic RestJson error info from an HTTP response.
3737
@@ -43,12 +43,12 @@ async def parse_rest_json_error_info(
4343
message: str | None = None
4444
json_body: dict[str, Document] | None = None
4545

46-
for header_key, header_value in http_response.headers:
47-
if header_key.lower() == _REST_JSON_CODE_HEADER:
48-
code = header_value
46+
for field in http_response.fields:
47+
if field.name.lower() == _REST_JSON_CODE_HEADER:
48+
code = field.values[0]
4949

5050
if check_body:
51-
if body := await http_response.body.read():
51+
if body := await http_response.consume_body():
5252
json_body = json.loads(body)
5353

5454
if json_body:

python-packages/smithy-python/tests/unit/test_protocolutils.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1+
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
114
import json
15+
from collections.abc import AsyncIterator
216

317
import pytest
418

5-
from smithy_python._private.http import Response
6-
from smithy_python.interfaces.http import HeadersList
19+
from smithy_python._private.http import HTTPResponse, tuples_list_to_fields
20+
from smithy_python.async_utils import async_list
721
from smithy_python.protocolutils import RestJsonErrorInfo, parse_rest_json_error_info
822
from smithy_python.types import Document
923

1024

11-
class _AsyncReader:
12-
def __init__(self, body: str):
13-
self._body: bytes = body.encode("utf-8")
14-
15-
async def read(self, size: int = -1) -> bytes:
16-
result: bytes = self._body
17-
if size <= 0:
18-
self._body = b""
19-
else:
20-
result = self._body[:size]
21-
self._body = self._body[size:]
22-
return result
23-
24-
2525
@pytest.mark.parametrize(
2626
"headers, body, expected",
2727
[
@@ -82,17 +82,19 @@ async def read(self, size: int = -1) -> bytes:
8282
],
8383
)
8484
async def test_parse_rest_json_error_info(
85-
headers: HeadersList, body: Document, expected: RestJsonErrorInfo
85+
headers: list[tuple[str, str]], body: Document, expected: RestJsonErrorInfo
8686
) -> None:
87-
response = Response(
88-
status_code=400, headers=headers, body=_AsyncReader(json.dumps(body))
87+
response = HTTPResponse(
88+
status=400,
89+
fields=tuples_list_to_fields(headers),
90+
body=async_list([json.dumps(body).encode()]),
8991
)
9092
actual = await parse_rest_json_error_info(response)
9193
assert actual == expected
9294

9395

9496
class _ExceptionThrowingBody:
95-
async def read(self, size: int = -1) -> bytes:
97+
def __aiter__(self) -> AsyncIterator[bytes]:
9698
raise Exception("Body unexpectedly accessed")
9799

98100

@@ -117,8 +119,10 @@ async def read(self, size: int = -1) -> bytes:
117119
],
118120
)
119121
async def test_parse_rest_json_error_info_without_body(
120-
headers: HeadersList, expected: RestJsonErrorInfo
122+
headers: list[tuple[str, str]], expected: RestJsonErrorInfo
121123
) -> None:
122-
response = Response(status_code=400, headers=headers, body=_ExceptionThrowingBody())
124+
response = HTTPResponse(
125+
status=400, fields=tuples_list_to_fields(headers), body=_ExceptionThrowingBody()
126+
)
123127
actual = await parse_rest_json_error_info(response, check_body=False)
124128
assert actual == expected

0 commit comments

Comments
 (0)