Skip to content

Commit 1ea6433

Browse files
committed
support eu api domain
1 parent 9048a04 commit 1ea6433

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

flareio/api_client.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,35 @@
1515
from flareio.version import __version__ as _flareio_version
1616

1717

18+
_API_DOMAIN_DEFAULT: str = "api.flare.io"
19+
_ALLOWED_API_DOMAINS: t.Tuple[str, ...] = (
20+
_API_DOMAIN_DEFAULT,
21+
"api.eu.flare.io",
22+
)
23+
24+
1825
class FlareApiClient:
1926
def __init__(
2027
self,
2128
*,
2229
api_key: str,
2330
tenant_id: t.Optional[int] = None,
2431
session: t.Optional[requests.Session] = None,
32+
api_domain: t.Optional[str] = None,
33+
_enable_beta_features: bool = False,
2534
) -> None:
2635
if not api_key:
2736
raise Exception("API Key cannot be empty.")
37+
38+
api_domain = api_domain or _API_DOMAIN_DEFAULT
39+
if api_domain not in _ALLOWED_API_DOMAINS:
40+
raise Exception(
41+
f"Invalid API domain: {api_domain}. Only {_ALLOWED_API_DOMAINS} are supported."
42+
)
43+
if api_domain != _API_DOMAIN_DEFAULT and not _enable_beta_features:
44+
raise Exception("Custom API domains considered a beta feature.")
45+
self._api_domain: str = api_domain
46+
2847
self._api_key: str = api_key
2948
self._tenant_id: t.Optional[int] = tenant_id
3049

@@ -93,7 +112,7 @@ def generate_token(self) -> str:
93112
}
94113

95114
resp = self._session.post(
96-
"https://api.flare.io/tokens/generate",
115+
f"https://{self._api_domain}/tokens/generate",
97116
json=payload,
98117
headers={
99118
"Authorization": self._api_key,
@@ -128,12 +147,12 @@ def _request(
128147
json: t.Optional[t.Dict[str, t.Any]] = None,
129148
headers: t.Optional[t.Dict[str, t.Any]] = None,
130149
) -> requests.Response:
131-
url = urljoin("https://api.flare.io", url)
150+
url = urljoin(f"https://{self._api_domain}", url)
132151

133152
netloc: str = urlparse(url).netloc
134-
if not netloc == "api.flare.io":
153+
if not netloc == self._api_domain:
135154
raise Exception(
136-
f"Client was used to access {netloc=} at {url=}. Only the domain api.flare.io is supported."
155+
f"Client was used to access {netloc=} at {url=}. Only the domain {self._api_domain} is supported."
137156
)
138157

139158
headers = {

tests/test_api_client_creation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ def test_create_client() -> None:
1414
FlareApiClient(api_key="test")
1515

1616

17+
def test_create_client_eu() -> None:
18+
FlareApiClient(
19+
api_key="test",
20+
api_domain="api.eu.flare.io",
21+
_enable_beta_features=True,
22+
)
23+
24+
25+
def test_create_client_bad_api_domain() -> None:
26+
with pytest.raises(Exception, match="Invalid API domain"):
27+
FlareApiClient(api_key="test", api_domain="bad.com")
28+
29+
1730
def test_create_client_empty_api_key() -> None:
1831
with pytest.raises(Exception, match="API Key cannot be empty."):
1932
FlareApiClient(

tests/test_api_client_endpoints.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ def test_get_path_only() -> None:
8282
assert mocker.last_request.url == "https://api.flare.io/hello/test"
8383

8484

85+
def test_get_eu_domain() -> None:
86+
client = get_test_client(
87+
api_domain="api.eu.flare.io",
88+
_enable_beta_features=True,
89+
)
90+
with requests_mock.Mocker() as mocker:
91+
mocker.register_uri(
92+
"GET",
93+
"https://api.eu.flare.io/hello/test",
94+
status_code=200,
95+
)
96+
client.get("/hello/test")
97+
assert mocker.last_request.url == "https://api.eu.flare.io/hello/test"
98+
99+
85100
def test_get_user_agent() -> None:
86101
client = get_test_client()
87102
with requests_mock.Mocker() as mocker:

tests/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ def get_test_client(
99
*,
1010
tenant_id: t.Optional[int] = None,
1111
authenticated: bool = True,
12+
api_domain: str | None = None,
13+
_enable_beta_features: bool = False,
1214
) -> FlareApiClient:
1315
client = FlareApiClient(
1416
api_key="test-api-key",
1517
tenant_id=tenant_id,
18+
api_domain=api_domain,
19+
_enable_beta_features=_enable_beta_features,
1620
)
1721

1822
if authenticated:
1923
with requests_mock.Mocker() as mocker:
2024
mocker.register_uri(
2125
"POST",
22-
"https://api.flare.io/tokens/generate",
26+
f"https://{client._api_domain}/tokens/generate",
2327
json={
2428
"token": "test-token-hello",
2529
},

0 commit comments

Comments
 (0)