-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
283 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Modeus API implemented using a controller. | ||
""" | ||
|
||
from typing import Optional | ||
|
||
from blacksheep import Response | ||
from blacksheep.server.bindings import FromJson | ||
from blacksheep.server.controllers import Controller, post | ||
from pydantic import BaseModel | ||
from requests import RequestException | ||
|
||
from integration import modeus | ||
from integration.exceptions import ModeusError | ||
|
||
|
||
class ModeusCreds(BaseModel): | ||
"""Modeus creds.""" | ||
|
||
username: str | ||
password: str | ||
|
||
|
||
class NetologyController(Controller): | ||
"""Controller for Modeus API.""" | ||
|
||
@classmethod | ||
def route(cls) -> Optional[str]: | ||
"""Get route.""" | ||
return "/api/modeus" | ||
|
||
@classmethod | ||
def class_name(cls) -> str: | ||
"""Get class name.""" | ||
return "Modeus" | ||
|
||
@post() | ||
async def get_modeus_cookies(self, item: FromJson[ModeusCreds]) -> Response: | ||
""" | ||
Auth in Modeus and return cookies. | ||
""" | ||
try: | ||
return self.json( | ||
await modeus.login(item.value.username, item.value.password), | ||
) | ||
except (RequestException, ModeusError) as exception: | ||
return self.json({"error": f"can't authenticate {exception}"}, status=400) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.