Skip to content

Commit 521be6b

Browse files
adds websites list reload and delete.
Co-authored-by: Piotr Kaznowski <piotr@kazno.dev>
1 parent 5f67a08 commit 521be6b

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

pythonanywhere_core/schedule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Schedule:
1111
"""Interface for PythonAnywhere scheduled tasks API.
1212
13-
Uses `pythonanywhere.api` :method: `get_api_endpoint` to create url,
13+
Uses `pythonanywhere_core.api` :method: `get_api_endpoint` to create url,
1414
which is stored in a class variable `Schedule.base_url`, then calls
1515
`call_api` with appropriate arguments to execute scheduled tasks tasks
1616
actions. Covers 'GET' and 'POST' methods for tasks list, as well as

pythonanywhere_core/website.py

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
import getpass
2-
from urllib.parse import urljoin
32

43
from pythonanywhere_core.base import call_api, get_api_endpoint
54

65

76
class Website:
7+
""" Interface for PythonAnywhere websites API.
8+
9+
Uses ``pythonanywhere_core.base`` :method: ``get_api_endpoint`` to
10+
create url, which is stored in a class variable ``Website.api_endpoint``,
11+
then calls ``call_api`` with appropriate arguments to execute websites
12+
action.
13+
14+
Use :method: ``Website.create`` to create new website.
15+
Use :method: ``Website.get`` to get website info.
16+
Use :method: ``Website.list`` to get all websites list.
17+
Use :method: ``Website.reload`` to reload website.
18+
Use :method: ``Website.delete`` to delete website.
19+
"""
20+
821
api_endpoint = get_api_endpoint(username=getpass.getuser(), flavor="websites")
922

10-
def create(self, domain_name: str, command: str):
23+
def create(self, domain_name: str, command: str) -> dict:
24+
"""Creates new website with ``domain_name`` and ``command``.
25+
26+
:param domain_name: domain name for new website
27+
:param command: command for new website
28+
:returns: dictionary with created website info"""
29+
1130
response = call_api(
1231
self.api_endpoint,
1332
"post",
@@ -19,18 +38,45 @@ def create(self, domain_name: str, command: str):
1938
)
2039
return response.json()
2140

22-
def get(self, domain_name: str):
41+
def get(self, domain_name: str) -> dict:
42+
"""Returns dictionary with website info for ``domain_name``.
43+
:param domain_name:
44+
:return: dictionary with website info"""
45+
2346
response = call_api(
24-
urljoin(self.api_endpoint, domain_name),
47+
f"{self.api_endpoint}{domain_name}/",
2548
"get",
2649
)
2750
return response.json()
2851

29-
def list(self):
30-
raise NotImplemented
52+
def list(self) -> list:
53+
"""Returns list of dictionaries with all websites info.
54+
:return: list of dictionaries with websites info"""
3155

32-
def reload(self):
33-
raise NotImplemented
56+
response = call_api(
57+
self.api_endpoint,
58+
"get",
59+
)
60+
return response.json()
61+
62+
def reload(self, domain_name: str) -> dict:
63+
"""Reloads website with ``domain_name``.
64+
:param domain_name: domain name for website to reload
65+
:return: dictionary with response"""
3466

35-
def delete(self):
36-
raise NotImplemented
67+
response = call_api(
68+
f"{self.api_endpoint}{domain_name}/reload/",
69+
"post",
70+
)
71+
return response.json()
72+
73+
def delete(self, domain_name: str) -> dict:
74+
"""Deletes website with ``domain_name``.
75+
:param domain_name: domain name for website to delete
76+
:return: empty dictionary"""
77+
78+
call_api(
79+
f"{self.api_endpoint}{domain_name}/",
80+
"delete",
81+
)
82+
return {}

tests/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_get_api_endpoint_gets_domain_from_pythonanywhere_domain_and_adds_on_www
5050
("students", "v0"),
5151
("webapps", "v0"),
5252
("websites", "v1"),
53-
]
53+
]
5454
)
5555
def test_get_api_endpoint_returns_url_with_correct_api_version(flavor, expected_version):
5656
result = get_api_endpoint(username="bill", flavor=flavor)

tests/test_website.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,47 @@ def test_create_returns_json_with_created_website_info(
6464
"POST to create needs the payload to be passed as json field"
6565
)
6666

67+
6768
def test_get_returns_json_with_info_for_given_domain(
6869
api_responses, webapps_base_url, website_info, domain_name
6970
):
7071
api_responses.add(
7172
responses.GET,
72-
url=urljoin(webapps_base_url, domain_name),
73+
url=f"{webapps_base_url}{domain_name}/",
7374
status=200,
7475
body=json.dumps(website_info)
7576
)
7677

7778
assert Website().get(domain_name=domain_name) == website_info
79+
80+
81+
def test_list_returns_json_with_info_for_all_websites(api_responses, webapps_base_url, website_info):
82+
api_responses.add(
83+
responses.GET,
84+
url=webapps_base_url,
85+
status=200,
86+
body=json.dumps([website_info])
87+
)
88+
89+
assert Website().list() == [website_info]
90+
91+
92+
def test_reloads_website(api_responses, domain_name, webapps_base_url):
93+
api_responses.add(
94+
responses.POST,
95+
url=f"{webapps_base_url}{domain_name}/reload",
96+
status=200,
97+
body=json.dumps({"status": "OK"})
98+
)
99+
100+
assert Website().reload(domain_name=domain_name) == {"status": "OK"}
101+
102+
103+
def test_deletes_website(api_responses, domain_name, webapps_base_url):
104+
api_responses.add(
105+
responses.DELETE,
106+
url=f"{webapps_base_url}{domain_name}/",
107+
status=204,
108+
)
109+
110+
assert Website().delete(domain_name=domain_name) == {}

0 commit comments

Comments
 (0)