-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrequests.py
73 lines (59 loc) · 2.67 KB
/
nrequests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Network requests to LXP IThub. Return specific values or dict.
Copyright (c) 2024, Igor Molchanov. Please see the AUTHORS file
for details. All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
"""
import aiohttp
async def sign_in(session: aiohttp.ClientSession, login: str, password: str) -> tuple[str, int]:
"""
Authorization in the system.
Returns the user's authorization token and id.
"""
data = {
"login": login,
"password": password,
}
async with session.post('/api/v2/auth/sign-in', data=data) as response:
response_json = await response.json()
if not response.ok:
message = response_json['message']
raise RuntimeError(f"Not getting the authorization key: {message}")
return response_json['token'], response_json['data']['id']
async def subject(session: aiohttp.ClientSession, subject_id: int) -> dict:
"""Getting a subject from LXP IThub."""
async with session.get(f'/api/v2/subjects/{subject_id}') as response:
response_json = await response.json()
if not response.ok:
message = response_json['message']
raise RuntimeError(f"Not getting the subject: {message}")
return response_json['data']
async def step(session: aiohttp.ClientSession, step_id: int) -> dict:
"""Getting a step from LXP IThub."""
async with session.get(f'/api/v2/lessons/{step_id}') as response:
response_json = await response.json()
if not response.ok:
message = response_json['message']
raise RuntimeError(f"Not getting the step: {message}")
return response_json['data']
async def load_file(session: aiohttp.ClientSession, link: str) -> bytes:
"""Getting a file from LXP IThub."""
if "https" not in link:
async with session.get(link, allow_redirects=True) as response:
return await response.content.read()
async with aiohttp.ClientSession() as session:
async with session.get(link, allow_redirects=True) as response:
return await response.content.read()
async def student_subjects_per_page(
session: aiohttp.ClientSession,
page: int,
user_id: int
) -> dict:
"""Lazy load request to get a list of the user's subjects by student role."""
address = f'/api/v2/users/{user_id}/subjects?role=student&page={page}'
async with session.get(address) as response:
response_json = await response.json()
if not response.ok:
message = response_json['message']
raise RuntimeError(f"Not getting the subjects: {message}")
return response_json['data']