Skip to content

Commit 01644b6

Browse files
authored
Merge pull request #996 from sphinx-contrib/add-publish-debug-for-reqrsp-body
Add support for logging request/response body data
2 parents 8bcc8f2 + ef5445c commit 01644b6

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/configuration.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,10 @@ Advanced publishing configuration
15191519

15201520
Switched from boolean to string for setting new debugging options.
15211521

1522+
.. versionchanged:: 2.6
1523+
1524+
Introduce the ``headers_and_data`` option.
1525+
15221526
.. warning::
15231527

15241528
Enabling certain debugging options may reveal information such as
@@ -1537,6 +1541,7 @@ Advanced publishing configuration
15371541
- ``deprecated``: Log warnings when a deprecated API call is used
15381542
(*for development purposes*).
15391543
- ``headers``: Log requests and responses, including their headers.
1544+
- ``headers_and_data``: Log header data along with request/response bodies.
15401545
- ``urllib3``: Enable urllib3 library debugging messages.
15411546

15421547
An example debugging configuration is as follows:

sphinxcontrib/confluencebuilder/debug.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ class PublishDebug(Flag):
1818

1919
# do not perform any logging
2020
none = auto()
21+
# log raw requests/responses in stdout with body data
22+
data = auto()
2123
# logs warnings when confluence reports a deprecated api call
2224
deprecated = auto()
2325
# log raw requests/responses in stdout with header data (redacted auth)
2426
headers = auto()
27+
# log both header and body data
28+
headers_and_data = headers | data
2529
# log raw requests/responses in stdout with header data (no redactions)
2630
_headers_raw = auto()
2731
headers_raw = headers | _headers_raw
2832
# log urllib3-supported debug messages
2933
urllib3 = auto()
3034
# enable all logging
31-
all = headers | urllib3
35+
all = data | headers | urllib3
3236
# enable all developer logging
3337
developer = deprecated | all

sphinxcontrib/confluencebuilder/rest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ def _format_error(self, rsp, path):
403403
def _process_request(self, method, path, *args, **kwargs):
404404
publish_debug_opts = self.config.confluence_publish_debug
405405
dump = PublishDebug.headers in publish_debug_opts
406+
dump_body = PublishDebug.headers_and_data in publish_debug_opts
406407

407408
rest_url = f'{self.url}{path}'
408409
base_req = requests.Request(method, rest_url, *args, **kwargs)
@@ -431,6 +432,15 @@ def _process_request(self, method, path, *args, **kwargs):
431432
print('\n'.join(f'{k}: {v}' for k, v in filtered_headers.items()))
432433
print('', flush=True)
433434

435+
if dump_body and req.body:
436+
print('(debug) Request data]')
437+
try:
438+
json_data = json.dumps(json.loads(req.body), indent=2)
439+
print(json_data)
440+
except TypeError:
441+
print('(non-json)')
442+
print('', flush=True)
443+
434444
# perform the rest request
435445
rsp = self.session.send(req, timeout=self.timeout)
436446

@@ -441,6 +451,16 @@ def _process_request(self, method, path, *args, **kwargs):
441451
print('\n'.join(f'{k}: {v}' for k, v in rsp.headers.items()))
442452
print('', flush=True)
443453

454+
if dump_body and rsp.text:
455+
print('(debug) Response data]')
456+
try:
457+
rsp.encoding = self.CONFLUENCE_DEFAULT_ENCODING
458+
json_data = json.dumps(json.loads(rsp.text), indent=2)
459+
print(json_data)
460+
except ValueError:
461+
print('(non-json)')
462+
print('', flush=True)
463+
444464
# if confluence or a proxy reports a retry-after delay (to pace us),
445465
# track it to delay the next request made
446466
# (https://datatracker.ietf.org/doc/html/rfc2616.html#section-14.37)

0 commit comments

Comments
 (0)