feat: added register_user method
Some checks failed
Pylint / Pylint (3.12) (push) Failing after 36s

This commit is contained in:
Seaswimmer 2023-12-20 23:51:57 -05:00
parent 7c6219924e
commit 76109a6494
Signed by: cswimr
GPG key ID: 1EBC234EEDA901AE

View file

@ -27,37 +27,51 @@ class ZiplineApi:
): ):
self._rest_adapter = RestAdapter(hostname=hostname, token=token, ssl=ssl, enforced_signing=enforced_signing, logger=logger) self._rest_adapter = RestAdapter(hostname=hostname, token=token, ssl=ssl, enforced_signing=enforced_signing, logger=logger)
def get_user(self, user_id: int) -> User: def register_user(self, username: str, password: str, invite: str = None, admin: bool = False) -> User:
"""Get a user by ID """Register a new user
/// admonition | Requires Administrator
type: danger
///
Args: Args:
user_id (int): Integer ID of the user to retrieve username (str): Username to register
password (str): Password for the new user
invite (str): Invite code to register the new user with, only required if registration without invites is disabled and the authenticated user is not an administrator
admin (bool): Whether or not the new user should be an administrator, authenticated user must be a super administrator to create an administrator
Raises:
Forbidden: Raised if the authenticated user is not an super administrator and attempts to create an administrator
FeatureDisabledError: Raised when:\n
- registration or invites are disabled on the Zipline instance and the authenticated user is not an administrator
- invite code is provided and invites are disabled
PyZiplineError: Raised if the API changes, causing a breaking change in this method
ValueError: Raised when the username is already taken or if the invite code is invalid/expired
Returns: Returns:
User: The user with the given ID User: The newly created user
""" """
result = self._rest_adapter.get(endpoint=f"user/{user_id}") data = {'username': username, 'password': password}
return User(**result.data) if invite is not None:
data['code'] = invite
if admin:
data['admin'] = True
def get_self(self) -> User: result: Result = self._rest_adapter.post(endpoint="auth/register", data=data)
"""Get the currently authenticated user
/// admonition | Requires Authentication
type: warning
///
Returns:
User: The currently authenticated user
"""
result = self._rest_adapter.get(endpoint=f"user")
if result.status_code == 200: if result.status_code == 200:
return User(**result.data) return User(**result.data)
elif result.status_code == 401:
raise ValueError(result.message) if result.message == 'This endpoint is unavailable due to current configurations':
raise FeatureDisabledError('user registration or invites are disabled')
if result.message =='Bad Username/Password':
if self.check_user_exists(username):
raise ValueError('username already taken')
raise FeatureDisabledError('invite code is provided and invites are disabled')
if result.message == 'Bad invite':
raise ValueError('invite code is invalid or expired')
if result.message == 'not an administrator':
raise Forbidden(result.message)
raise PyZiplineError(f"{result.status_code}: {result.message}\n{result.data}")
def check_user_exists(self, username: str, invite: str = None) -> bool: def check_user_exists(self, username: str, invite: str = None) -> bool:
"""Check if a user exists by username """Check if a user exists by username