comparison sat/plugins/plugin_comp_ap_gateway/__init__.py @ 3881:2e4a0f6050bd

component AP gateway: better verbose logging: gather data to show according to verbosity and log it all at once with a small `<<<` or `>>>` indicating the direction. With a verbosity of 3, headers are also shown.
author Goffi <goffi@goffi.org>
date Wed, 31 Aug 2022 17:07:03 +0200
parents 03761f8ba8bb
children 6da749bbf320
comparison
equal deleted inserted replaced
3880:03761f8ba8bb 3881:2e4a0f6050bd
1164 @param doc: document to send 1164 @param doc: document to send
1165 """ 1165 """
1166 if self.verbose: 1166 if self.verbose:
1167 __, actor_args = self.parseAPURL(actor_id) 1167 __, actor_args = self.parseAPURL(actor_id)
1168 actor_account = actor_args[0] 1168 actor_account = actor_args[0]
1169 log.info( 1169 to_log = [
1170 f"==> {actor_account} is signing and posting to {url}:\n{pformat(doc)}" 1170 "",
1171 ) 1171 f">>> {actor_account} is signing and posting to {url}:\n{pformat(doc)}"
1172 ]
1172 1173
1173 p_url = parse.urlparse(url) 1174 p_url = parse.urlparse(url)
1174 body = json.dumps(doc).encode() 1175 body = json.dumps(doc).encode()
1175 digest_algo, digest_hash = self.getDigest(body) 1176 digest_algo, digest_hash = self.getDigest(body)
1176 digest = f"{digest_algo}={digest_hash}" 1177 digest = f"{digest_algo}={digest_hash}"
1181 "Date": http.datetimeToString().decode(), 1182 "Date": http.datetimeToString().decode(),
1182 "Digest": digest 1183 "Digest": digest
1183 } 1184 }
1184 headers, __ = self.getSignatureData(self.getKeyId(actor_id), headers) 1185 headers, __ = self.getSignatureData(self.getKeyId(actor_id), headers)
1185 1186
1186 headers["Content-Type"] = ( 1187 if self.verbose:
1187 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' 1188 if self.verbose>=3:
1188 ) 1189 h_to_log = "\n".join(f" {k}: {v}" for k,v in headers.items())
1190 to_log.append(f" headers:\n{h_to_log}")
1191 to_log.append("---")
1192 log.info("\n".join(to_log))
1193
1189 resp = await treq.post( 1194 resp = await treq.post(
1190 url, 1195 url,
1191 body, 1196 body,
1192 headers=headers, 1197 headers=headers,
1193 ) 1198 )