Skip to content

Commit

Permalink
Merge pull request #198 from anaconda/debug_api
Browse files Browse the repository at this point in the history
Add debug logging to requests
  • Loading branch information
mcg1969 authored Oct 7, 2024
2 parents 255d5c2 + e993b11 commit 3fa58e9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ae5_tools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@
"time": "timestamp/ms",
}

# Used by _response_hook to help log redirected URLs
last_redirect = None


# This is a wrapper around session.request that enables us to do debug logging
def _response_hook(resp, *args, **kwargs):
global last_redirect
req = resp.request
url = req.url
code = resp.status_code
prefix = req.method.upper()
if url == last_redirect:
prefix = "-> " + prefix
print(prefix, url, code, file=sys.stderr)
last_redirect = resp.headers["location"] if 300 <= code < 400 else None


class EmptyRecordList(list):
def __init__(self, record_type, columns=None):
Expand Down Expand Up @@ -335,6 +351,9 @@ def _build_requests_session() -> Session:
adapter: HTTPAdapter = HTTPAdapter(max_retries=retries)
session.mount(prefix="https://", adapter=adapter)

if get_env_var(name="API_DEBUG"):
session.hooks["response"].append(_response_hook)

return session

@staticmethod
Expand Down

0 comments on commit 3fa58e9

Please sign in to comment.