From da03f8e6adb166e85bc81706071438bcae317e8d Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 9 Feb 2020 19:12:34 +0100 Subject: [PATCH] probably break some stuff --- royalpack/stars/__init__.py | 25 ++++++++++--------------- royalpack/stars/api_diario_get.py | 6 ++---- royalpack/stars/api_diario_list.py | 1 - royalpack/stars/api_discord_cv.py | 3 --- royalpack/stars/api_discord_play.py | 8 ++------ royalpack/stars/api_user_get.py | 20 ++++++++++---------- royalpack/stars/api_user_list.py | 12 ++++++------ royalpack/stars/api_wiki_get.py | 22 +++++++++++----------- royalpack/stars/api_wiki_list.py | 12 ++++++------ 9 files changed, 47 insertions(+), 62 deletions(-) diff --git a/royalpack/stars/__init__.py b/royalpack/stars/__init__.py index a5f71714..20c08bbd 100644 --- a/royalpack/stars/__init__.py +++ b/royalpack/stars/__init__.py @@ -1,29 +1,24 @@ # Imports go here! +from .api_diario_get import ApiDiarioGetStar +from .api_diario_list import ApiDiarioListStar from .api_user_list import ApiUserListStar from .api_user_get import ApiUserGetStar -from .api_diario_list import ApiDiarioListStar -from .api_diario_get import ApiDiarioGetStar from .api_discord_cv import ApiDiscordCvStar -from .api_wiki_get import ApiWikiGetStar -from .api_wiki_list import ApiUserListStar from .api_discord_play import ApiDiscordPlayStar +from .api_wiki_get import ApiWikiGetStar +from .api_wiki_list import ApiWikiListStar # Enter the PageStars of your Pack here! available_page_stars = [ - ApiUserListStar, - ApiUserGetStar, - ApiDiarioListStar, ApiDiarioGetStar, + ApiDiarioListStar, ApiDiscordCvStar, - ApiWikiGetStar, - ApiUserListStar, ApiDiscordPlayStar, -] - -# Enter the ExceptionStars of your Pack here! -available_exception_stars = [ - + ApiUserGetStar, + ApiUserListStar, + ApiWikiGetStar, + ApiWikiListStar, ] # Don't change this, it should automatically generate __all__ -__all__ = [star.__name__ for star in [*available_page_stars, *available_exception_stars]] +__all__ = [star.__name__ for star in available_page_stars] diff --git a/royalpack/stars/api_diario_get.py b/royalpack/stars/api_diario_get.py index bbbaebc2..6170241b 100644 --- a/royalpack/stars/api_diario_get.py +++ b/royalpack/stars/api_diario_get.py @@ -1,5 +1,3 @@ -from starlette.requests import Request -from starlette.responses import * from royalnet.constellation.api import * from royalnet.utils import * from ..tables import * @@ -10,9 +8,9 @@ class ApiDiarioGetStar(ApiStar): async def api(self, data: ApiData) -> dict: try: - diario_id = int(data["diario_id"]) + diario_id = int(data["id"]) except ValueError: - raise InvalidParameterError("'diario_id' is not a valid int.") + raise InvalidParameterError("'id' is not a valid int.") entry: Diario = await asyncify(data.session.query(self.alchemy.get(Diario)).get, diario_id) if entry is None: raise NotFoundError("No such diario entry.") diff --git a/royalpack/stars/api_diario_list.py b/royalpack/stars/api_diario_list.py index 3966bc2a..95b29b6f 100644 --- a/royalpack/stars/api_diario_list.py +++ b/royalpack/stars/api_diario_list.py @@ -1,4 +1,3 @@ -from starlette.requests import Request from starlette.responses import * from royalnet.constellation.api import * from royalnet.utils import * diff --git a/royalpack/stars/api_discord_cv.py b/royalpack/stars/api_discord_cv.py index 1d1066b3..a1b9f189 100644 --- a/royalpack/stars/api_discord_cv.py +++ b/royalpack/stars/api_discord_cv.py @@ -1,7 +1,4 @@ -from starlette.requests import Request -from starlette.responses import * from royalnet.constellation.api import * -from royalnet.utils import * class ApiDiscordCvStar(ApiStar): diff --git a/royalpack/stars/api_discord_play.py b/royalpack/stars/api_discord_play.py index bffe053a..881cae59 100644 --- a/royalpack/stars/api_discord_play.py +++ b/royalpack/stars/api_discord_play.py @@ -1,6 +1,4 @@ from typing import * -from starlette.requests import Request -from starlette.responses import * from royalnet.constellation.api import * import logging @@ -9,7 +7,7 @@ log = logging.getLogger(__name__) class ApiDiscordPlayStar(ApiStar): - path = "/api/discord/play" + path = "/api/discord/play/v1" async def api(self, data: ApiData) -> dict: url = data["url"] @@ -25,6 +23,4 @@ class ApiDiscordPlayStar(ApiStar): url=url, guild_id=guild_id, user=user) - return JSONResponse(response, headers={ - "Access-Control-Allow-Origin": "*", - }) + return response diff --git a/royalpack/stars/api_user_get.py b/royalpack/stars/api_user_get.py index c8b95e6d..f04d1288 100644 --- a/royalpack/stars/api_user_get.py +++ b/royalpack/stars/api_user_get.py @@ -3,19 +3,19 @@ from starlette.responses import * from royalnet.constellation import * from royalnet.utils import * from royalnet.backpack.tables import * +from royalnet.constellation.api import * -class ApiUserGetStar(PageStar): - path = "/api/user/get/{uid_str}" +class ApiUserGetStar(ApiStar): + path = "/api/user/get/v1" - async def page(self, request: Request) -> JSONResponse: - uid_str = request.path_params.get("uid_str", "") + async def api(self, data: ApiData) -> dict: + user_id_str = data["id"] try: - uid = int(uid_str) + user_id = int(user_id_str) except (ValueError, TypeError): - return shoot(400, "Invalid uid") - async with self.alchemy.session_acm() as session: - user: User = await asyncify(session.query(self.alchemy.get(User)).get, uid) + raise InvalidParameterError("'id' is not a valid int.") + user: User = await asyncify(data.session.query(self.alchemy.get(User)).get, user_id) if user is None: - return shoot(404, "No such user") - return JSONResponse(user.json()) + raise NotFoundError("No such user.") + return user.json() diff --git a/royalpack/stars/api_user_list.py b/royalpack/stars/api_user_list.py index f520472c..44b0aeff 100644 --- a/royalpack/stars/api_user_list.py +++ b/royalpack/stars/api_user_list.py @@ -3,12 +3,12 @@ from starlette.responses import * from royalnet.constellation import * from royalnet.utils import * from royalnet.backpack.tables import * +from royalnet.constellation.api import * -class ApiUserListStar(PageStar): - path = "/api/user/list" +class ApiUserListStar(ApiStar): + path = "/api/user/list/v1" - 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.get(User)).all) - return JSONResponse([user.json() for user in users]) + async def api(self, data: ApiData) -> dict: + users: typing.List[User] = await asyncify(data.session.query(self.alchemy.get(User)).all) + return [user.json() for user in users] diff --git a/royalpack/stars/api_wiki_get.py b/royalpack/stars/api_wiki_get.py index 72f5d3c6..ee1127dd 100644 --- a/royalpack/stars/api_wiki_get.py +++ b/royalpack/stars/api_wiki_get.py @@ -4,19 +4,19 @@ from royalnet.constellation import * from royalnet.utils import * from ..tables import * import uuid +from royalnet.constellation.api import * -class ApiWikiGetStar(PageStar): - path = "/api/wiki/get/{wiki_page_uuid}" +class ApiWikiGetStar(ApiStar): + path = "/api/wiki/get/v1" - async def page(self, request: Request) -> JSONResponse: - wiki_page_uuid_str = request.path_params.get("wiki_page_uuid", "") + async def api(self, data: ApiData) -> dict: + wikipage_id_str = data["id"] try: - wiki_page_uuid = uuid.UUID(wiki_page_uuid_str) + wikipage_id = uuid.UUID(wikipage_id_str) except (ValueError, AttributeError, TypeError): - return shoot(400, "Invalid wiki_page_uuid") - async with self.alchemy.session_acm() as session: - wikipage: WikiPage = await asyncify(session.query(self.alchemy.get(WikiPage)).get, wiki_page_uuid) - if wikipage is None: - return shoot(404, "No such page") - return JSONResponse(wikipage.json_full()) + raise InvalidParameterError("'id' is not a valid UUID.") + wikipage: WikiPage = await asyncify(data.session.query(self.alchemy.get(WikiPage)).get, wikipage_id) + if wikipage is None: + raise NotFoundError("No such page.") + return wikipage.json_full() diff --git a/royalpack/stars/api_wiki_list.py b/royalpack/stars/api_wiki_list.py index a0789947..ca378635 100644 --- a/royalpack/stars/api_wiki_list.py +++ b/royalpack/stars/api_wiki_list.py @@ -4,12 +4,12 @@ from royalnet.constellation import * from royalnet.utils import * from royalnet.backpack.tables import * from ..tables import * +from royalnet.constellation.api import * -class ApiUserListStar(PageStar): - path = "/api/wiki/list" +class ApiWikiListStar(ApiData): + path = "/api/wiki/list/v1" - async def page(self, request: Request) -> JSONResponse: - async with self.alchemy.session_acm() as session: - pages: typing.List[WikiPage] = await asyncify(session.query(self.alchemy.get(WikiPage)).all) - return JSONResponse([page.json_list() for page in pages]) + async def api(self, data: ApiData) -> dict: + pages: typing.List[WikiPage] = await asyncify(data.session.query(self.alchemy.get(WikiPage)).all) + return [page.json_list() for page in pages]