diff --git a/pyzipline/zipline.py b/pyzipline/zipline.py index 3637fed..dd21f61 100644 --- a/pyzipline/zipline.py +++ b/pyzipline/zipline.py @@ -28,6 +28,49 @@ class ZiplineApi: ): self._rest_adapter = RestAdapter(hostname=hostname, token=token, ssl=ssl, enforced_signing=enforced_signing, logger=logger) + def create_invite(self, expiry: timedelta = timedelta(days=1), count: int = 1) -> Union[Invite, List[Invite]]: + """Create an invite code + + /// admonition | Requires Authentication + type: warning + /// + + /// admonition | Requires Administrator + type: danger + /// + + Args: + expiry (timedelta): Timedelta object representing when the invite should expire + count (int): Number of invites to create + + Raises: + FeatureDisabledError: Raised when invites are disabled on the Zipline instance + Forbidden: Raised if the authenticated user is not an administrator + PyZiplineError: Raised if the API changes, causing a breaking change in this method + ValueError: Raised when the expiry datetime is invalid or the count is less than 1 + + Returns: + Invite: The newly created invite code(s) + """ + json = {'expiresAt': 'date=' + convert_datetime_to_str(datetime.now() + expiry), 'count': count} + result: Result = self._rest_adapter.post(endpoint="auth/invite", json=json) + if result.status_code == 200: + if count > 1: + invite_list = [] + for invite in result.data: + i = Invite(**invite) + invite_list.append(i) + return invite_list + data = result.data[0] if isinstance(result.data, list) else result.data + return Invite(**data) + if result.message == 'invites are disabled': + raise FeatureDisabledError(result.message) + if result.message == 'invalid date': + raise ValueError(f"{result.status_code}: {result.message}\n{result.data}\n{json}") + if result.message == 'not an administrator': + raise Forbidden(result.message) + raise PyZiplineError(f"{result.status_code}: {result.message}\n{result.data}") + def register_user(self, username: str, password: str, invite: str = None, admin: bool = False) -> User: """Register a new user