Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.

Commit b364886

Browse files
committed
Various name and type fixes + a method to get schools
1 parent f41de67 commit b364886

3 files changed

Lines changed: 40 additions & 26 deletions

File tree

netschoolapi/async_client_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async def _infinite_request(
5454
while True:
5555
try:
5656
response = await self.client.request(
57-
method, path, params=params, json=json, data=data
57+
method, path, params=params, json=json, data=data # type: ignore
5858
)
5959
except httpx.ReadTimeout:
6060
pass

netschoolapi/netschoolapi.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ def __init__(
4040
async def __aenter__(self) -> 'NetSchoolAPI':
4141
return self
4242

43-
async def __aexit__(self, exc_type, exc_val, exc_tb):
43+
async def __aexit__(self, _exc_type, _exc_val, _exc_tb):
4444
await self.logout()
4545

4646
async def login(
47-
self, user_name: str, password: str, school: str,
47+
self, user_name: str, password: str,
48+
school_name_or_id: Union[int, str],
4849
requests_timeout: int = None):
4950
requester = self._wrapped_client.make_requester(requests_timeout)
5051
# Getting the `NSSESSIONID` cookie for `auth/getdata`
@@ -66,7 +67,7 @@ async def login(
6667
'login',
6768
data={
6869
'loginType': 1,
69-
**(await self._address(school, requester)),
70+
**(await self._address(school_name_or_id, requester)),
7071
'un': user_name,
7172
'pw': pw,
7273
'pw2': pw2,
@@ -112,7 +113,7 @@ async def login(
112113
assignment['id']: assignment['name']
113114
for assignment in assignment_reference
114115
}
115-
self._login_data = (user_name, password, school)
116+
self._login_data = (user_name, password, school_name_or_id)
116117

117118
async def _request_with_optional_relogin(
118119
self, requests_timeout: Optional[int], path: str,
@@ -194,7 +195,8 @@ async def diary(
194195
)
195196
diary_schema = schemas.Diary()
196197
diary_schema.context['assignment_types'] = self._assignment_types
197-
return diary_schema.load(response.json())
198+
diary = diary_schema.load(response.json())
199+
return diary # type: ignore
198200

199201
async def overdue(
200202
self,
@@ -221,7 +223,7 @@ async def overdue(
221223
assignments_schema = schemas.Assignment()
222224
assignments_schema.context['assignment_types'] = self._assignment_types
223225
assignments = assignments_schema.load(response.json(), many=True)
224-
return assignments
226+
return assignments # type: ignore
225227

226228
async def announcements(
227229
self, take: Optional[int] = -1,
@@ -232,7 +234,7 @@ async def announcements(
232234
params={'take': take},
233235
)
234236
announcements = schemas.Announcement().load(response.json(), many=True)
235-
return announcements
237+
return announcements # type: ignore
236238

237239
async def attachments(
238240
self, assignment_id: int,
@@ -249,15 +251,15 @@ async def attachments(
249251
return []
250252
attachments_json = response[0]['attachments']
251253
attachments = schemas.Attachment().load(attachments_json, many=True)
252-
return attachments
254+
return attachments # type: ignore
253255

254256
async def school(self, requests_timeout: int = None) -> schemas.School:
255257
response = await self._request_with_optional_relogin(
256258
requests_timeout,
257259
'schools/{0}/card'.format(self._school_id),
258260
)
259261
school = schemas.School().load(response.json())
260-
return school
262+
return school # type: ignore
261263

262264
async def logout(self, requests_timeout: int = None):
263265
try:
@@ -282,20 +284,26 @@ async def full_logout(self, requests_timeout: int = None):
282284
await self.logout(requests_timeout)
283285
await self._wrapped_client.client.aclose()
284286

287+
async def schools(
288+
self, requests_timeout: int = None) -> List[schemas.ShortSchool]:
289+
resp = await self._wrapped_client.request(requests_timeout, "addresses/schools")
290+
schools = schemas.ShortSchool().load(resp.json(), many=True)
291+
return schools # type: ignore
292+
285293
async def _address(
286-
self, school: str, requester: Requester) -> Dict[str, int]:
287-
response = await requester('addresses/schools')
294+
self, school_name_or_id: Union[int, str],
295+
requester: Requester) -> Dict[str, int]:
296+
schools = (await requester("addresses/schools")).json()
288297

289-
schools_reference = response.json()
290-
for school_ in schools_reference:
291-
if school_['name'] == school or school_['id'] == school:
292-
self._school_id = school_['id']
298+
for school in schools:
299+
if school["name"] == school_name_or_id or school["id"] == school_name_or_id:
300+
self._school_id = school['id']
293301
return {
294-
'cid': school_['countryId'],
295-
'sid': school_['stateId'],
296-
'pid': school_['municipalityDistrictId'],
297-
'cn': school_['cityId'],
302+
'cid': school['countryId'],
303+
'sid': school['stateId'],
304+
'pid': school['municipalityDistrictId'],
305+
'cn': school['cityId'],
298306
'sft': 2,
299-
'scid': school_['id'],
307+
'scid': school['id'],
300308
}
301-
raise errors.SchoolNotFoundError(school)
309+
raise errors.SchoolNotFoundError(school_name_or_id)

netschoolapi/schemas.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,22 @@ class Diary(NetSchoolAPISchema):
8080
schedule = fields.List(fields.Nested(Day), data_key='weekDays')
8181

8282

83+
class ShortSchool(NetSchoolAPISchema):
84+
name = fields.String()
85+
id = fields.Integer()
86+
address = fields.String(data_key="addressString")
87+
88+
8389
class School(NetSchoolAPISchema):
8490
name = fields.String(data_key='fullSchoolName')
85-
about = fields.String(data_key='about')
91+
about = fields.String()
8692

87-
address = fields.String(data_key='address')
88-
email = fields.String(data_key='email')
93+
address = fields.String()
94+
email = fields.String()
8995
site = fields.String(data_key='web')
9096
phone = fields.String(data_key='phones')
9197

92-
director = fields.String(data_key='director')
98+
director = fields.String()
9399
AHC = fields.String(data_key='principalAHC')
94100
IT = fields.String(data_key='principalIT')
95101
UVR = fields.String(data_key='principalUVR')

0 commit comments

Comments
 (0)