Added user-friendly CLI

This commit is contained in:
2023-03-12 22:14:03 +01:00
parent 48acab2d5e
commit c670db72fa
4 changed files with 154 additions and 84 deletions

View File

@@ -11,7 +11,14 @@ import aiofiles
from aiohttp import ClientSession, FormData
from ujson import dumps
from classes.exceptions import SubmissionUploadError
from classes.exceptions import (
AlbumCreationDuplicateError,
AlbumCreationError,
AlbumCreationNameError,
SubmissionUploadError,
UserCreationDuplicateError,
UserCreationError,
)
from modules.logger import logWrite
from modules.utils import configGet
@@ -169,5 +176,33 @@ async def move_pic(id: str, token: Union[str, None] = None) -> bool:
return False
async def create_user(username: str, email: str, password: str) -> None:
response = await http_session.post(
f'{configGet("address", "posting", "api")}/users',
data={"user": username, "email": email, "password": password},
)
if response.status == 409:
raise UserCreationDuplicateError(username)
elif response.status != 204:
raise UserCreationError(response.status, await response.text(encoding="utf-8"))
return None
async def create_album(name: str, title: str) -> None:
token = await authorize()
response = await http_session.post(
f'{configGet("address", "posting", "api")}/albums',
params={"name": name, "title": title},
headers={"Authorization": f"Bearer {token}"},
)
if response.status == 409:
raise AlbumCreationDuplicateError(name)
elif response.status == 406:
raise AlbumCreationNameError(await response.json())
elif response.status != 200:
raise AlbumCreationError(response.status, await response.text(encoding="utf-8"))
return None
if __name__ == "__main__":
print(asyncio.run(authorize()))