changeset 3847:aaa4e7815ba8

component AP gateway: new `verbose` attribute in AP gateway to activate debug logs: when verbose is not null, object posted or returned on GET request are logged. Check comments for vebosity level
author Goffi <goffi@goffi.org>
date Thu, 14 Jul 2022 12:55:30 +0200
parents cc13efdd8360
children e9c380ef41c8
files sat/plugins/plugin_comp_ap_gateway/__init__.py sat/plugins/plugin_comp_ap_gateway/http_server.py
diffstat 2 files changed, 42 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_ap_gateway/__init__.py	Thu Jul 14 12:55:30 2022 +0200
+++ b/sat/plugins/plugin_comp_ap_gateway/__init__.py	Thu Jul 14 12:55:30 2022 +0200
@@ -110,6 +110,11 @@
 
 class APGateway:
     IMPORT_NAME = IMPORT_NAME
+    # show data send or received through HTTP, used for debugging
+    # 1: log POST objects
+    # 2: log POST and GET objects
+    # 3: log POST and GET objects with HTTP headers for GET requests
+    verbose = 0
 
     def __init__(self, host):
         self.host = host
@@ -967,6 +972,13 @@
         @param actor_id: originating actor ID (URL)
         @param doc: document to send
         """
+        if self.verbose:
+            __, actor_args = self.parseAPURL(actor_id)
+            actor_account = actor_args[0]
+            log.info(
+                f"==> {actor_account} is signing and posting to {url}:\n{pformat(doc)}"
+            )
+
         p_url = parse.urlparse(url)
         body = json.dumps(doc).encode()
         digest_algo, digest_hash = self.getDigest(body)
@@ -991,6 +1003,8 @@
         if resp.code >= 400:
             text = await resp.text()
             log.warning(f"POST request to {url} failed [{resp.code}]: {text}")
+        elif self.verbose:
+            log.info(f"==> response code: {resp.code}")
         return resp
 
     def _publishMessage(self, mess_data_s: str, service_s: str, profile: str):
--- a/sat/plugins/plugin_comp_ap_gateway/http_server.py	Thu Jul 14 12:55:30 2022 +0200
+++ b/sat/plugins/plugin_comp_ap_gateway/http_server.py	Thu Jul 14 12:55:30 2022 +0200
@@ -688,6 +688,20 @@
         request: "HTTPRequest",
         signing_actor: Optional[str] = None
     ) -> None:
+        if self.apg.verbose:
+            try:
+                data = json.load(request.content)
+            except (json.JSONDecodeError, ValueError) as e:
+                pass
+            else:
+                from pprint import pformat
+                log.info(
+                    f"==> got {request.method.decode()} request:\n{pformat(data)}\n---"
+                )
+
+            finally:
+                request.content.seek(0)
+
         path = request.path.decode()
         ap_url = parse.urljoin(
             f"https://{self.apg.public_url}",
@@ -725,6 +739,20 @@
         if ret_data is not None:
             request.setHeader("content-type", CONTENT_TYPE_AP)
             request.write(json.dumps(ret_data).encode())
+            if self.apg.verbose>=2:
+                from pprint import pformat
+                to_log = [f"==> ret (code: {request.code}):\n{pformat(ret_data)}"]
+                log.info(
+
+                )
+                if self.apg.verbose>=3:
+                    headers = "\n".join(
+                        f"    {k.decode()}: {v.decode()}"
+                        for k,v in request.getAllHeaders().items()
+                    )
+                    to_log.append("  headers:\n{headers}")
+                to_log.append("---")
+                log.info("\n".join(to_log))
         request.finish()
 
     async def APPostRequest(self, request: "HTTPRequest"):