]> git.mxchange.org Git - fba.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Wed, 5 Jul 2023 23:41:33 +0000 (01:41 +0200)
committerRoland Häder <roland@mxchange.org>
Wed, 5 Jul 2023 23:41:33 +0000 (01:41 +0200)
- use fetch_response() instead of invoking reqto.get()
- more debug logging
- check response status of bot posts

fba/http/network.py

index 5db3a98d92d3bc704bbb232fb4252a16429bd1dc..8553afaa8f73425a07df6cd69690e7edeb618334 100644 (file)
@@ -162,14 +162,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict:
 
     try:
         logger.debug("Sending GET to domain='%s',path='%s',timeout(%d)='%s'", domain, path, len(timeout), timeout)
-        response = reqto.get(
-            f"https://{domain}{path}",
-            headers={**api_headers, **headers},
-            timeout=timeout,
-            cookies=cookies.get_all(domain),
-            allow_redirects=False
-        )
-
+        response = fetch_response(domain, path, headers, timeout)
     except exceptions as exception:
         logger.debug("Fetching path='%s' from domain='%s' failed. exception[%s]='%s'", path, domain, type(exception), str(exception))
         json_reply["status_code"]   = 999
@@ -212,8 +205,10 @@ def send_bot_post(domain: str, blocklist: list):
     for block in blocklist:
         logger.debug("block[%s]='%s'", type(block), block)
         if block["reason"] is None or block["reason"] == '':
+            logger.debug("block[blocked]='%s' is being blocked with no reason specified", block["blocked"])
             message = message + block["blocked"] + " with unspecified reason\n"
         else:
+            logger.debug("block[reason]()=%d", len(block["reason"]))
             if len(block["reason"]) > 420:
                 block["reason"] = block["reason"][0:419] + "[…]"
 
@@ -222,21 +217,20 @@ def send_bot_post(domain: str, blocklist: list):
     if truncated:
         message = message + "(the list has been truncated to the first 20 entries)"
 
-    botheaders = {**api_headers, **{"Authorization": "Bearer " + config.get("bot_token")}}
-
-    req = reqto.post(
+    response = reqto.post(
         f"{config.get('bot_instance')}/api/v1/statuses",
         data={
             "status"      : message,
-            "visibility"  : config.get('bot_visibility'),
+            "visibility"  : config.get("bot_visibility"),
             "content_type": "text/plain"
         },
-        headers=botheaders,
+        headers={**api_headers, **{"Authorization": "Bearer " + config.get("bot_token")}},
         timeout=(config.get("connection_timeout"), config.get("read_timeout")),
         allow_redirects=False
-    ).json()
+    )
 
-    return True
+    logger.debug("response.ok='%s',response.status_code=%d,response.text()=%d", response.ok, response.status_code, len(response.text))
+    return response.ok and response.status_code < 300 and response.text.strip() != ""
 
 def fetch_response(domain: str, path: str, headers: dict, timeout: tuple, allow_redirects: bool = False) -> requests.models.Response:
     logger.debug("domain='%s',path='%s',headers()=%d,timeout='%s',allow_redirects='%s' - CALLED!", domain, path, len(headers), timeout, allow_redirects)