Skip to content

Commit

Permalink
Adds decorator to apply rewrite rules
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>
  • Loading branch information
jeandet committed Jan 18, 2025
1 parent d36a0ad commit 4928d7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 7 additions & 7 deletions speasy/core/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from speasy import __version__
from speasy.config import core as core_config
from .url_utils import host_and_port, apply_rewrite_rules
from .url_utils import host_and_port, ApplyRewriteRules

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -90,28 +90,29 @@ def __init__(self, verb):
# self._http.mount("http://", self._adapter)
self._verb = partial(pool.request, method=verb, retries=retry_strategy)

@ApplyRewriteRules()
def __call__(self, url, headers: dict = None, params: dict = None, timeout: int = DEFAULT_TIMEOUT):
# self._adapter.timeout = timeout
headers = headers or {}
headers['User-Agent'] = USER_AGENT
url = apply_rewrite_rules(url)
return Response(self._verb(url=url, headers=headers, fields=params, timeout=timeout))


get = _HttpVerb("GET")
head = _HttpVerb("HEAD")


@ApplyRewriteRules()
def urlopen(url, timeout: int = DEFAULT_TIMEOUT, headers: dict = None):
headers = {} if headers is None else headers
headers['User-Agent'] = USER_AGENT
url = apply_rewrite_rules(url)
return Response(pool.urlopen(method="GET", url=url, headers=headers, timeout=timeout))


def is_server_up(url: Optional[str] = None, host: Optional[str] = None, port: Optional[int] = None,
timeout: int = 5, retries=5) -> bool:
"""Checks if a server is up and running.
@ApplyRewriteRules()
def is_server_up(url: Optional[str] = None, host: Optional[str] = None, port: Optional[int] = None, timeout: int = 5,
retries=5) -> bool:
"""Checks if a server is up and running. If url is provided, host and port are ignored.
Parameters
----------
Expand All @@ -135,7 +136,6 @@ def is_server_up(url: Optional[str] = None, host: Optional[str] = None, port: Op
If neither url nor host and port are provided
"""
if url is not None:
url = apply_rewrite_rules(url)
host, port = host_and_port(url)
elif host is None or port is None:
raise ValueError("Either url or host and port must be provided")
Expand Down
11 changes: 10 additions & 1 deletion speasy/core/url_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Dict, Tuple
from typing import Dict, Tuple, Callable
from urllib.parse import urlparse, urlencode
from functools import wraps

from speasy.config import core as core_config

_REWRITE_RULES_ = core_config.http_rewrite_rules.get()
Expand Down Expand Up @@ -98,6 +100,13 @@ def apply_rewrite_rules(url: str) -> str:
return _REWRITE_RULES_[base_url] + url[len(base_url):]
return url

class ApplyRewriteRules:
def __call__(self, f:Callable):
@wraps(f)
def wrapper(url:str, *args, **kwargs):
if url is not None:
return f(apply_rewrite_rules(url), *args, **kwargs)
return f(url, *args, **kwargs)

def extract_path(url: str) -> str:
return urlparse(url).path

0 comments on commit 4928d7d

Please sign in to comment.