1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 logging.basicConfig(level=logging.INFO)
23 logger = logging.getLogger(__name__)
25 def from_response(response: requests.models.Response) -> any:
26 logger.debug("response[]='%s' - CALLED!", type(response))
27 if not isinstance(response, requests.models.Response):
28 raise ValueError(f"Parameter response[]='{type(response)}' is not type of 'Response'")
29 elif response.headers.get("content-type") is None or response.headers.get("content-type").split(";")[0] not in ["application/json", "application/jrd+json", "application/activity+json"]:
30 logger.warning("response.headers[content-type]='%s' is not a JSON type, below json() invocation may raise an exception", response.headers.get("content-type"))
33 if response.text.strip() != "":
34 logger.debug("response.text()=%d is not empty, invoking response.json() ...", len(response.text))
36 data = response.json()
38 logger.debug("data[]='%s' - EXIT!", type(data))
39 if not isinstance(data, list) and not isinstance(data, dict):
40 logger.warning("data[]='%s' is not wanted, wrapping into 'dict'", type(data))
43 except json.decoder.JSONDecodeError as exception:
44 logger.warning("Exception '%s' during decoding JSON from response.url='%s'", type(exception), response.url)
46 logger.debug("data[]='%s' - EXIT!", type(data))