Skip to content

Commit 62526b4

Browse files
committed
Take the hostname for the PythonAnywhere site from $PYTHONANYWHERE_SITE if its available -- otherwise fail back to www.$PYTHONANYWHERE_DOMAIN and then to www.pythonanywhere.com. by: Giles
1 parent cb0896c commit 62526b4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pythonanywhere/api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ class NoTokenError(Exception):
2121

2222

2323
def get_api_endpoint():
24-
domain = os.environ.get("PYTHONANYWHERE_DOMAIN", "pythonanywhere.com")
25-
return "https://www.{domain}/api/v0/user/{{username}}/{{flavor}}/".format(domain=domain)
24+
hostname = os.environ.get(
25+
"PYTHONANYWHERE_SITE",
26+
"www." + os.environ.get(
27+
"PYTHONANYWHERE_DOMAIN",
28+
"pythonanywhere.com"
29+
)
30+
)
31+
return "https://{hostname}/api/v0/user/{{username}}/{{flavor}}/".format(hostname=hostname)
2632

2733

2834
def call_api(url, method, **kwargs):

tests/test_api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@
1111

1212

1313
class TestGetAPIEndpoint:
14-
def test_gets_domain_from_env_if_set(self, monkeypatch):
14+
15+
def test_defaults_to_pythonanywhere_dot_com_if_no_environment_variables(self):
1516
assert get_api_endpoint() == "https://www.pythonanywhere.com/api/v0/user/{username}/{flavor}/"
17+
18+
def test_gets_domain_from_pythonanywhere_site_and_ignores_pythonanywhere_domain_if_both_set(self, monkeypatch):
19+
monkeypatch.setenv("PYTHONANYWHERE_SITE", "www.foo.com")
20+
monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "wibble.com")
21+
assert get_api_endpoint() == "https://www.foo.com/api/v0/user/{username}/{flavor}/"
22+
23+
def test_gets_domain_from_pythonanywhere_domain_and_adds_on_www_if_set_but_no_pythonanywhere_site(self, monkeypatch):
1624
monkeypatch.setenv("PYTHONANYWHERE_DOMAIN", "foo.com")
1725
assert get_api_endpoint() == "https://www.foo.com/api/v0/user/{username}/{flavor}/"
1826

0 commit comments

Comments
 (0)