PyZipline/pyzipline/zipline.py

92 lines
3.5 KiB
Python
Raw Normal View History

2023-12-19 05:36:18 -05:00
import logging
2023-12-20 20:48:45 -05:00
import json
2023-12-19 05:36:18 -05:00
from pyzipline.rest_adapter import RestAdapter
2023-12-20 20:48:45 -05:00
from pyzipline.exceptions import PyZiplineError, FeatureDisabledError
2023-12-19 05:36:18 -05:00
from pyzipline.models import *
class ZiplineApi:
"""Represents an instance of the Zipline API.
All API requests should be made through this class.
Args:
hostname (str): The hostname of your Zipline instance, WITHOUT https or http.
token (str = None): String used for authentication when making requests.
ssl (bool = True): Normally set to True, but if your Zipline instance doesn't use SSL/TLS, set this to False.
enforced_signing (bool = True): Normally set to True, but if having SSL/TLS cert validation issues, can turn off with False.
logger (logging.Logger = None): If your app has a logger, pass it in here.
"""
2023-12-19 05:36:18 -05:00
def __init__(
self,
hostname: str,
token: str = '',
ssl: bool = True,
enforced_signing: bool = True,
logger: logging.Logger = None
):
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
2023-12-20 20:48:45 -05:00
/// admonition | Requires Administrator
type: danger
///
Args:
user_id (int): Integer ID of the user to retrieve
Returns:
2023-12-20 20:48:45 -05:00
User: The user with the given ID
2023-12-19 05:36:18 -05:00
"""
result = self._rest_adapter.get(endpoint=f"user/{user_id}")
return User(**result.data)
def get_self(self) -> User:
"""Get the currently authenticated user
2023-12-20 20:48:45 -05:00
/// admonition | Requires Authentication
type: warning
///
Returns:
2023-12-20 20:48:45 -05:00
User: The currently authenticated user
2023-12-19 05:36:18 -05:00
"""
result = self._rest_adapter.get(endpoint=f"user")
2023-12-20 20:48:45 -05:00
if result.status_code == 200:
return User(**result.data)
elif result.status_code == 401:
raise ValueError(result.message)
def check_user_exists(self, username: str, invite: str = None) -> bool:
"""Check if a user exists by username
Args:
username (str): Username to check
invite (str = None): Invite code to use, only required if registration without invites is disabled
Raises:
FeatureDisabledError: Raised when registration or invites are disabled on the Zipline instance
2023-12-20 20:48:45 -05:00
PyZiplineError: Raised if the API changes, causing a breaking change in this method
ValueError: Raised when the username is not present, or the invite code is invalid/not present and invites are enabled
Returns:
bool: True if user exists, False if not
"""
2023-12-20 20:48:45 -05:00
data = {'username': username} if invite is None else {'username': username, 'code': invite}
result: Result = self._rest_adapter.post(endpoint=f"user/check", data=data)
if result.status_code == 200:
2023-12-20 20:48:45 -05:00
return False
elif result.message == 'username already exists':
return True
2023-12-20 20:48:45 -05:00
elif result.message == 'user registration is disabled':
raise FeatureDisabledError('user registration or invites are disabled')
elif result.message == 'invalid invite code':
2023-12-20 20:48:45 -05:00
raise ValueError(result.message + "(most likely doesn't exist)")
elif result.message == 'no code':
raise ValueError('invite code not provided')
elif result.message == 'no username':
raise ValueError('username not provided')
else:
raise PyZiplineError(result.message)