-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstudents.py
More file actions
56 lines (38 loc) · 1.73 KB
/
students.py
File metadata and controls
56 lines (38 loc) · 1.73 KB
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
from typing import Optional
from pythonanywhere_core.base import call_api, get_api_endpoint, get_username
class StudentsAPI:
"""
Interface for the PythonAnywhere Students API.
This class uses the `get_api_endpoint` function from
``pythonanywhere.api.base`` to construct the API URL, which is stored
in the class variable ``base_url``. It then calls the ``call_api`` method
with the appropriate arguments to perform student-related actions.
Supported HTTP Methods:
- `GET`
- `DELETE`
Methods:
- :meth:`StudentsAPI.get`: Retrieve a list of students.
- :meth:`StudentsAPI.delete`: Remove a student.
"""
base_url: str = get_api_endpoint(username=get_username(), flavor="students")
def get(self) -> Optional[dict]:
"""Returns list of PythonAnywhere students related with user's account.
:returns: dictionary with students info
"""
result = call_api(self.base_url, "GET")
if result.status_code == 200:
return result.json()
raise Exception(f"GET to list students failed, got {result.text}")
def delete(self, student_username: str) -> Optional[int]:
"""Returns 204 if student has been successfully removed, raises otherwise.
:param student_username: student username to be removed
:returns: 204 if student has been successfully removed
"""
url = f"{self.base_url}{student_username}"
result = call_api(url, "DELETE")
if result.status_code == 204:
return result.status_code
detail = f": {result.text}" if result.text else ""
raise Exception(
f"DELETE to remove student {student_username!r} failed, got {result}{detail}"
)