diff --git a/pyzipline/zipline.py b/pyzipline/zipline.py index 9b2c5ef..02c881f 100644 --- a/pyzipline/zipline.py +++ b/pyzipline/zipline.py @@ -1,9 +1,10 @@ +"""This module contains the ZiplineApi class, which is the main class used to interact with the Zipline API.""" import logging -import json from pyzipline.rest_adapter import RestAdapter -from pyzipline.exceptions import PyZiplineError, FeatureDisabledError -from pyzipline.models import * +from pyzipline.exceptions import PyZiplineError, FeatureDisabledError, Forbidden +from pyzipline.models import * # pylint: disable=wildcard-import,unused-wildcard-import +# pylint: disable=not-a-mapping class ZiplineApi: """Represents an instance of the Zipline API. @@ -77,15 +78,58 @@ class ZiplineApi: result: Result = self._rest_adapter.post(endpoint=f"user/check", data=data) if result.status_code == 200: return False - elif result.message == 'username already exists': + if result.message == 'username already exists': return True - elif result.message == 'user registration is disabled': + if result.message == 'user registration is disabled': raise FeatureDisabledError('user registration or invites are disabled') - elif result.message == 'invalid invite code': + if result.message == 'invalid invite code': raise ValueError(result.message + "(most likely doesn't exist)") - elif result.message == 'no code': + if result.message == 'no code': raise ValueError('invite code not provided') - elif result.message == 'no username': + if result.message == 'no username': raise ValueError('username not provided') - else: - raise PyZiplineError(result.message) + raise PyZiplineError(f"{result.status_code}: {result.message}\n{result.data}") + + def get_self(self) -> User: + """Get the currently authenticated user + + /// admonition | Requires Authentication + type: warning + /// + + Raises: + PyZiplineError: Raised if the API changes, causing a breaking change in this method + + Returns: + User: The currently authenticated user + """ + result = self._rest_adapter.get(endpoint="user") + if result.status_code == 200: + return User(**result.data) + if result.status_code == 401: + raise Forbidden(result.message) + raise PyZiplineError(f"{result.status_code}: {result.message}\n{result.data}") + + def get_user(self, user_id: int) -> User: + """Get a user by ID + + /// admonition | Requires Administrator + type: danger + /// + + Args: + user_id (int): Integer ID of the user to retrieve + + Raises: + Forbidden: Raised if the authenticated user is not an administrator + PyZiplineError: Raised if the API changes, causing a breaking change in this method + + Returns: + User: The user with the given ID + """ + result = self._rest_adapter.get(endpoint=f"user/{user_id}") + if result.status_code == 200: + return User(**result.data) + if result.status_code == 403: + raise Forbidden(result.message) + raise PyZiplineError(f"{result.status_code}: {result.message}\n{result.data}")