comparison 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
comparison
equal deleted inserted replaced
3846:cc13efdd8360 3847:aaa4e7815ba8
108 RE_ALLOWED_UNQUOTED = re.compile(r"^[a-zA-Z0-9_-]+$") 108 RE_ALLOWED_UNQUOTED = re.compile(r"^[a-zA-Z0-9_-]+$")
109 109
110 110
111 class APGateway: 111 class APGateway:
112 IMPORT_NAME = IMPORT_NAME 112 IMPORT_NAME = IMPORT_NAME
113 # show data send or received through HTTP, used for debugging
114 # 1: log POST objects
115 # 2: log POST and GET objects
116 # 3: log POST and GET objects with HTTP headers for GET requests
117 verbose = 0
113 118
114 def __init__(self, host): 119 def __init__(self, host):
115 self.host = host 120 self.host = host
116 self.initialised = False 121 self.initialised = False
117 self.client = None 122 self.client = None
965 970
966 @param url: AP server endpoint 971 @param url: AP server endpoint
967 @param actor_id: originating actor ID (URL) 972 @param actor_id: originating actor ID (URL)
968 @param doc: document to send 973 @param doc: document to send
969 """ 974 """
975 if self.verbose:
976 __, actor_args = self.parseAPURL(actor_id)
977 actor_account = actor_args[0]
978 log.info(
979 f"==> {actor_account} is signing and posting to {url}:\n{pformat(doc)}"
980 )
981
970 p_url = parse.urlparse(url) 982 p_url = parse.urlparse(url)
971 body = json.dumps(doc).encode() 983 body = json.dumps(doc).encode()
972 digest_algo, digest_hash = self.getDigest(body) 984 digest_algo, digest_hash = self.getDigest(body)
973 digest = f"{digest_algo}={digest_hash}" 985 digest = f"{digest_algo}={digest_hash}"
974 986
989 headers=headers, 1001 headers=headers,
990 ) 1002 )
991 if resp.code >= 400: 1003 if resp.code >= 400:
992 text = await resp.text() 1004 text = await resp.text()
993 log.warning(f"POST request to {url} failed [{resp.code}]: {text}") 1005 log.warning(f"POST request to {url} failed [{resp.code}]: {text}")
1006 elif self.verbose:
1007 log.info(f"==> response code: {resp.code}")
994 return resp 1008 return resp
995 1009
996 def _publishMessage(self, mess_data_s: str, service_s: str, profile: str): 1010 def _publishMessage(self, mess_data_s: str, service_s: str, profile: str):
997 mess_data: dict = data_format.deserialise(mess_data_s) # type: ignore 1011 mess_data: dict = data_format.deserialise(mess_data_s) # type: ignore
998 service = jid.JID(service_s) 1012 service = jid.JID(service_s)