Skip to content

Commit

Permalink
add route get calendar #20
Browse files Browse the repository at this point in the history
  • Loading branch information
depocoder committed Sep 26, 2024
1 parent be89089 commit 85de7b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
21 changes: 20 additions & 1 deletion backend/app/controllers/netology.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import Optional

from blacksheep import Response
from blacksheep import Response, FromQuery
from blacksheep.server.bindings import FromJson
from blacksheep.server.controllers import Controller, post
from httpx import HTTPStatusError
Expand Down Expand Up @@ -68,3 +68,22 @@ async def get_course(
return self.json({"error": f"can't authenticate {exception}"}, status=400)
except NetologyUnauthorizedError as exception:
return self.json({"error": f"{exception}"}, status=401)

@post('/calendar/')
async def get_calendar(
self,
program_id: FromQuery[int],
cookies: FromJson[NetologyCookies],
) -> Response:
"""
Auth in Netology and return cookies.
"""
try:
course = await netology.get_calendar(cookies.value, program_id.value)
return self.json(
course,
)
except (RequestException, HTTPStatusError) as exception:
return self.json({"error": f"can't authenticate {exception}"}, status=400)
except NetologyUnauthorizedError as exception:
return self.json({"error": f"{exception}"}, status=401)
37 changes: 12 additions & 25 deletions backend/integration/netology.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""Netology API implementation."""
from http.client import UNAUTHORIZED
from typing import List, Any
from typing import Any

import httpx
import requests
from httpx import AsyncClient

from app.controllers.models import NetologyCookies, NetologyPrograms, NetologyProgram
Expand All @@ -30,11 +29,11 @@ async def auth_netology(username: str, password: str, timeout: int = 15) -> Neto
timeout=timeout,
)
response = await session.post('/backend/api/user/sign_in', data={
"login": username,
"password": password,
"remember": "1",
},
)
"login": username,
"password": password,
"remember": "1",
},
)
if response.status_code == UNAUTHORIZED:
raise NetologyUnauthorizedError('Username/password is incorrect.')
response.raise_for_status()
Expand All @@ -57,25 +56,13 @@ async def send_request(
return response.json()


def get_program_ids(session: requests.Session) -> List[str]:
"""Get your Netology program ids."""
response = session.get(
"https://netology.ru/backend/api/user/programs/calendar/filters",
)
response.raise_for_status()
serialized_response = response.json()
programs = serialized_response["programs"]
return [program["id"] for program in programs]


def get_calendar(session: requests.Session, calendar_id: str) -> NetologyCookies:
async def get_calendar(cookies: NetologyCookies, calendar_id: int) -> dict[str, Any]:
"""Get your calendar events."""
response = session.get(
"https://netology.ru/backend/api/user/programs/calendar",
params={"program_ids[]": f"{calendar_id}"},
)
response.raise_for_status()
return NetologyCookies.model_validate_json(response.text)
response = await send_request(cookies, request_settings={
'method': 'GET', 'url': '/backend/api/user/programs/calendar',
'params': {'program_ids[]': calendar_id},
})
return response


async def get_utmn_course(cookies: NetologyCookies) -> NetologyProgram:
Expand Down

0 comments on commit 85de7b1

Please sign in to comment.