Skip to content

Commit

Permalink
v0.2.3
Browse files Browse the repository at this point in the history
- Добавлена авторизация напрямую через логин и пароль от "Моей Школы"
- Исправлена обработка ошибок от API
- Добавлены 3 новых метода в Mobile API "Моей школы" и соответственно типы для них
  • Loading branch information
Den4ikSuperOstryyPer4ik committed Sep 21, 2023
1 parent d2a842e commit d79385c
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 5 deletions.
2 changes: 1 addition & 1 deletion octodiary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# https://opensource.org/licenses/MIT
# https://github.com/OctoDiary

__version__ = "0.2.2"
__version__ = "0.2.3"

from . import asyncApi, syncApi, exceptions, types

Expand Down
3 changes: 2 additions & 1 deletion octodiary/asyncApi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# https://opensource.org/licenses/MIT
# https://github.com/OctoDiary

from json import JSONDecodeError
from typing import Optional

import aiohttp
Expand Down Expand Up @@ -63,7 +64,7 @@ async def _check_response(response: aiohttp.ClientResponse):
description=json_response.get("description", None),
details=json_response.get("details", None),
)
except:
except JSONDecodeError:
raise APIError(
url=str(response.url),
status_code=response.status,
Expand Down
103 changes: 103 additions & 0 deletions octodiary/asyncApi/myschool/mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
SubjectMarksForSubject,
UserChildrens,
UserSettings,
RatingRankClass,
RatingRankSubject,
RatingRankShort,
)
from octodiary.types.myschool.web import SessionUserInfo

from ..base import AsyncBaseApi

Expand All @@ -46,6 +50,31 @@ class AsyncMobileAPI(AsyncBaseApi):
Async Mobile API class wrapper.
"""

async def login(self, username: str, password: str) -> str:
"""Авторизоваться и получить токен напрямую через обычный логин и пароль."""
return (
await self.get(
url="https://authedu.mosreg.ru/v3/auth/kauth/callback",
required_token=False, return_raw_response=True,
params={
"code": (
await self.post(
url="https://authedu.mosreg.ru/lms/api/sessions",
required_token=False,
json={
"login": username,
"password_plain": password
},
model=SessionUserInfo,
custom_headers={
"Accept": "application/json"
}
)
).authentication_token
}
)
).cookies.get("aupd_token").value

async def handle_action(self, response: ClientResponse, action: str = None, failed: str = None) -> str | bool:
match action or failed:
case None:
Expand Down Expand Up @@ -492,3 +521,77 @@ async def get_lesson_schedule_items(
},
model=LessonScheduleItems
)

async def get_rating_rank_class(
self,
profile_id: int,
person_id: str,
classUnitId: int,
date: date = None
) -> list[RatingRankClass]:
"""
Получить общий рейтинг класса
"""
return await self.get(
url="https://authedu.mosreg.ru/api/ej/rating/v1/rank/class",
model=RatingRankClass, is_list=True,
custom_headers={
"x-mes-subsystem": "familymp",
"client-type": "diary-mobile",
"profile-id": profile_id
},
params={
"person_id": person_id,
"classUnitId": classUnitId,
"date": self.date_to_string(date)
}
)

async def get_raging_rank_short(
self,
profile_id: int,
person_id: str,
begin_date: date,
end_date: date
) -> list[RatingRankShort]:
"""
Получить общий рейтинг класса
"""
return await self.get(
url="https://authedu.mosreg.ru/api/ej/rating/v1/rank/class",
model=RatingRankShort, is_list=True,
custom_headers={
"x-mes-subsystem": "familymp",
"client-type": "diary-mobile",
"profile-id": profile_id
},
params={
"person_id": person_id,
"beginDate": self.date_to_string(begin_date),
"endDate": self.date_to_string(end_date),
}
)

async def get_rating_rank_subjects(
self,
profile_id: int,
person_id: str,
date: date
) -> list[RatingRankSubject]:
"""
Получить рейтинг по предметам
"""
return await self.get(
url="https://authedu.mosreg.ru/api/ej/rating/v1/rank/subject",
model=RatingRankSubject, is_list=True,
custom_headers={
"x-mes-subsystem": "familymp",
"client-type": "diary-mobile",
"profile-id": profile_id
},
params={
"personId": person_id,
"date": self.date_to_string(date),
}
)

25 changes: 25 additions & 0 deletions octodiary/asyncApi/myschool/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ class AsyncWebAPI(AsyncBaseApi):
Async Web API class wrapper.
"""

async def login(self, username: str, password: str) -> str:
"""Авторизоваться и получить токен напрямую через обычный логин и пароль."""
return (
await self.get(
url="https://authedu.mosreg.ru/v3/auth/kauth/callback",
required_token=False, return_raw_response=True,
params={
"code": (
await self.post(
url="https://authedu.mosreg.ru/lms/api/sessions",
required_token=False,
json={
"login": username,
"password_plain": password
},
model=SessionUserInfo,
custom_headers={
"Accept": "application/json"
}
)
).authentication_token
}
)
).cookies.get("aupd_token").value

async def handle_action(self, response: ClientResponse, action: str = None, failed: str = None) -> str | bool:
match action or failed:
case None:
Expand Down
2 changes: 1 addition & 1 deletion octodiary/syncApi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _check_response(response: requests.Response):
description=json_response.get("description", None),
details=json_response.get("details", None),
)
except:
except requests.exceptions.JSONDecodeError:
raise APIError(
url=str(response.url),
status_code=response.status_code,
Expand Down
103 changes: 103 additions & 0 deletions octodiary/syncApi/myschool/mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
SubjectMarksForSubject,
UserChildrens,
UserSettings,
RatingRankClass,
RatingRankSubject,
RatingRankShort
)
from octodiary.types.myschool.web import SessionUserInfo

from ..base import SyncBaseApi

Expand All @@ -47,6 +51,31 @@ class SyncMobileAPI(SyncBaseApi):
Sync Mobile API class wrapper.
"""

def login(self, username: str, password: str) -> str:
"""Авторизоваться и получить токен напрямую через обычный логин и пароль."""
return (
self.get(
url="https://authedu.mosreg.ru/v3/auth/kauth/callback",
required_token=False, return_raw_response=True,
params={
"code": (
self.post(
url="https://authedu.mosreg.ru/lms/api/sessions",
required_token=False,
json={
"login": username,
"password_plain": password
},
model=SessionUserInfo,
custom_headers={
"Accept": "application/json"
}
)
).authentication_token
}
)
).cookies["aupd_token"]

def handle_action(self, response: Response, action: str = None, failed: str = None) -> str | bool:
match action or failed:
case None:
Expand Down Expand Up @@ -499,3 +528,77 @@ def get_lesson_schedule_items(
},
model=LessonScheduleItems
)

def get_rating_rank_class(
self,
profile_id: int,
person_id: str,
classUnitId: int,
date: date = None
) -> list[RatingRankClass]:
"""
Получить общий рейтинг класса
"""
return self.get(
url="https://authedu.mosreg.ru/api/ej/rating/v1/rank/class",
model=RatingRankClass, is_list=True,
custom_headers={
"x-mes-subsystem": "familymp",
"client-type": "diary-mobile",
"profile-id": profile_id
},
params={
"person_id": person_id,
"classUnitId": classUnitId,
"date": self.date_to_string(date)
}
)

def get_raging_rank_short(
self,
profile_id: int,
person_id: str,
begin_date: date,
end_date: date
) -> list[RatingRankShort]:
"""
Получить общий рейтинг класса
"""
return self.get(
url="https://authedu.mosreg.ru/api/ej/rating/v1/rank/class",
model=RatingRankShort, is_list=True,
custom_headers={
"x-mes-subsystem": "familymp",
"client-type": "diary-mobile",
"profile-id": profile_id
},
params={
"person_id": person_id,
"beginDate": self.date_to_string(begin_date),
"endDate": self.date_to_string(end_date),
}
)

def get_rating_rank_subjects(
self,
profile_id: int,
person_id: str,
date: date
) -> list[RatingRankSubject]:
"""
Получить рейтинг по предметам
"""
return self.get(
url="https://authedu.mosreg.ru/api/ej/rating/v1/rank/subject",
model=RatingRankSubject, is_list=True,
custom_headers={
"x-mes-subsystem": "familymp",
"client-type": "diary-mobile",
"profile-id": profile_id
},
params={
"personId": person_id,
"date": self.date_to_string(date),
}
)

25 changes: 25 additions & 0 deletions octodiary/syncApi/myschool/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ class SyncWebAPI(SyncBaseApi):
Sync Web API class wrapper.
"""

def login(self, username: str, password: str) -> str:
"""Авторизоваться и получить токен напрямую через обычный логин и пароль."""
return (
self.get(
url="https://authedu.mosreg.ru/v3/auth/kauth/callback",
required_token=False, return_raw_response=True,
params={
"code": (
self.post(
url="https://authedu.mosreg.ru/lms/api/sessions",
required_token=False,
json={
"login": username,
"password_plain": password
},
model=SessionUserInfo,
custom_headers={
"Accept": "application/json"
}
)
).authentication_token
}
)
).cookies["aupd_token"]

def handle_action(self, response: Response, action: str = None, failed: str = None) -> str | bool:
match action or failed:
case None:
Expand Down
4 changes: 2 additions & 2 deletions octodiary/types/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import json
import typing
from datetime import datetime
from datetime import datetime, date
from enum import Enum

from pydantic import BaseModel
Expand All @@ -25,7 +25,7 @@ class Type(BaseModel):
def __default__(type: "Type"):
if isinstance(type, (bytes, typing.Match)):
return repr(type)
elif isinstance(type, (Enum, datetime)):
elif isinstance(type, (Enum, datetime, date)):
return str(type)

return {
Expand Down
5 changes: 5 additions & 0 deletions octodiary/types/myschool/mobile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from .user_childrens import UserChildrens
from .user_settings import UserSettings
from .users_profile_info import ProfileInfo
from .rating import RatingRankClass, RatingRankShort, RatingRankSubject


__all__ = [
"EventsResponse",
Expand All @@ -44,4 +46,7 @@
"Notification",
"SubjectMarksForSubject",
"LessonScheduleItems",
"RatingRankClass",
"RatingRankShort",
"RatingRankSubject"
]
Loading

0 comments on commit d79385c

Please sign in to comment.