Skip to content

Commit

Permalink
add modues event search controller
Browse files Browse the repository at this point in the history
  • Loading branch information
AzamatKomaev committed Sep 23, 2024
1 parent 787d78c commit 6384024
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
29 changes: 29 additions & 0 deletions backend/app/controllers/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from datetime import datetime

from pydantic import BaseModel, Field


class NetologyCreds(BaseModel):
"""Netology creds."""

username: str
password: str


class ModeusCreds(BaseModel):
"""Modeus creds."""

username: str
password: str

"""
{"size":500,"timeMin":"2024-09-23T00:00:00+03:00","timeMax":"2024-09-29T23:59:59+03:00","attendeePersonId":["d69c87c8-aece-4f39-b6a2-7b467b968211"]}
_"""

class ModeusSearchEvents(BaseModel):
"""Modeus search events body."""

size: int
time_min: datetime = Field(alias="timeMin")
time_max: datetime = Field(alias="timeMax")
attendee_person_id: list[str] = Field(alias="attendeePersonId")
36 changes: 26 additions & 10 deletions backend/app/controllers/modeus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
Modeus API implemented using a controller.
"""


from datetime import datetime
from typing import Optional

from blacksheep import Response
from blacksheep.server.bindings import FromJson
from blacksheep.server.bindings import FromJson, FromHeader
from blacksheep.server.controllers import Controller, post
from integration import modeus
from integration.exceptions import ModeusError
from pydantic import BaseModel
from requests import RequestException

from integration import modeus
from integration.exceptions import ModeusError
from . import models

class ModeusCreds(BaseModel):
"""Modeus creds."""

username: str
password: str
class FromAuthorizationHeader(FromHeader[str]):
name = "Authorization"


class NetologyController(Controller):
class ModeusController(Controller):
"""Controller for Modeus API."""

@classmethod
Expand All @@ -34,7 +34,7 @@ def class_name(cls) -> str:
return "Modeus"

@post()
async def get_modeus_cookies(self, item: FromJson[ModeusCreds]) -> Response:
async def get_modeus_cookies(self, item: FromJson[models.ModeusCreds]) -> Response:
"""
Auth in Modeus and return cookies.
"""
Expand All @@ -44,3 +44,19 @@ async def get_modeus_cookies(self, item: FromJson[ModeusCreds]) -> Response:
)
except (RequestException, ModeusError) as exception:
return self.json({"error": f"can't authenticate {exception}"}, status=400)

@post("/events/")
async def get_modeus_events(self, auth: FromAuthorizationHeader, item: FromJson[models.ModeusSearchEvents]) -> Response:
"""
Get events from Modeus.
"""
try:
jwt = auth.value.split()[1]
return self.json(
await modeus.get_events(jwt, item.value)
)
except IndexError as exception:
return self.json({"error": "cannot parse authorization header"})
except (RequestException, ModeusError) as exception:
return self.json({"error": f"can't authenticate {exception}"}, status=400)

12 changes: 3 additions & 9 deletions backend/app/controllers/netology.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@
from blacksheep import Response
from blacksheep.server.bindings import FromJson
from blacksheep.server.controllers import Controller, post
from integration import netology
from pydantic import BaseModel
from requests import RequestException


class NetologyCreds(BaseModel):
"""Netology creds."""

username: str
password: str
from integration import netology
from . import models


class NetologyController(Controller):
Expand All @@ -33,7 +27,7 @@ def class_name(cls) -> str:
return "Netology"

@post()
async def get_netology_cookies(self, item: FromJson[NetologyCreds]) -> Response:
async def get_netology_cookies(self, item: FromJson[models.NetologyCreds]) -> Response:
"""
Auth in Netology and return cookies.
"""
Expand Down
22 changes: 22 additions & 0 deletions backend/integration/modeus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import httpx
from bs4 import BeautifulSoup, Tag
from httpx import URL, AsyncClient

from app.controllers.models import ModeusSearchEvents
from integration.exceptions import CannotAuthenticateError, LoginFailedError


_token_re = re.compile(r"id_token=([a-zA-Z0-9\-_.]+)")
_AUTH_URL = "https://auth.modeus.org/oauth2/authorize"

Expand Down Expand Up @@ -108,3 +111,22 @@ def _extract_token_from_url(url: str, match_index: int = 1) -> str | None:
if (match := _token_re.search(url)) is None:
return None
return match[match_index]


async def get_events(__jwt: str, body: ModeusSearchEvents, timeout: int = 15) -> list | None:
"""Get events for student in modeus"""
session = AsyncClient(
http2=True,
base_url="https://utmn.modeus.org/",
timeout=timeout,
)
headers = {
"Authorization": f"Bearer {__jwt}",
"accept": "application/json",
"content-type": "application/json"
}
response = await session.post("/schedule-calendar-v2/api/calendar/events/search",
headers=headers,
json=body.json(by_alias=True))
print(response.text)
return response.json()
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
environment:
APP_HOST: "0.0.0.0"
APP_PORT: "8000"
env_file:
- .env
volumes:
- ./backend:/app/
ports:
Expand Down

0 comments on commit 6384024

Please sign in to comment.