Skip to content

Commit 386e8a1

Browse files
committed
Changed interface of base.get_api_endpoint so it receives username and flavor, and returns interpolated string, by Filip, Piotr
1 parent 5568a00 commit 386e8a1

File tree

10 files changed

+32
-23
lines changed

10 files changed

+32
-23
lines changed

pythonanywhere_core/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
}
1616

1717

18-
def get_api_endpoint() -> str:
18+
def get_api_endpoint(username: str, flavor: str) -> str:
1919
hostname = os.environ.get(
2020
"PYTHONANYWHERE_SITE",
2121
"www." + os.environ.get("PYTHONANYWHERE_DOMAIN", "pythonanywhere.com"),
2222
)
23-
return f"https://{hostname}/api/v0/user/{{username}}/{{flavor}}/"
23+
return f"https://{hostname}/api/v0/user/{username}/{flavor}/"
2424

2525

2626
def helpful_token_error_message() -> str:

pythonanywhere_core/files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Files:
4141
subdirectories of a directory at `path` (limited to 1000 results)
4242
"""
4343

44-
base_url = get_api_endpoint().format(username=getpass.getuser(), flavor="files")
44+
base_url = get_api_endpoint(username=getpass.getuser(), flavor="files")
4545
path_endpoint = urljoin(base_url, "path")
4646
sharing_endpoint = urljoin(base_url, "sharing/")
4747
tree_endpoint = urljoin(base_url, "tree/")
@@ -176,4 +176,4 @@ def tree_get(self, path: str) -> dict:
176176
if result.ok:
177177
return result.json()
178178

179-
raise PythonAnywhereApiException(f"GET to {url} failed, got {result}{self._error_msg(result)}")
179+
raise PythonAnywhereApiException(f"GET to {url} failed, got {result}{self._error_msg(result)}")

pythonanywhere_core/schedule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Schedule:
2222
Use :method: `Schedule.delete` to delete existing task.
2323
Use :method: `Schedule.update` to update existing task."""
2424

25-
base_url: str = get_api_endpoint().format(username=getpass.getuser(), flavor="schedule")
25+
base_url: str = get_api_endpoint(username=getpass.getuser(), flavor="schedule")
2626

2727
def create(self, params: dict) -> Optional[dict]:
2828
"""Creates new scheduled task using `params`.

pythonanywhere_core/students.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class StudentsAPI:
2121
- use :method: `StudentsAPI.delete` to remove a student
2222
"""
2323

24-
base_url: str = get_api_endpoint().format(username=getpass.getuser(), flavor="students")
24+
base_url: str = get_api_endpoint(username=getpass.getuser(), flavor="students")
2525

2626
def get(self) -> Optional[dict]:
2727
"""Returns list of PythonAnywhere students related with user's account."""

pythonanywhere_core/webapp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ class Webapp:
2828
2929
"""
3030
def __init__(self, domain: str) -> None:
31-
self.endpoint = get_api_endpoint()
3231
self.username = getpass.getuser()
33-
self.files_url = self.endpoint.format(username=self.username, flavor="files")
34-
self.webapps_url = self.endpoint.format(username=self.username, flavor="webapps")
32+
self.files_url = get_api_endpoint(username=self.username, flavor="files")
33+
self.webapps_url = get_api_endpoint(username=self.username, flavor="webapps")
3534
self.domain = domain
3635
self.domain_url = f"{self.webapps_url}{self.domain}/"
3736

tests/test_base.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,31 @@ def mock_requests(mocker):
1414
return mocker.patch("pythonanywhere_core.base.requests")
1515

1616

17-
def test_api_endpoint_defaults_to_pythonanywhere_dot_com_if_no_environment_variables():
18-
assert get_api_endpoint() == "https://www.pythonanywhere.com/api/v0/user/{username}/{flavor}/"
17+
def test_get_api_endpoint_defaults_to_pythonanywhere_dot_com_if_no_environment_variables():
18+
result = get_api_endpoint(username="bill", flavor="webapp")
1919

20+
assert result == "https://www.pythonanywhere.com/api/v0/user/bill/webapp/"
2021

21-
def test_gets_domain_from_pythonanywhere_site_and_ignores_pythonanywhere_domain_if_both_set(monkeypatch):
22+
23+
def test_get_api_endpoint_gets_domain_from_pythonanywhere_site_and_ignores_pythonanywhere_domain_if_both_set(
24+
monkeypatch
25+
):
2226
monkeypatch.setenv("PYTHONANYWHERE_SITE", "www.foo.com")
2327
monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "wibble.com")
2428

25-
assert get_api_endpoint() == "https://www.foo.com/api/v0/user/{username}/{flavor}/"
29+
result = get_api_endpoint(username="bill", flavor="webapp")
30+
31+
assert result == "https://www.foo.com/api/v0/user/bill/webapp/"
2632

2733

28-
def test_gets_domain_from_pythonanywhere_domain_and_adds_on_www_if_set_but_no_pythonanywhere_site(monkeypatch):
34+
def test_get_api_endpoint_gets_domain_from_pythonanywhere_domain_and_adds_on_www_if_set_but_pythonanywhere_site_is_not(
35+
monkeypatch
36+
):
2937
monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "foo.com")
3038

31-
assert get_api_endpoint() == "https://www.foo.com/api/v0/user/{username}/{flavor}/"
39+
result = get_api_endpoint(username="bill", flavor="webapp")
40+
41+
assert result == "https://www.foo.com/api/v0/user/bill/webapp/"
3242

3343

3444
def test_raises_on_401(api_token, api_responses):

tests/test_files.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def username():
1717

1818
@pytest.fixture()
1919
def base_url(username):
20-
return get_api_endpoint().format(username=username, flavor="files")
20+
return get_api_endpoint(username=username, flavor="files")
2121

2222

2323
@pytest.fixture()
@@ -295,7 +295,7 @@ def test_sharing_post_raises_exception_when_path_not_provided(
295295
"provided path is not valid" # or similar
296296
)
297297
assert str(e.value) == expected_error_msg
298-
298+
299299

300300
def test_sharing_get_returns_sharing_url_when_path_is_shared(
301301
api_token, api_responses, base_url, home_dir_path, username
@@ -325,7 +325,7 @@ def test_sharing_get_returns_empty_string_when_path_not_shared(
325325
api_responses.add(method=responses.GET, url=url, status=404)
326326

327327
assert Files().sharing_get(valid_path) == ""
328-
328+
329329

330330
def test_returns_204_on_sucessful_unshare(
331331
api_token, api_responses, base_url, home_dir_path, username
@@ -336,7 +336,7 @@ def test_returns_204_on_sucessful_unshare(
336336
api_responses.add(method=responses.DELETE, url=url, status=204)
337337

338338
assert Files().sharing_delete(valid_path) == 204
339-
339+
340340

341341
def test_returns_list_of_the_regular_files_and_subdirectories_of_a_directory(
342342
api_token, api_responses, base_url, default_home_dir_files, home_dir_path

tests/test_schedule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.fixture
1313
def task_base_url():
14-
return get_api_endpoint().format(username=getpass.getuser(), flavor="schedule")
14+
return get_api_endpoint(username=getpass.getuser(), flavor="schedule")
1515

1616

1717
@pytest.fixture

tests/test_students.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@pytest.fixture
1212
def students_base_url():
13-
return get_api_endpoint().format(username=getpass.getuser(), flavor="students")
13+
return get_api_endpoint(username=getpass.getuser(), flavor="students")
1414

1515

1616
class TestStudentsAPIGet:

tests/test_webapp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
@pytest.fixture
1616
def base_url():
17-
return get_api_endpoint().format(username=getpass.getuser(), flavor="webapps")
17+
return get_api_endpoint(username=getpass.getuser(), flavor="webapps")
1818

1919

2020
@pytest.fixture
@@ -24,7 +24,7 @@ def domain():
2424

2525
@pytest.fixture()
2626
def base_file_url():
27-
return get_api_endpoint().format(username=getpass.getuser(), flavor='files')
27+
return get_api_endpoint(username=getpass.getuser(), flavor='files')
2828

2929

3030
@pytest.fixture

0 commit comments

Comments
 (0)