kopia lustrzana https://github.com/tsileo/little-boxes
Improve the debug mode, fixes #4
rodzic
3f59845b8a
commit
c2524dccb2
|
@ -8,13 +8,20 @@ import requests
|
|||
from .__version__ import __version__
|
||||
from .errors import ActivityNotFoundError
|
||||
from .errors import RemoteActivityGoneError
|
||||
from .urlutils import check_url
|
||||
from .urlutils import check_url as check_url
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from little_boxes import activitypub as ap # noqa: type checking
|
||||
|
||||
|
||||
class Backend(abc.ABC):
|
||||
def debug_mode(self) -> bool:
|
||||
"""Should be overidded to return `True` in order to enable the debug mode."""
|
||||
return False
|
||||
|
||||
def check_url(self, url: str) -> None:
|
||||
check_url(url, debug=self.debug_mode())
|
||||
|
||||
def user_agent(self) -> str:
|
||||
return (
|
||||
f"{requests.utils.default_user_agent()} (Little Boxes/{__version__};"
|
||||
|
@ -26,7 +33,7 @@ class Backend(abc.ABC):
|
|||
return binascii.hexlify(os.urandom(8)).decode("utf-8")
|
||||
|
||||
def fetch_json(self, url: str, **kwargs):
|
||||
check_url(url)
|
||||
self.check_url(url)
|
||||
resp = requests.get(
|
||||
url,
|
||||
headers={"User-Agent": self.user_agent(), "Accept": "application/json"},
|
||||
|
@ -53,7 +60,7 @@ class Backend(abc.ABC):
|
|||
pass # pragma: no cover
|
||||
|
||||
def fetch_iri(self, iri: str, **kwargs) -> "ap.ObjectType": # pragma: no cover
|
||||
check_url(iri)
|
||||
self.check_url(iri)
|
||||
resp = requests.get(
|
||||
iri,
|
||||
headers={
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
import ipaddress
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
from typing import Dict
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from . import strtobool
|
||||
from .errors import Error
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -18,14 +16,13 @@ class InvalidURLError(Error):
|
|||
pass
|
||||
|
||||
|
||||
def is_url_valid(url: str) -> bool:
|
||||
def is_url_valid(url: str, debug: bool = False) -> bool:
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ["http", "https"]:
|
||||
return False
|
||||
|
||||
# XXX in debug mode, we want to allow requests to localhost to test the federation with local instances
|
||||
debug_mode = strtobool(os.getenv("MICROBLOGPUB_DEBUG", "false"))
|
||||
if debug_mode: # pragma: no cover
|
||||
if debug: # pragma: no cover
|
||||
return True
|
||||
|
||||
if parsed.hostname in ["localhost"]:
|
||||
|
@ -56,9 +53,9 @@ def is_url_valid(url: str) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
def check_url(url: str) -> None:
|
||||
logger.debug(f"check_url {url}")
|
||||
if not is_url_valid(url):
|
||||
def check_url(url: str, debug: bool = False) -> None:
|
||||
logger.debug(f"check_url {url} debug={debug}")
|
||||
if not is_url_valid(url, debug=debug):
|
||||
raise InvalidURLError(f'"{url}" is invalid')
|
||||
|
||||
return None
|
||||
|
|
|
@ -65,6 +65,9 @@ class InMemBackend(Backend):
|
|||
|
||||
return calls
|
||||
|
||||
def debug_mode(self) -> bool:
|
||||
return True
|
||||
|
||||
def setup_actor(self, name, pusername):
|
||||
"""Create a new actor in this backend."""
|
||||
p = ap.Person(
|
||||
|
|
Ładowanie…
Reference in New Issue