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
This commit is contained in:
parent
f4af47a929
commit
cd9d1d27e3
2 changed files with 42 additions and 0 deletions
|
|
@ -110,6 +110,11 @@ RE_ALLOWED_UNQUOTED = re.compile(r"^[a-zA-Z0-9_-]+$")
|
|||
|
||||
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 @@ class APGateway:
|
|||
@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 @@ class APGateway:
|
|||
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):
|
||||
|
|
|
|||
|
|
@ -688,6 +688,20 @@ class HTTPAPGServer(web_resource.Resource):
|
|||
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 @@ class HTTPAPGServer(web_resource.Resource):
|
|||
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"):
|
||||
|
|
|
|||
Loading…
Reference in a new issue