|
7 | 7 | import os |
8 | 8 | import sys |
9 | 9 | import time |
10 | | -from typing import Any, Iterable, List, Optional |
| 10 | +from logging import getLogger |
| 11 | +from typing import Any, Dict, Iterable, List, Optional |
11 | 12 |
|
12 | 13 | import requests |
13 | 14 | from packaging import version |
|
23 | 24 | ) |
24 | 25 | from .helpers import handle_url_overrides |
25 | 26 |
|
| 27 | +logger = getLogger(__name__) |
| 28 | + |
26 | 29 |
|
27 | 30 | def get_all_pages(ctx, operation: OpenAPIOperation, args: List[str]): |
28 | 31 | """ |
@@ -87,13 +90,18 @@ def do_request( |
87 | 90 |
|
88 | 91 | # Print response debug info is requested |
89 | 92 | if ctx.debug_request: |
90 | | - _print_request_debug_info(method, url, headers, body) |
| 93 | + # Multiline log entries aren't ideal, we should consider |
| 94 | + # using single-line structured logging in the future. |
| 95 | + logger.debug( |
| 96 | + "\n" |
| 97 | + + "\n".join(_format_request_for_log(method, url, headers, body)) |
| 98 | + ) |
91 | 99 |
|
92 | 100 | result = method(url, headers=headers, data=body, verify=API_CA_PATH) |
93 | 101 |
|
94 | 102 | # Print response debug info is requested |
95 | 103 | if ctx.debug_request: |
96 | | - _print_response_debug_info(result) |
| 104 | + logger.debug("\n" + "\n".join(_format_response_for_log(result))) |
97 | 105 |
|
98 | 106 | while _check_retry(result) and not ctx.no_retry and ctx.retry_count < 3: |
99 | 107 | time.sleep(_get_retry_after(result.headers)) |
@@ -282,38 +290,61 @@ def _build_request_body(ctx, operation, parsed_args) -> Optional[str]: |
282 | 290 | return json.dumps(_traverse_request_body(expanded_json)) |
283 | 291 |
|
284 | 292 |
|
285 | | -def _print_request_debug_info(method, url, headers, body): |
| 293 | +def _format_request_for_log( |
| 294 | + method: Any, |
| 295 | + url: str, |
| 296 | + headers: Dict[str, str], |
| 297 | + body: str, |
| 298 | +) -> List[str]: |
286 | 299 | """ |
287 | | - Prints debug info for an HTTP request |
| 300 | + Builds a debug output for the given request. |
| 301 | +
|
| 302 | + :param method: The HTTP method of the request. |
| 303 | + :param url: The URL of the request. |
| 304 | + :param headers: The headers of the request. |
| 305 | + :param body: The body of the request. |
| 306 | +
|
| 307 | + :returns: The lines of the generated debug output. |
288 | 308 | """ |
289 | | - print(f"> {method.__name__.upper()} {url}", file=sys.stderr) |
| 309 | + result = [f"> {method.__name__.upper()} {url}"] |
| 310 | + |
290 | 311 | for k, v in headers.items(): |
291 | 312 | # If this is the Authorization header, sanitize the token |
292 | 313 | if k.lower() == "authorization": |
293 | 314 | v = "Bearer " + "*" * 64 |
294 | | - print(f"> {k}: {v}", file=sys.stderr) |
295 | | - print("> Body:", file=sys.stderr) |
296 | | - print("> ", body or "", file=sys.stderr) |
297 | | - print("> ", file=sys.stderr) |
| 315 | + |
| 316 | + result.append(f"> {k}: {v}") |
| 317 | + |
| 318 | + result.extend(["> Body:", f"> {body or ''}", f"> "]) |
| 319 | + |
| 320 | + return result |
298 | 321 |
|
299 | 322 |
|
300 | | -def _print_response_debug_info(response): |
| 323 | +def _format_response_for_log( |
| 324 | + response: requests.Response, |
| 325 | +): |
301 | 326 | """ |
302 | | - Prints debug info for a response from requests |
| 327 | + Builds a debug output for the given request. |
| 328 | +
|
| 329 | + :param response: The HTTP response to format. |
| 330 | +
|
| 331 | + :returns: The lines of the generated debug output. |
303 | 332 | """ |
| 333 | + |
304 | 334 | # these come back as ints, convert to HTTP version |
305 | 335 | http_version = response.raw.version / 10 |
306 | 336 | body = response.content.decode("utf-8", errors="replace") |
307 | 337 |
|
308 | | - print( |
309 | | - f"< HTTP/{http_version:.1f} {response.status_code} {response.reason}", |
310 | | - file=sys.stderr, |
311 | | - ) |
| 338 | + result = [ |
| 339 | + f"< HTTP/{http_version:.1f} {response.status_code} {response.reason}" |
| 340 | + ] |
| 341 | + |
312 | 342 | for k, v in response.headers.items(): |
313 | | - print(f"< {k}: {v}", file=sys.stderr) |
314 | | - print("< Body:", file=sys.stderr) |
315 | | - print("< ", body or "", file=sys.stderr) |
316 | | - print("< ", file=sys.stderr) |
| 343 | + result.append(f"< {k}: {v}") |
| 344 | + |
| 345 | + result.extend(["< Body:", f"< {body or ''}", "< "]) |
| 346 | + |
| 347 | + return result |
317 | 348 |
|
318 | 349 |
|
319 | 350 | def _attempt_warn_old_version(ctx, result): |
|
0 commit comments