From 88bcb0114d04e3f04d35747b62129b6fba1cd393 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 6 Jul 2023 01:41:33 +0200 Subject: [PATCH] Continued: - use fetch_response() instead of invoking reqto.get() - more debug logging - check response status of bot posts --- fba/http/network.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/fba/http/network.py b/fba/http/network.py index 5db3a98..8553afa 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -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) -- 2.39.5