Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic functions #2

Open
wants to merge 65 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
00f5a90
Added requests to requirements
Zeusina Aug 8, 2023
243cb7f
Added endpoints.py for URLs
Zeusina Aug 8, 2023
ff257e5
Added InvalidLoginOrPasswordException
Zeusina Aug 8, 2023
96f668c
Added Client class and login method
Zeusina Aug 8, 2023
cafdae0
Update LICENSE
Zeusina Aug 8, 2023
e680d56
Added types module
Zeusina Aug 9, 2023
2a68da3
Added endpoint for getting children list
Zeusina Aug 9, 2023
13a576b
Added get_child_list & login_by_token functions
Zeusina Aug 9, 2023
71396f6
Added child class
Zeusina Aug 9, 2023
81817ed
Added docstrings
Zeusina Aug 9, 2023
c6326a0
Moved request headers to request_parameters
Zeusina Aug 9, 2023
6e22e04
Added docstring in request_parameters.py
Zeusina Aug 9, 2023
b67919e
Added Teacher class(Usage not implemented yet)
Zeusina Aug 9, 2023
891e753
Fixed docstring
Zeusina Aug 9, 2023
04fb53f
Added teacher list url to endpoints
Zeusina Aug 9, 2023
2acacb0
Removed not implemented error
Zeusina Aug 9, 2023
58346ae
Import Teacher, Child classes to types module
Zeusina Aug 9, 2023
e25014f
Added method to get list of teachers of student
Zeusina Aug 9, 2023
c17d891
Added getting started at README.md
Zeusina Aug 9, 2023
ed2d722
Added subject class(Implementation work in progress)
Zeusina Aug 9, 2023
d8a746f
GITBOOK-1: change request with no subject merged in GitBook
Zeusina Aug 9, 2023
4d0c2d8
Delete SUMMARY.md
Zeusina Aug 9, 2023
53d26e4
Delete login.md
Zeusina Aug 9, 2023
303f0b4
Added link to full documentation to README.md
Zeusina Aug 9, 2023
15322aa
Merge remote-tracking branch 'origin/basic_functions' into basic_func…
Zeusina Aug 9, 2023
b9afc3d
Add ActionPayload class
Zeusina Aug 13, 2023
a70c45c
Add Education class
Zeusina Aug 13, 2023
f91f06a
Add Identity class
Zeusina Aug 13, 2023
69f5c63
Refactored main classes to match API
Zeusina Aug 13, 2023
e29d979
Updated README.md
Zeusina Aug 13, 2023
b6a1126
Removed unused imports
Zeusina Aug 14, 2023
96d25c8
Updated Child class docstring
Zeusina Aug 14, 2023
06566c5
Updated Client class docstring
Zeusina Aug 14, 2023
e181eb5
Fixed typo in logger entry
Zeusina Aug 14, 2023
ecf9963
Fixed typo in logger entry
Zeusina Aug 14, 2023
7415ef9
Added MarkEntry class(usage not implemented yet)
Zeusina Aug 14, 2023
a39e022
Added LessonEntry class(usage not implemented yet)
Zeusina Aug 14, 2023
115f91b
Added Task class(usage not implemented yet)
Zeusina Aug 14, 2023
69b30ef
Added function to get marks by period
Zeusina Aug 16, 2023
f97925c
Replaced requests.request to requests.session to increase speed
Zeusina Aug 18, 2023
c661c3b
Added using headers from request parameters file
Zeusina Aug 19, 2023
8ec922a
Added using headers from request parameters file
Zeusina Aug 19, 2023
607c783
Added new field to ActionPayload class
Zeusina Aug 19, 2023
e652e97
Added new Estimate class
Zeusina Aug 19, 2023
9631902
Added new type hints in LessonEntry class
Zeusina Aug 19, 2023
bcc3d2b
Added new endpoint to get lessons by period
Zeusina Aug 19, 2023
826d363
Removed NotImplementedError in Task class
Zeusina Aug 19, 2023
367baa7
Added function to get lessons by period
Zeusina Aug 19, 2023
bde61f4
Added docstring to Estimate class
Zeusina Aug 19, 2023
2e1e212
Updated docstring to ActionPayload class
Zeusina Aug 19, 2023
51df37c
Renamed get_lesson_list method
Zeusina Aug 19, 2023
986b727
Fixed ActionPayload import
Zeusina Sep 19, 2023
a36d50d
Fixed link for getting marks
Zeusina Sep 20, 2023
a32a5c9
Fixed getting list of child
Zeusina Sep 22, 2023
613d5ad
Added docstrings for modules
Zeusina Dec 11, 2023
d6405a2
Refactor with linter
Zeusina Dec 11, 2023
0704193
Refactor with linter
Zeusina Dec 11, 2023
9c4d773
Changed json processing method
Zeusina Dec 11, 2023
e1a14ba
refactor: use dataclasses and improve list comprehensions in Child class
Zeusina Jan 12, 2024
5dbd8d7
chore: update copyright year in LICENSE file
Zeusina Jan 12, 2024
37c6698
refactor: convert classes to dataclasses in types module
Zeusina Jan 17, 2024
95e1153
build: updated setup.py file
Zeusina Feb 13, 2024
f932f37
ci: added ci file for automatic build whl
Zeusina Feb 13, 2024
082ce2e
refactor: fixed typo in package name
Zeusina Mar 11, 2024
92225e0
build: updated build info
Zeusina Mar 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: 'Build'

on:
push:
branches:
- master


jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: "Set up python"
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: "Set version variable"
shell: bash
run: echo "WHL_VERSION=$(python setup.py --version)" >> $GITHUB_ENV
- name: "Prepare dependencies"
shell: bash
run: |
python3 -m pip install wheel setuptools
- name: "Build package"
shell: bash
run: |
python3 setup.py bdist_wheel

- uses: actions/upload-artifact@v4
with:
name: build
path: "dist/petersburgedu_wrap-${{ env.WHL_VERSION }}-py3-none-any.whl"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) [year] [fullname]
Copyright (c) 2024 Kachusov Kirill

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
# Petersburgedu wrap
This module is designed for work with petersburgedu internal API.
This module is designed for work with petersburgedu internal API.
[Full documentaton(WIP)](https://zeusina.gitbook.io/petersburgedu-wrap/)

## English
### Work in progress

## Русский(Russian)
### Работа в процессе

### Быстрый старт
Для быстрого старта в моей библиотеке вы можете воспользоваться минимальным кодом

```python
import petersburgedu_wrap
import logging

# Импорт необходимых библиотек
logging.basicConfig(level=logging.DEBUG) # Настройка логирования

client = petersburgedu_wrap.client.Client() # Создание клиента
client.login_by_token("Ваш токен") # Вход в систему через токен

children = client.get_child_list() # Получить список детей

teachers = client.children[0].get_teacher_list() # Получение списка учителей
for teacher in teachers:
print(f"Имя: {teacher.firstname}, Фамилия: {teacher.surname}, Занимаемая должность: {teacher.position_name}")
```

Как альтернатива логина через токен, который можно получить на сайте петербургского образования можно использовать логин по почте и паролю

```python
import petersburgedu_wrap
import logging

# Импорт необходимых библиотек
logging.basicConfig(level=logging.DEBUG) # Настройка логирования

client = petersburgedu_wrap.client.Client() # Создание клиента
client.login(login="Ваша почта", password="Ваш пароль") # Вход в систему через токен

children = client.get_child_list() # Получить список детей

teachers = client.children[0].get_teacher_list() # Получение списка учителей
for teacher in teachers:
print(f"Имя: {teacher.firstname}, Фамилия: {teacher.surname}, Занимаемая должность: {teacher.position_name}")
```
#### Полная документация будет написана позже
5 changes: 5 additions & 0 deletions petersburgedu_wrap/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
Library for accessing petersburg education website via python
"""
from petersburgedu_wrap.client import Client
import petersburgedu_wrap.types
138 changes: 138 additions & 0 deletions petersburgedu_wrap/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import logging
import requests

from petersburgedu_wrap.types.education import Education
from petersburgedu_wrap.utils import endpoints, request_parameters
from petersburgedu_wrap.errors.invalid_login_or_password_exc import InvalidLoginOrPasswordException
from petersburgedu_wrap.types import Child, ActionPayload, Identity


class Client:
def __init__(self) -> None:
"""
Init function for Client class.
Used for set parameters in Client class.
:return:
"""
self._token = None
self.children: list[Child] = []
self.logger = logging.getLogger(__name__)
self.logger.debug("Client was created")

def login(self, login: str, password: str) -> None:
"""
Function to log in petersburg educational portal with email and password.
This function will get JWT token and store it as Client class parameter.
:param login:
:param password:
:return:
"""
url = endpoints.LOGIN_URL
self.logger.debug("Preparing payload & headers to login as %login%".replace("%login%", login))
payload = (
'{"type": "email", "login": "%login%", "activation_code": null, "password": "%password%", "_isEmpty": '
'false}'.replace("%login%", login).replace("%password%", password))
headers = request_parameters.headers
self.logger.debug("Sending login request as %login%".replace("%login%", login))
response = requests.request("POST", url, headers=headers, data=payload)
self.logger.debug(
"Request was successfully sent, status code: %code%".replace("%code%", str(response.status_code)))
if response.status_code == 200:
json_response: dict = response.json()
token = json_response.get("data", None).get("token", None)
if token:
self._token = token
else:
raise IndexError
elif response.status_code == 400:
raise InvalidLoginOrPasswordException
else:
raise ValueError

def login_by_token(self, token: str) -> None:
"""
This function will store JWT token as Client class parameter
USE IF YOU HAVE JWT TOKEN
:rtype: object
:param token:
:return:
"""
self.logger.debug("Registered by token")
self._token = token

def get_child_list(self) -> list[Child]:
"""
This function will get child list from petersburg educational portal, store it as class parameter and return
it as list.
:rtype: list[Child]
:return: list
"""
url = endpoints.RELATED_CHILD_LIST_URL
self.logger.info("Preparing data for getting child list")
payload = {}
headers = request_parameters.headers
cookies = {
"X-JWT-Token": self._token
}
self.logger.debug("Sending request to API to get child list")
response = requests.request("GET", url, headers=headers, data=payload, cookies=cookies)
self.logger.debug(
"Response with child list return %code% - status code".replace("%code%", str(response.status_code)))
if response.status_code != 200:
return []
response_json: dict = response.json()
for child in response_json["data"]["items"]:
firstname = child.get("firstname", "")
surname = child.get("surname", "")
middlename = child.get("middlename", "")
hash_uid = child.get("hash_uid", "")
action_payload = ActionPayload(
can_apply_for_distance=child["action_payload"].get("can_apply_for_distance", True),
can_print=child["action_payload"].get("can_print", None))
identity = Identity(id=child["identity"]["id"])
educations: list[Education] = []
for education in child["educations"]:
push_subscribe = education.get("push_subscribe", "true")
education_id = education["education_id"]
group_id = education["group_id"]
group_name = education.get("group_name", "")
institution_id = education["institution_id"]
institution_name = education.get("institution_name", "")
is_active = education.get("is_active", None)
distance_education = education.get("distance_education", False)
distance_education_updated_at = education.get("distance_education_updated_at", "")
parent_firstname = education.get("parent_firstname", "")
parent_surname = education.get("parent_surname", "")
parent_middlename = education.get("parent_middlename", "")
parent_email = education.get("parent_email", "")
jurisdiction_name = education.get("jurisdiction_name", "")
jurisdiction_id = education["jurisdiction_id"]
edu = Education(push_subscribe=push_subscribe,
education_id=education_id,
group_id=group_id,
group_name=group_name,
institution_id=institution_id,
institution_name=institution_name,
is_active=is_active,
distance_education=distance_education,
distance_education_updated_at=distance_education_updated_at,
parent_firstname=parent_firstname,
parent_surname=parent_surname,
parent_middlename=parent_middlename,
parent_email=parent_email,
jurisdiction_name=jurisdiction_name,
jurisdiction_id=jurisdiction_id)
educations.append(edu)

self.children.append(
Child(
firstname=firstname,
surname=surname,
middlename=middlename,
educations=educations,
hash_uid=hash_uid,
action_payload=action_payload,
identity=identity,
token=self._token
))
return self.children
File renamed without changes.
10 changes: 10 additions & 0 deletions petersburgedu_wrap/errors/invalid_login_or_password_exc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class InvalidLoginOrPasswordException(Exception):
def __init__(self, *args):
"""
Error raised when login or password are incorrect when user log in.
:param args:
"""
if args:
self.message = args[0]
else:
self.message = None
7 changes: 7 additions & 0 deletions petersburgedu_wrap/types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Module used for storing types of this library
"""
from petersburgedu_wrap.types.child import Child
from petersburgedu_wrap.types.teacher import Teacher
from petersburgedu_wrap.types.action_payload import ActionPayload
from petersburgedu_wrap.types.identity import Identity
14 changes: 14 additions & 0 deletions petersburgedu_wrap/types/action_payload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import dataclasses


@dataclasses.dataclass
class ActionPayload:
"""
Class represents action payload API entity
:param can_apply_for_distance:
:param can_print:
:param can_add_homework:
"""
can_apply_for_distance: bool = True
can_print: bool = True
can_add_homework: bool = True
Loading