Skip to content
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

make all the things strings #13

Merged
merged 5 commits into from
Jan 12, 2024
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
22 changes: 16 additions & 6 deletions src/supergood/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from .api import Api
from .constants import *
from .helpers import redact_values, safe_decode, safe_parse_json
from .helpers import decode_headers, redact_values, safe_decode, safe_parse_json
from .logger import Logger
from .remote_config import get_vendor_endpoint_from_config, parse_remote_config_json
from .repeating_thread import RepeatingThread
Expand Down Expand Up @@ -158,6 +158,7 @@ def _should_ignore(
def _cache_request(self, request_id, url, method, body, headers):
request = {}
try:
url = safe_decode(url) # we do this first so the urlparse isn't also bytes
host_domain = urlparse(url).hostname
request["metadata"] = {}
# Check that we should cache the request
Expand All @@ -178,11 +179,11 @@ def _cache_request(self, request_id, url, method, body, headers):
filtered_headers = (
{}
if (not self.base_config["logRequestHeaders"] or headers is None)
else dict(headers)
else decode_headers(dict(headers))
)
request["request"] = {
"id": request_id,
"method": method,
"method": safe_decode(method),
"url": url,
"body": filtered_body,
"headers": filtered_headers,
Expand Down Expand Up @@ -216,13 +217,13 @@ def _cache_response(
filtered_headers = (
{}
if not self.base_config["logResponseHeaders"]
else dict(response_headers)
else decode_headers(dict(response_headers))
)
response = {
"body": filtered_body,
"headers": filtered_headers,
"status": response_status,
"statusText": response_status_text,
"statusText": safe_decode(response_status_text),
"respondedAt": datetime.now().isoformat(),
}
self._response_cache[request_id] = {
Expand Down Expand Up @@ -324,8 +325,17 @@ def flush_cache(self, force=False) -> None:
self.log.debug(f"Flushing {len(data)} items")
self.api.post_events(data)
except Exception:
payload = self._build_log_payload()
trace = "".join(traceback.format_exc())
try:
urls = []
for entry in data:
if entry.get("request", None):
urls.append(entry.get("request").get("url"))
num_events = len(data)
payload = self._build_log_payload(num_events=num_events, urls=urls)
except Exception:
# something is really messed up, just report out
payload = self._build_log_payload()
self.log.error(ERRORS["POSTING_EVENTS"], trace, payload)
finally: # always occurs, even from internal returns
for response_key in response_keys:
Expand Down
15 changes: 13 additions & 2 deletions src/supergood/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,21 @@ def safe_decode(input, encoding="utf-8"):
try:
if isinstance(input, bytes) and input[:2] == GZIP_START_BYTES:
return gzip.decompress(input)

if isinstance(input, str):
return input

return input.decode(encoding)
except Exception:
return str(input)


def decode_headers(headers, encoding="utf-8"):
new_headers = {}
for key, value in headers.items():
decoded_key = key
if isinstance(key, bytes):
decoded_key = safe_decode(key, encoding)
decoded_value = value
if isinstance(value, bytes):
decoded_value = safe_decode(value, encoding)
new_headers[decoded_key] = decoded_value
return new_headers