Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes uncaught AttributeError in login() #48

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
31 changes: 27 additions & 4 deletions plemmy/lemmyhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,22 @@ def get_unread_registration_application_count(self) -> requests.Response:
None, None
)

def hide_community(self, community_id: int, hidden: bool, reason: str = '') -> requests.Response:
""" hide_community: Hide a community from public / "All" view. Admins only.

Args:
community_id (int): ID of community to hide
hidden (bool): True if hidden, False otherwise
reason (str): reason for hiding community

Returns:
requests.Response: result of API call
"""

form = create_form(locals())
return put_handler(self._session, f"{self._api_url}/community/hide",
form)

def leave_admin(self) -> requests.Response:
""" leave_admin: current user leaves admin group

Expand Down Expand Up @@ -1311,24 +1327,31 @@ def lock_post(self, locked: bool, post_id: int) -> requests.Response:
return post_handler(self._session, f"{self._api_url}/post/lock", form)

def login(self, username_or_email: str,
password: str) -> requests.Response:
password: str, totp_2fa_token: str = None) -> requests.Response:
""" login: login to Lemmy instance, setting `LemmyHttp._session` jwt to
authenticated user jwt

Args:
username_or_email (str): username or email for login
password (str): password for login
totp_2fa_token (str): 2FA token if enabled

Raises:
requests.ConnectionError: if no connection to server could be made

Raises:
requests.ConnectionError: if no connection to server could be made

Returns:
requests.Response: result of API call
"""

form = create_form(locals())
re = post_handler(self._session, f"{self._api_url}/user/login", form)
if re.status_code == 200:
if not isinstance(re, requests.Response):
raise requests.ConnectionError("Login failed as no connection to server could be made.")
elif re.status_code == 200:
self._session = create_session(self._headers, re.json()["jwt"])
else:
raise Exception("Login failed with status code: " + str(re.status_code))
return re

def mark_all_as_read(self) -> requests.Response:
Expand Down