diff --git a/README.md b/README.md index ec6d715..7c937e6 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,16 @@ See working examples in [examples/](https://github.com/db0/pythorhead/tree/main/ # Exceptions -If you want to raise exceptions on failures instead of returning null, pass `raise_exceptions=True` to the `Lemmy()` constructor +If you want to raise exceptions on failures instead of returning null, pass `raise_exceptions=True` to the `Lemmy()` constructor. + +The normal request timeout is 3 seconds. If you want to change this number, you can set it on the constructor uising `request_timeout=` ## Sample Post Usage ```python from pythorhead import Lemmy -lemmy = Lemmy("https://lemmy.dbzer0.com") +lemmy = Lemmy("https://lemmy.dbzer0.com",request_timeout=2) lemmy.log_in("username", "password") community_id = lemmy.discover_community("botart") lemmy.post.create(community_id, "Hello Lemmy World") diff --git a/examples/user.py b/examples/user.py index 9eccfe5..0c0dd9d 100644 --- a/examples/user.py +++ b/examples/user.py @@ -3,7 +3,6 @@ import argparse import json import os - from pythorhead import Lemmy arg_parser = argparse.ArgumentParser() @@ -49,7 +48,7 @@ if not lemmy_password: lemmy_password = os.getenv("LEMMY_PASSWORD") -lemmy = Lemmy(f"https://{lemmy_domain}") +lemmy = Lemmy(f"https://{lemmy_domain}", raise_exceptions=True, request_timeout=2) if lemmy_username and lemmy_password: login = lemmy.log_in(lemmy_username, lemmy_password) user = lemmy.user.get(username=args.username) diff --git a/pythorhead/lemmy.py b/pythorhead/lemmy.py index d1788a7..60f6cad 100644 --- a/pythorhead/lemmy.py +++ b/pythorhead/lemmy.py @@ -20,8 +20,8 @@ class Lemmy: _known_communities = {} _requestor: Requestor - def __init__(self, api_base_url: str, raise_exceptions = False) -> None: - self._requestor = Requestor(raise_exceptions) + def __init__(self, api_base_url: str, raise_exceptions = False, request_timeout=3) -> None: + self._requestor = Requestor(raise_exceptions, request_timeout) self._requestor.set_domain(api_base_url) self.post = Post(self._requestor) self.community = Community(self._requestor) diff --git a/pythorhead/requestor.py b/pythorhead/requestor.py index 022d2c9..0bf198e 100644 --- a/pythorhead/requestor.py +++ b/pythorhead/requestor.py @@ -26,11 +26,13 @@ class Requestor: nodeinfo: Optional[dict] = None domain: Optional[str] = None raise_exceptions: Optional[bool] = False + request_timeout: Optional[int] = 3 - def __init__(self, raise_exceptions = False): + def __init__(self, raise_exceptions = False, request_timeout = 3): self._auth = Authentication() self.set_api_base_url = self._auth.set_api_base_url self.raise_exceptions = raise_exceptions + self.request_timeout = request_timeout def set_domain(self, domain: str): self.domain = domain @@ -72,7 +74,7 @@ def api(self, method: Request, endpoint: str, **kwargs) -> Optional[dict]: "Sec-GPC": "1", "User-Agent": "pythorhead/0.5", } - r = REQUEST_MAP[method](f"{self._auth.api_url}{endpoint}", headers = headers, **kwargs) + r = REQUEST_MAP[method](f"{self._auth.api_url}{endpoint}", headers = headers, timeout=self.request_timeout , **kwargs) except Exception as err: if not self.raise_exceptions: logger.error(f"Error encountered while {method} on endpoint {endpoint}: {err}") @@ -92,7 +94,7 @@ def image(self, method: Request, **kwargs) -> Optional[dict]: cookies = {} if self._auth.token: cookies["jwt"] = self._auth.token - r = REQUEST_MAP[method](self._auth.image_url, cookies=cookies, **kwargs) + r = REQUEST_MAP[method](self._auth.image_url, cookies=cookies, timeout=self.request_timeout, **kwargs) if not r.ok: logger.error(f"Error encountered while {method}: {r.text}") return