Skip to content

Commit 6238d2d

Browse files
authored
"Fix" download errors to show a nicer exception. (#4)
1 parent 6d1a426 commit 6238d2d

File tree

7 files changed

+227
-165
lines changed

7 files changed

+227
-165
lines changed

poetry.lock

Lines changed: 173 additions & 159 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/smartschool_report_on_results

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
from smartschool import PathCredentials, Results, Smartschool, logger
3+
from smartschool import PathCredentials, Results, Smartschool, logger, DownloadError
44
from smartschool.common import IsSaved, capture_and_email_all_exceptions, save, send_email
55
from smartschool.objects import Result
66

@@ -143,8 +143,11 @@ def process_result(result: Result) -> None:
143143

144144
@capture_and_email_all_exceptions(email_from=session.creds.other_info['email_from'], email_to=session.creds.other_info['email_to'])
145145
def main():
146-
for result in Results():
147-
process_result(result)
146+
try:
147+
for result in Results():
148+
process_result(result)
149+
except DownloadError:
150+
...
148151

149152

150153
if __name__ == '__main__':

src/smartschool/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .agenda import SmartschoolHours, SmartschoolLessons, SmartschoolMomentInfos
44
from .courses import Courses, TopNavCourses
55
from .credentials import EnvCredentials, PathCredentials
6+
from .exceptions import DownloadError, SmartSchoolException
67
from .logger import setup_logger
78
from .messages import (
89
AdjustMessageLabel,
@@ -49,6 +50,9 @@
4950
"MessageMoveToTrash",
5051
"MessageLabel",
5152
"ResultDetail",
53+
# Exceptions
54+
"SmartSchoolException",
55+
"DownloadError",
5256
]
5357

5458
logger = setup_logger(logging.DEBUG)

src/smartschool/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class SmartSchoolException(Exception): ...
2+
3+
4+
class DownloadError(SmartSchoolException): ...

src/smartschool/results.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from itertools import count
22
from typing import Iterator
33

4+
from .exceptions import DownloadError
45
from .objects import Result, ResultWithDetails
56
from .session import session
67

@@ -26,7 +27,11 @@ class Results:
2627

2728
def __iter__(self) -> Iterator[Result]:
2829
for page_nr in count(start=1): # pragma: no branch
29-
json = session.json(f"/results/api/v1/evaluations/?pageNumber={page_nr}&itemsOnPage={RESULTS_PER_PAGE}")
30+
downloaded_webpage = session.get(f"/results/api/v1/evaluations/?pageNumber={page_nr}&itemsOnPage={RESULTS_PER_PAGE}")
31+
if not downloaded_webpage or not downloaded_webpage.content:
32+
raise DownloadError("No JSON was returned for the results?!")
33+
34+
json = downloaded_webpage.json()
3035
for result in json:
3136
yield Result(**result)
3237

@@ -39,5 +44,9 @@ def __init__(self, result_id: str):
3944
self.result_id = result_id
4045

4146
def get(self) -> ResultWithDetails:
42-
json = session.json(f"/results/api/v1/evaluations/{self.result_id}")
47+
downloaded_webpage = session.get(f"/results/api/v1/evaluations/{self.result_id}")
48+
if not downloaded_webpage or not downloaded_webpage.content:
49+
raise DownloadError("No JSON was returned for the details?!")
50+
51+
json = downloaded_webpage.json()
4352
return ResultWithDetails(**json)

tests/requests/get/results/api/v1/evaluations/empty_return_value.json

Whitespace-only changes.

tests/results_tests.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from smartschool import ResultDetail, Results
1+
import pytest
2+
from requests_mock import ANY
3+
from smartschool import DownloadError, ResultDetail, Results
24

35

46
def test_results_normal_flow(mocker):
@@ -19,3 +21,29 @@ def test_result_detail_normal_flow():
1921
assert sut.name == "Repetitie hoofdstuk 1"
2022
assert sut.graphic.achieved_points == 13.5
2123
assert sut.details.teachers[0].name.startingWithFirstName == "Gert Segers"
24+
25+
26+
def test_result_detail_with_empty_return_value():
27+
with pytest.raises(DownloadError):
28+
ResultDetail(result_id="empty_return_value").get()
29+
30+
31+
def test_result_detail_with_faulty_session_get(requests_mock):
32+
requests_mock.register_uri(ANY, ANY, status_code=404)
33+
34+
with pytest.raises(DownloadError):
35+
ResultDetail(result_id="abc_normal_123").get()
36+
37+
38+
def test_result_with_empty_return_value(requests_mock):
39+
requests_mock.register_uri(ANY, ANY, text="")
40+
41+
with pytest.raises(DownloadError):
42+
next(iter(Results()))
43+
44+
45+
def test_result_with_faulty_session_get(requests_mock):
46+
requests_mock.register_uri(ANY, ANY, status_code=404)
47+
48+
with pytest.raises(DownloadError):
49+
next(iter(Results()))

0 commit comments

Comments
 (0)