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")
"scoreboard": True,
"mode" : mode,
"amount" : amount,
- "scores" : network.json_from_response(response)
+ "scores" : json_helper.from_response(response)
})
@router.get(config.get("base_url") + "/")
--- /dev/null
+# 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 <https://www.gnu.org/licenses/>.
+
+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
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
)
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:
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:
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:
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