Skip to content

Commit

Permalink
Merge pull request #996 from sphinx-contrib/add-publish-debug-for-req…
Browse files Browse the repository at this point in the history
…rsp-body

Add support for logging request/response body data
  • Loading branch information
jdknight authored Jun 27, 2024
2 parents 8bcc8f2 + ef5445c commit 01644b6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,10 @@ Advanced publishing configuration

Switched from boolean to string for setting new debugging options.

.. versionchanged:: 2.6

Introduce the ``headers_and_data`` option.

.. warning::

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

An example debugging configuration is as follows:
Expand Down
6 changes: 5 additions & 1 deletion sphinxcontrib/confluencebuilder/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ class PublishDebug(Flag):

# do not perform any logging
none = auto()
# log raw requests/responses in stdout with body data
data = auto()
# logs warnings when confluence reports a deprecated api call
deprecated = auto()
# log raw requests/responses in stdout with header data (redacted auth)
headers = auto()
# log both header and body data
headers_and_data = headers | data
# log raw requests/responses in stdout with header data (no redactions)
_headers_raw = auto()
headers_raw = headers | _headers_raw
# log urllib3-supported debug messages
urllib3 = auto()
# enable all logging
all = headers | urllib3
all = data | headers | urllib3
# enable all developer logging
developer = deprecated | all
20 changes: 20 additions & 0 deletions sphinxcontrib/confluencebuilder/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def _format_error(self, rsp, path):
def _process_request(self, method, path, *args, **kwargs):
publish_debug_opts = self.config.confluence_publish_debug
dump = PublishDebug.headers in publish_debug_opts
dump_body = PublishDebug.headers_and_data in publish_debug_opts

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

if dump_body and req.body:
print('(debug) Request data]')
try:
json_data = json.dumps(json.loads(req.body), indent=2)
print(json_data)
except TypeError:
print('(non-json)')
print('', flush=True)

# perform the rest request
rsp = self.session.send(req, timeout=self.timeout)

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

if dump_body and rsp.text:
print('(debug) Response data]')
try:
rsp.encoding = self.CONFLUENCE_DEFAULT_ENCODING
json_data = json.dumps(json.loads(rsp.text), indent=2)
print(json_data)
except ValueError:
print('(non-json)')
print('', flush=True)

# if confluence or a proxy reports a retry-after delay (to pace us),
# track it to delay the next request made
# (https://datatracker.ietf.org/doc/html/rfc2616.html#section-14.37)
Expand Down

0 comments on commit 01644b6

Please sign in to comment.