From c0030e3643451a7ce5c7de73f894a0090c09a3a9 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 6 Nov 2019 15:54:08 +0100 Subject: [PATCH] 5.0a88 --- royalnet/bots/discord.py | 6 +---- royalnet/packs/common/stars/__init__.py | 4 +-- .../{version.py => api_royalnet_version.py} | 4 +-- royalnet/packs/common/tables/users.py | 8 ++++++ royalnet/packs/royal/stars/__init__.py | 10 ++++++-- royalnet/packs/royal/stars/api_diario_get.py | 22 ++++++++++++++++ royalnet/packs/royal/stars/api_diario_list.py | 25 +++++++++++++++++++ royalnet/packs/royal/stars/api_user_get.py | 22 ++++++++++++++++ royalnet/packs/royal/stars/api_user_list.py | 15 +++++++++++ royalnet/packs/royal/tables/diario.py | 16 +++++++++++- royalnet/version.py | 2 +- royalnet/web/__init__.py | 2 ++ royalnet/web/error.py | 7 ++++++ royalnet/web/star.py | 2 +- 14 files changed, 131 insertions(+), 14 deletions(-) rename royalnet/packs/common/stars/{version.py => api_royalnet_version.py} (74%) create mode 100644 royalnet/packs/royal/stars/api_diario_get.py create mode 100644 royalnet/packs/royal/stars/api_diario_list.py create mode 100644 royalnet/packs/royal/stars/api_user_get.py create mode 100644 royalnet/packs/royal/stars/api_user_list.py create mode 100644 royalnet/web/error.py diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index 48377fb8..e9cdcb7e 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -1,18 +1,14 @@ import discord import sentry_sdk import logging as _logging -import asyncio from .generic import GenericBot from ..utils import * from ..error import * from ..audio import * from ..commands import * -log = _logging.getLogger(__name__) -# TODO: Load the opus library -if not discord.opus.is_loaded(): - log.error("Opus is not loaded. Weird behaviour might emerge.") +log = _logging.getLogger(__name__) class MusicData: diff --git a/royalnet/packs/common/stars/__init__.py b/royalnet/packs/common/stars/__init__.py index 2393b7e5..49192e58 100644 --- a/royalnet/packs/common/stars/__init__.py +++ b/royalnet/packs/common/stars/__init__.py @@ -1,10 +1,10 @@ # Imports go here! -from .version import VersionStar +from .api_royalnet_version import ApiRoyalnetVersionStar # Enter the PageStars of your Pack here! available_page_stars = [ - VersionStar, + ApiRoyalnetVersionStar, ] # Enter the ExceptionStars of your Pack here! diff --git a/royalnet/packs/common/stars/version.py b/royalnet/packs/common/stars/api_royalnet_version.py similarity index 74% rename from royalnet/packs/common/stars/version.py rename to royalnet/packs/common/stars/api_royalnet_version.py index af16dd2b..37e9e487 100644 --- a/royalnet/packs/common/stars/version.py +++ b/royalnet/packs/common/stars/api_royalnet_version.py @@ -4,10 +4,10 @@ from starlette.responses import * from royalnet.web import PageStar -class VersionStar(PageStar): +class ApiRoyalnetVersionStar(PageStar): path = "/api/royalnet/version" - async def page(self, request: Request, **kwargs) -> JSONResponse: + async def page(self, request: Request) -> JSONResponse: return JSONResponse({ "version": { "semantic": royalnet.version.semantic diff --git a/royalnet/packs/common/tables/users.py b/royalnet/packs/common/tables/users.py index 5f176a78..88ca5f87 100644 --- a/royalnet/packs/common/tables/users.py +++ b/royalnet/packs/common/tables/users.py @@ -28,6 +28,14 @@ class User: def avatar(self): return Column(LargeBinary) + def json(self): + return { + "uid": self.uid, + "username": self.username, + "role": self.role, + "avatar": self.avatar + } + def __repr__(self): return f"<{self.__class__.__qualname__} {self.username}>" diff --git a/royalnet/packs/royal/stars/__init__.py b/royalnet/packs/royal/stars/__init__.py index 10748e81..511a9101 100644 --- a/royalnet/packs/royal/stars/__init__.py +++ b/royalnet/packs/royal/stars/__init__.py @@ -1,9 +1,15 @@ # Imports go here! - +from .api_user_list import ApiUserListStar +from .api_user_get import ApiUserGetStar +from .api_diario_list import ApiDiarioListStar +from .api_diario_get import ApiDiarioGetStar # Enter the PageStars of your Pack here! available_page_stars = [ - + ApiUserListStar, + ApiUserGetStar, + ApiDiarioListStar, + ApiDiarioGetStar, ] # Enter the ExceptionStars of your Pack here! diff --git a/royalnet/packs/royal/stars/api_diario_get.py b/royalnet/packs/royal/stars/api_diario_get.py new file mode 100644 index 00000000..5c2ddf53 --- /dev/null +++ b/royalnet/packs/royal/stars/api_diario_get.py @@ -0,0 +1,22 @@ +from starlette.requests import Request +from starlette.responses import * +from royalnet.web import * +from royalnet.utils import * +from ..tables import Diario + + +class ApiDiarioGetStar(PageStar): + path = "/api/diario/get/{diario_id}" + tables = {Diario} + + async def page(self, request: Request) -> JSONResponse: + diario_id_str = request.path_params.get("diario_id", "") + try: + diario_id = int(diario_id_str) + except (ValueError, TypeError): + return error(400, "Invalid diario_id") + async with self.alchemy.session_acm() as session: + entry: Diario = await asyncify(session.query(self.alchemy.User).get, diario_id) + if entry is None: + return error(404, "No such user") + return JSONResponse(entry.json()) diff --git a/royalnet/packs/royal/stars/api_diario_list.py b/royalnet/packs/royal/stars/api_diario_list.py new file mode 100644 index 00000000..0b11af91 --- /dev/null +++ b/royalnet/packs/royal/stars/api_diario_list.py @@ -0,0 +1,25 @@ +from starlette.requests import Request +from starlette.responses import * +from royalnet.web import * +from royalnet.utils import * +from ..tables import Diario + + +class ApiDiarioListStar(PageStar): + path = "/api/diario/list" + tables = {Diario} + + async def page(self, request: Request) -> JSONResponse: + page_str = request.query_params.get("page", "0") + try: + page = int(page_str) + except (ValueError, TypeError): + return error(400, "Invalid offset") + async with self.alchemy.session_acm() as session: + if page < 0: + page = -page-1 + entries: typing.List[Diario] = await asyncify(session.query(self.alchemy.Diario).order_by(self.alchemy.Diario.diario_id.desc()).limit(500).offset(page * 500).all) + else: + entries: typing.List[Diario] = await asyncify(session.query(self.alchemy.Diario).order_by(self.alchemy.Diario.diario_id).limit(500).offset(page * 500).all) + response = [entry.json() for entry in entries] + return JSONResponse(response) diff --git a/royalnet/packs/royal/stars/api_user_get.py b/royalnet/packs/royal/stars/api_user_get.py new file mode 100644 index 00000000..d547c7b6 --- /dev/null +++ b/royalnet/packs/royal/stars/api_user_get.py @@ -0,0 +1,22 @@ +from starlette.requests import Request +from starlette.responses import * +from royalnet.web import * +from royalnet.utils import * +from royalnet.packs.common.tables import User + + +class ApiUserGetStar(PageStar): + path = "/api/user/get/{uid_str}" + tables = {User} + + async def page(self, request: Request) -> JSONResponse: + uid_str = request.path_params.get("uid_str", "") + try: + uid = int(uid_str) + except (ValueError, TypeError): + return error(400, "Invalid uid") + async with self.alchemy.session_acm() as session: + user: User = await asyncify(session.query(self.alchemy.User).get, uid) + if user is None: + return error(404, "No such user") + return JSONResponse(user.json()) diff --git a/royalnet/packs/royal/stars/api_user_list.py b/royalnet/packs/royal/stars/api_user_list.py new file mode 100644 index 00000000..3b9a562e --- /dev/null +++ b/royalnet/packs/royal/stars/api_user_list.py @@ -0,0 +1,15 @@ +from starlette.requests import Request +from starlette.responses import * +from royalnet.web import * +from royalnet.utils import * +from royalnet.packs.common.tables import User + + +class ApiUserListStar(PageStar): + path = "/api/user/list" + tables = {User} + + async def page(self, request: Request) -> JSONResponse: + async with self.alchemy.session_acm() as session: + users: typing.List[User] = await asyncify(session.query(self.alchemy.User).all) + return JSONResponse([user.json() for user in users]) diff --git a/royalnet/packs/royal/tables/diario.py b/royalnet/packs/royal/tables/diario.py index 1f133ef4..32769861 100644 --- a/royalnet/packs/royal/tables/diario.py +++ b/royalnet/packs/royal/tables/diario.py @@ -1,4 +1,5 @@ import re +import datetime from sqlalchemy import Column, \ Integer, \ Text, \ @@ -38,7 +39,7 @@ class Diario: return Column(Text) @declared_attr - def timestamp(self): + def timestamp(self) -> datetime.datetime: return Column(DateTime, nullable=False) @declared_attr @@ -57,6 +58,19 @@ class Diario: def quoted_account(self): return relationship("User", foreign_keys=self.quoted_account_id, backref="diario_quoted") + def json(self) -> dict: + return { + "diario_id": self.diario_id, + "creator": self.creator.json() if self.creator else None, + "quoted_account": self.quoted_account.json() if self.quoted_account else None, + "quoted": self.quoted, + "text": self.text, + "context": self.context, + "timestamp": self.timestamp.isoformat(), + "media_url": self.media_url, + "spoiler": self.spoiler + } + def __repr__(self): return f" JSONResponse: + return JSONResponse({ + "error": description + }, status_code=code) diff --git a/royalnet/web/star.py b/royalnet/web/star.py index 265594f0..bc8c5d24 100644 --- a/royalnet/web/star.py +++ b/royalnet/web/star.py @@ -11,7 +11,7 @@ class Star: def __init__(self, constellation: "Constellation"): self.constellation: "Constellation" = constellation - async def page(self, request: Request, **kwargs) -> Response: + async def page(self, request: Request) -> Response: raise NotImplementedError() @property