Skip to content

Commit

Permalink
feat: Allows specifying request timeout (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
db0 authored Oct 18, 2023
1 parent 4f73afe commit 4d75bd6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions examples/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import argparse
import json
import os

from pythorhead import Lemmy

arg_parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pythorhead/lemmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions pythorhead/requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
Expand All @@ -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
Expand Down

0 comments on commit 4d75bd6

Please sign in to comment.