-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathresources.py
More file actions
30 lines (23 loc) · 1.14 KB
/
resources.py
File metadata and controls
30 lines (23 loc) · 1.14 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
from pythonanywhere_core.base import call_api, get_api_endpoint, get_username
from pythonanywhere_core.exceptions import PythonAnywhereApiException
class CPU:
"""Interface for PythonAnywhere CPU resources API.
Uses `pythonanywhere_core.base` :method: `get_api_endpoint` to
create url, which is stored in a class variable `CPU.base_url`,
then calls `call_api` with appropriate arguments to execute CPU
resource actions.
Methods:
- :meth:`CPU.get_cpu_usage`: Get current CPU usage information.
"""
def __init__(self):
self.base_url = get_api_endpoint(username=get_username(), flavor="cpu")
def get_cpu_usage(self):
"""Get current CPU usage information.
:returns: dictionary with CPU usage information including daily limit,
total usage, and next reset time
:raises PythonAnywhereApiException: if API call fails
"""
response = call_api(url=self.base_url, method="GET")
if not response.ok:
raise PythonAnywhereApiException(f"GET to {self.base_url} failed, got {response}:{response.text}")
return response.json()