relay: add http request debugger
authorkaniini <kaniini@dereferenced.org>
Sun, 18 Nov 2018 00:07:36 +0000 (00:07 +0000)
committerkaniini <kaniini@dereferenced.org>
Sun, 18 Nov 2018 00:09:36 +0000 (00:09 +0000)
relay/actor.py
relay/http_debug.py [new file with mode: 0644]
relay/remote_actor.py

index 8ddcf0e..99a7dda 100644 (file)
@@ -9,6 +9,7 @@ import simplejson as json
 import cgi
 from Crypto.PublicKey import RSA
 from .database import DATABASE
+from .http_debug import http_debug
 
 
 # generate actor keys if not present
@@ -86,7 +87,7 @@ async def push_message_to_actor(actor, message, our_key_id):
 
     logging.debug('%r >> %r', inbox, message)
 
-    async with aiohttp.ClientSession() as session:
+    async with aiohttp.ClientSession(trace_configs=[http_debug()]) as session:
         async with session.post(inbox, data=data, headers=headers) as resp:
             resp_payload = await resp.text()
             logging.debug('%r >> resp %r', inbox, resp_payload)
diff --git a/relay/http_debug.py b/relay/http_debug.py
new file mode 100644 (file)
index 0000000..4bb87c3
--- /dev/null
@@ -0,0 +1,12 @@
+import logging
+import aiohttp
+
+
+async def on_request_start(session, trace_config_ctx, params):
+    logging.debug("HTTP START [%r], [%r]", session, params)
+
+
+def http_debug():
+    trace_config = aiohttp.TraceConfig()
+    trace_config.on_request_start.append(on_request_start)
+    return trace_config
index 28e49b7..a8b271f 100644 (file)
@@ -1,5 +1,6 @@
 import aiohttp
 from .database import DATABASE
+from .http_debug import http_debug
 
 
 ACTORS = DATABASE.get("actors", {})
@@ -7,7 +8,7 @@ async def fetch_actor(uri, force=False):
     if uri in ACTORS and not force:
         return ACTORS[uri]
 
-    async with aiohttp.ClientSession() as session:
+    async with aiohttp.ClientSession(trace_configs=[http_debug()]) as session:
         async with session.get(uri, headers={'Accept': 'application/activity+json'}) as resp:
             ACTORS[uri] = (await resp.json(encoding='utf-8', content_type=None))
             DATABASE["actors"] = ACTORS