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