From 76109a6494b9c762fae56f6a94e9a7d14174988c Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Wed, 20 Dec 2023 23:51:57 -0500 Subject: [PATCH] feat: added register_user method --- pyzipline/zipline.py | 60 +++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/pyzipline/zipline.py b/pyzipline/zipline.py index 2426e91..0f564b3 100644 --- a/pyzipline/zipline.py +++ b/pyzipline/zipline.py @@ -27,37 +27,51 @@ class ZiplineApi: ): self._rest_adapter = RestAdapter(hostname=hostname, token=token, ssl=ssl, enforced_signing=enforced_signing, logger=logger) - def get_user(self, user_id: int) -> User: - """Get a user by ID - - /// admonition | Requires Administrator - type: danger - /// + def register_user(self, username: str, password: str, invite: str = None, admin: bool = False) -> User: + """Register a new user 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: - User: The user with the given ID + User: The newly created user """ - result = self._rest_adapter.get(endpoint=f"user/{user_id}") - return User(**result.data) + data = {'username': username, 'password': password} + if invite is not None: + data['code'] = invite + if admin: + data['admin'] = True - def get_self(self) -> User: - """Get the currently authenticated user - - /// admonition | Requires Authentication - type: warning - /// - - Returns: - User: The currently authenticated user - """ - result = self._rest_adapter.get(endpoint=f"user") + result: Result = self._rest_adapter.post(endpoint="auth/register", data=data) if result.status_code == 200: 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: """Check if a user exists by username