diff sat/plugins/plugin_comp_ap_gateway/__init__.py @ 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 4479f6074bc8
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):