From f290010f7602f2685acd840d5ed3e4a040d876ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 1 Jul 2023 01:21:47 +0200 Subject: [PATCH] Continued: - moved json_from_response() to fba.helpers.json.from_response() --- daemon.py | 5 ++--- fba/helpers/__init__.py | 1 + fba/helpers/json.py | 47 +++++++++++++++++++++++++++++++++++++++++ fba/http/network.py | 25 ++++------------------ 4 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 fba/helpers/json.py diff --git a/daemon.py b/daemon.py index 674837f..60a1d2d 100755 --- a/daemon.py +++ b/daemon.py @@ -37,10 +37,9 @@ from fba import database from fba import utils from fba.helpers import config +from fba.helpers import json as json_helper from fba.helpers import tidyup -from fba.http import network - from fba.models import blocks router = fastapi.FastAPI(docs_url=config.get("base_url") + "/docs", redoc_url=config.get("base_url") + "/redoc") @@ -255,7 +254,7 @@ def scoreboard(request: Request, mode: str, amount: int): "scoreboard": True, "mode" : mode, "amount" : amount, - "scores" : network.json_from_response(response) + "scores" : json_helper.from_response(response) }) @router.get(config.get("base_url") + "/") diff --git a/fba/helpers/__init__.py b/fba/helpers/__init__.py index 832e28f..4e68780 100644 --- a/fba/helpers/__init__.py +++ b/fba/helpers/__init__.py @@ -20,6 +20,7 @@ __all__ = [ 'cookies', 'dicts', 'domain', + 'json', 'locking', 'software', 'tidyup', diff --git a/fba/helpers/json.py b/fba/helpers/json.py new file mode 100644 index 0000000..9264f1b --- /dev/null +++ b/fba/helpers/json.py @@ -0,0 +1,47 @@ +# Fedi API Block - An aggregator for fetching blocking data from fediverse nodes +# Copyright (C) 2023 Free Software Foundation +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import logging + +import json +import requests + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +def from_response(response: requests.models.Response) -> list: + logger.debug("response[]='%s' - CALLED!", type(response)) + if not isinstance(response, requests.models.Response): + raise ValueError(f"Parameter response[]='{type(response)}' is not type of 'Response'") + elif response.headers.get("content-type") is None or response.headers.get("content-type").split(";")[0] != "application/json": + logger.warning("response.headers[content-type]='%s' is not a JSON type, below json() invocation may raise an exception", response.headers.get("content-type")) + + data = list() + if response.text.strip() != "": + logger.debug("response.text()=%d is not empty, invoking response.json() ...", len(response.text)) + try: + data = response.json() + + logger.debug("data[]='%s' - EXIT!", type(data)) + if not isinstance(data, list) and not isinstance(data, dict): + logger.debug("data[]='%s' is not wanted, wrapping into 'dict'", type(data)) + data = {data} + + except json.decoder.JSONDecodeError as exception: + logger.warning("Exception '%s' during decoding JSON from response.url='%s'", type(exception), response.url) + + logger.debug("data[]='%s' - EXIT!", type(data)) + return data diff --git a/fba/http/network.py b/fba/http/network.py index d713e92..f347b79 100644 --- a/fba/http/network.py +++ b/fba/http/network.py @@ -26,6 +26,7 @@ from fba import utils from fba.helpers import config from fba.helpers import cookies from fba.helpers import domain as domain_helper +from fba.helpers import json as json_helper from fba.models import instances @@ -83,7 +84,7 @@ def post_json_api(domain: str, path: str, data: str = "", headers: dict = dict() ) logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) - json_reply["json"] = json_from_response(response) + json_reply["json"] = json_helper.from_response(response) logger.debug("response.ok='%s',response.status_code=%d,json_reply[]='%s'", response.ok, response.status_code, type(json_reply)) if not response.ok or response.status_code >= 400 or len(json_reply["json"]) == 0: @@ -122,7 +123,7 @@ def fetch_api_url(url: str, timeout: tuple) -> dict: response = utils.fetch_url(url, api_headers, timeout) logger.debug("Parsing JSON response from url='%s' ...", url) - json_reply["json"] = json_from_response(response) + json_reply["json"] = json_helper.from_response(response) logger.debug("response.ok='%s',response.status_code='%s',json_reply[]='%s'", response.ok, response.status_code, type(json_reply)) if not response.ok or response.status_code >= 400 or len(json_reply["json"]) == 0: @@ -176,7 +177,7 @@ def get_json_api(domain: str, path: str, headers: dict, timeout: tuple) -> dict: raise exception logger.debug("Parsing JSON response from domain='%s',path='%s' ...", domain, path) - json_reply["json"] = json_from_response(response) + json_reply["json"] = json_helper.from_response(response) logger.debug("response.ok='%s',response.status_code=%d,json_reply[]='%s'", response.ok, response.status_code, type(json_reply)) if not response.ok or response.status_code >= 400 or len(json_reply["json"]) == 0: @@ -261,21 +262,3 @@ def fetch_response(domain: str, path: str, headers: dict, timeout: tuple) -> req logger.debug("response[]='%s' - EXIT!", type(response)) return response - -def json_from_response(response: requests.models.Response) -> list: - logger.debug("response[]='%s' - CALLED!", type(response)) - if not isinstance(response, requests.models.Response): - raise ValueError(f"Parameter response[]='{type(response)}' is not type of 'Response'") - elif response.headers.get("content-type") is None or response.headers.get("content-type").split(";")[0] != "application/json": - logger.warning("response.headers[content-type]='%s' is not a JSON type, below json() invocation may raise an exception", response.headers.get("content-type")) - - data = list() - if response.text.strip() != "": - logger.debug("response.text()=%d is not empty, invoking response.json() ...", len(response.text)) - try: - data = response.json() - except json.decoder.JSONDecodeError as exception: - logger.warning("Exception '%s' during decoding JSON from response.url='%s'", type(exception), response.url) - - logger.debug("data[]='%s' - EXIT!", type(data)) - return data -- 2.39.5