Skip to content

Commit eabeacb

Browse files
Adds method to list webapps.
1 parent 1772557 commit eabeacb

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

pythonanywhere_core/webapp.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@ class Webapp:
2121
2222
Methods:
2323
- :meth:`Webapp.create`: Create a new webapp.
24+
- :meth:`Webapp.create_static_file_mapping`: Create a static file mapping.
2425
- :meth:`Webapp.reload`: Reload the webapp.
2526
- :meth:`Webapp.set_ssl`: Set the SSL certificate and private key.
2627
- :meth:`Webapp.get_ssl_info`: Retrieve SSL certificate information.
2728
- :meth:`Webapp.delete_log`: Delete a log file.
2829
- :meth:`Webapp.get_log_info`: Retrieve log file information.
30+
31+
Class Methods:
32+
- :meth:`Webapp.list_webapps`: List all webapps for the current user.
2933
"""
34+
username = getpass.getuser()
35+
files_url = get_api_endpoint(username=username, flavor="files")
36+
webapps_url = get_api_endpoint(username=username, flavor="webapps")
37+
3038
def __init__(self, domain: str) -> None:
31-
self.username = getpass.getuser()
32-
self.files_url = get_api_endpoint(username=self.username, flavor="files")
33-
self.webapps_url = get_api_endpoint(username=self.username, flavor="webapps")
3439
self.domain = domain
3540
self.domain_url = f"{self.webapps_url}{self.domain}/"
3641

@@ -215,3 +220,14 @@ def get_log_info(self) -> dict:
215220
continue
216221
logs[log_type].append(log_index)
217222
return logs
223+
224+
@classmethod
225+
def list_webapps(cls) -> list:
226+
"""List all webapps for the current user.
227+
228+
:returns: list of webapps info as dictionaries
229+
"""
230+
response = call_api(cls.webapps_url, "get")
231+
if not response.ok:
232+
raise PythonAnywhereApiException(f"GET webapps via API failed, got {response}:{response.text}")
233+
return response.json()

tests/test_webapp.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,34 @@ def test_raises_if_get_does_not_20x(api_responses, api_token, base_file_url, web
447447

448448
assert "GET log files info via API failed" in str(e.value)
449449
assert "nope" in str(e.value)
450+
451+
452+
def test_list_webapps_returns_list(api_responses, api_token, base_url):
453+
# Simulate API response for listing webapps
454+
webapps_data = [
455+
{"id": 1, "domain_name": "www.domain1.com"},
456+
{"id": 2, "domain_name": "www.domain2.com"},
457+
]
458+
api_responses.add(
459+
responses.GET,
460+
base_url,
461+
status=200,
462+
body=json.dumps(webapps_data),
463+
)
464+
result = Webapp.list_webapps()
465+
assert isinstance(result, list)
466+
assert result == webapps_data
467+
468+
469+
def test_list_webapps_raises_on_error(api_responses, api_token, base_url):
470+
api_responses.add(
471+
responses.GET,
472+
base_url,
473+
status=500,
474+
body="server error",
475+
)
476+
with pytest.raises(PythonAnywhereApiException) as e:
477+
Webapp.list_webapps()
478+
assert "GET webapps via API failed" in str(e.value)
479+
assert "server error" in str(e.value)
480+

0 commit comments

Comments
 (0)