-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[low-code] convert request.body to a dict when converting to AirbyteLogMessage #20557
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2398adf
convert request body
girarda 93aabe7
fix tests
girarda 6d8ccd7
test body data
girarda 0adf45c
more tests
girarda c5547b8
more tests
girarda f40a56c
_
girarda 84bd5dc
return stacktrace
girarda 0912538
pretty print
girarda b6f62d6
Revert "pretty print"
girarda a6d0a93
Revert "Revert "pretty print""
girarda fa4ea8b
replace \n
girarda dcb8390
Merge branch 'master' into alex/request_body
girarda 3452bba
missing type hint
girarda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import json | ||
import logging | ||
from dataclasses import InitVar, dataclass, field | ||
from json import JSONDecodeError | ||
from typing import Any, Iterable, List, Mapping, MutableMapping, Optional, Union | ||
|
||
import requests | ||
|
@@ -367,7 +368,7 @@ def read_records( | |
stream_slice = stream_slice or {} # None-check | ||
self.paginator.reset() | ||
records_generator = self._read_pages( | ||
self.parse_records_and_emit_request_and_responses, | ||
self._parse_records_and_emit_request_and_responses, | ||
stream_slice, | ||
stream_state, | ||
) | ||
|
@@ -405,23 +406,42 @@ def state(self, value: StreamState): | |
"""State setter, accept state serialized by state getter.""" | ||
self.stream_slicer.update_cursor(value) | ||
|
||
def parse_records_and_emit_request_and_responses(self, request, response, stream_slice, stream_state) -> Iterable[StreamData]: | ||
def _parse_records_and_emit_request_and_responses(self, request, response, stream_slice, stream_state) -> Iterable[StreamData]: | ||
# Only emit requests and responses when running in debug mode | ||
if self.logger.isEnabledFor(logging.DEBUG): | ||
yield self._create_trace_message_from_request(request) | ||
yield self._create_trace_message_from_response(response) | ||
yield _prepared_request_to_airbyte_message(request) | ||
yield _response_to_airbyte_message(response) | ||
# Not great to need to call _read_pages which is a private method | ||
# A better approach would be to extract the HTTP client from the HttpStream and call it directly from the HttpRequester | ||
yield from self.parse_response(response, stream_slice=stream_slice, stream_state=stream_state) | ||
|
||
def _create_trace_message_from_request(self, request: requests.PreparedRequest): | ||
# FIXME: this should return some sort of trace message | ||
request_dict = {"url": request.url, "http_method": request.method, "headers": dict(request.headers), "body": request.body} | ||
log_message = filter_secrets(f"request:{json.dumps(request_dict)}") | ||
return AirbyteMessage(type=MessageType.LOG, log=AirbyteLogMessage(level=Level.INFO, message=log_message)) | ||
|
||
def _create_trace_message_from_response(self, response: requests.Response): | ||
# FIXME: this should return some sort of trace message | ||
response_dict = {"body": response.text, "headers": dict(response.headers), "status_code": response.status_code} | ||
log_message = filter_secrets(f"response:{json.dumps(response_dict)}") | ||
return AirbyteMessage(type=MessageType.LOG, log=AirbyteLogMessage(level=Level.INFO, message=log_message)) | ||
|
||
def _prepared_request_to_airbyte_message(request: requests.PreparedRequest) -> AirbyteMessage: | ||
# FIXME: this should return some sort of trace message | ||
request_dict = { | ||
"url": request.url, | ||
"http_method": request.method, | ||
"headers": dict(request.headers), | ||
"body": _body_binary_string_to_dict(request.body), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of assigning the body directly, delegate to |
||
} | ||
log_message = filter_secrets(f"request:{json.dumps(request_dict)}") | ||
return AirbyteMessage(type=MessageType.LOG, log=AirbyteLogMessage(level=Level.INFO, message=log_message)) | ||
|
||
|
||
def _body_binary_string_to_dict(body_str: str) -> Optional[Mapping[str, str]]: | ||
if body_str: | ||
if isinstance(body_str, (bytes, bytearray)): | ||
body_str = body_str.decode() | ||
try: | ||
return json.loads(body_str) | ||
except JSONDecodeError: | ||
return {k: v for k, v in [s.split("=") for s in body_str.split("&")]} | ||
else: | ||
return None | ||
|
||
|
||
def _response_to_airbyte_message(response: requests.Response) -> AirbyteMessage: | ||
# FIXME: this should return some sort of trace message | ||
response_dict = {"body": response.text, "headers": dict(response.headers), "status_code": response.status_code} | ||
log_message = filter_secrets(f"response:{json.dumps(response_dict)}") | ||
return AirbyteMessage(type=MessageType.LOG, log=AirbyteLogMessage(level=Level.INFO, message=log_message)) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to keep them FIXMEs for now 😞