# HG changeset patch # User Goffi # Date 1657796130 -7200 # Node ID aaa4e7815ba82eb9dd6b32982181d8075cd00187 # Parent cc13efdd8360e5d84909b880df57274c356a2ae3 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 diff -r cc13efdd8360 -r aaa4e7815ba8 sat/plugins/plugin_comp_ap_gateway/__init__.py --- 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): diff -r cc13efdd8360 -r aaa4e7815ba8 sat/plugins/plugin_comp_ap_gateway/http_server.py --- 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"):