Skip to content

fix parse_response to handle streaming responses #6415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2022
Merged

Conversation

thrau
Copy link
Member

@thrau thrau commented Jul 7, 2022

This PR fixes an issue/limitation of the parse_response operation in our aws client module.

The problem was that we were not really implementing the parsing logic of botocore correctly, in particular when streaming binary responses are expected, like in s3.GetObject, which caused a problem here: https://github.com/boto/botocore/blob/ae44d1de241c13626d05a9301cf62e460833c950/botocore/parsers.py#L910-L914

In boto, when you call s3_client.get_object(...), the Body attribute of the response (i.e., the payload), will not actually be bytes, but a readable io object. This PR introduces the same behavior for the parse_response method.

I modeled the implementation after the version in boto core https://github.com/boto/botocore/blob/ae44d1de241c13626d05a9301cf62e460833c950/botocore/endpoint.py#L65-L74

Future work is has_event_stream_output, which I think covers things like the kinesis shard iterator.

@thrau thrau requested a review from alexrashed July 7, 2022 19:11
@thrau thrau temporarily deployed to localstack-ext-tests July 7, 2022 19:11 Inactive
@coveralls
Copy link

coveralls commented Jul 7, 2022

Coverage Status

Coverage decreased (-0.03%) to 91.968% when pulling 7aff3ec on asf-fix-parse-response into 6f648b3 on v1.

@github-actions
Copy link

github-actions bot commented Jul 7, 2022

LocalStack integration with Pro

       3 files         3 suites   1h 5m 31s ⏱️
1 121 tests 1 081 ✔️ 40 💤 0
1 436 runs  1 367 ✔️ 69 💤 0

Results for commit 8fb5eed.

Copy link
Member

@alexrashed alexrashed left a comment

Choose a reason for hiding this comment

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

Great change! I think we can assume that the event streaming response needs to be a stream as well, but we can address this in the next iteration as well.

Comment on lines +57 to +58
if response_dict["status_code"] >= 300:
response_dict["body"] = response.data
Copy link
Member

Choose a reason for hiding this comment

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

Just an FYI: This explicitly contains any redirections (3xx). There are no 3xx http response status codes in the specifications, but these responses might have a response body. But imho it's safe to assume that there are not streaming responses in 300+ status codes.

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, just doing what the client does 🤷

Comment on lines 60 to 66
# TODO
LOG.warning(
"don't know how to parse an event stream output for %s.%s",
operation.service_model.service_name,
operation.name,
)
response_dict["body"] = response.data
Copy link
Member

Choose a reason for hiding this comment

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

I just dig a bit into this:

  • The parsers create an instance of the EventStream with response['body'] in this case: parsers.py#L348
  • The EventStream class in botocore expects this param to be a raw bytes stream:
    eventstream.py#L595

So I think we should just use the _ResponseStream here as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

cool, yea i added an additional _RawStream wrapper that exposes the interface botocore's EventStream object seems to expect. i'm too lazy to test this right now, so leaving a TODO for the next person :P

@localstack-bot localstack-bot force-pushed the asf-fix-parse-response branch from 8fb5eed to e4bd11d Compare July 8, 2022 08:12
@localstack-bot localstack-bot temporarily deployed to localstack-ext-tests July 8, 2022 08:12 Inactive
@localstack-bot localstack-bot force-pushed the asf-fix-parse-response branch from e4bd11d to 26eb419 Compare July 8, 2022 09:02
@localstack-bot localstack-bot temporarily deployed to localstack-ext-tests July 8, 2022 09:03 Inactive
@localstack-bot localstack-bot force-pushed the asf-fix-parse-response branch from 26eb419 to cdd7552 Compare July 8, 2022 09:31
@localstack-bot localstack-bot temporarily deployed to localstack-ext-tests July 8, 2022 09:32 Inactive
@localstack-bot localstack-bot force-pushed the asf-fix-parse-response branch from cdd7552 to 81dada4 Compare July 8, 2022 09:50
@localstack-bot localstack-bot temporarily deployed to localstack-ext-tests July 8, 2022 09:50 Inactive
@thrau thrau temporarily deployed to localstack-ext-tests July 8, 2022 10:56 Inactive
@thrau thrau merged commit f28aecd into v1 Jul 8, 2022
@localstack localstack locked and limited conversation to collaborators Jul 8, 2022
@alexrashed alexrashed deleted the asf-fix-parse-response branch July 8, 2022 12:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants