Skip to content

Commit eec0597

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

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

flareio/api_client.py

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

1717

18+
ALLOWED_API_DOMAINS: set[str] = (
19+
"api.flare.io",
20+
"api.eu.flare.io",
21+
)
22+
23+
1824
class FlareApiClient:
1925
def __init__(
2026
self,
2127
*,
2228
api_key: str,
2329
tenant_id: t.Optional[int] = None,
2430
session: t.Optional[requests.Session] = None,
31+
api_domain: str | None = None,
2532
) -> None:
2633
if not api_key:
2734
raise Exception("API Key cannot be empty.")
35+
36+
api_domain = api_domain or "api.flare.io"
37+
if api_domain not in ALLOWED_API_DOMAINS:
38+
raise Exception(
39+
f"Invalid API domain: {api_domain}. Only {ALLOWED_API_DOMAINS} are supported."
40+
)
41+
self.api_domain: str = api_domain
42+
2843
self._api_key: str = api_key
2944
self._tenant_id: t.Optional[int] = tenant_id
3045

@@ -93,7 +108,7 @@ def generate_token(self) -> str:
93108
}
94109

95110
resp = self._session.post(
96-
"https://api.flare.io/tokens/generate",
111+
f"https://{self.api_domain}/tokens/generate",
97112
json=payload,
98113
headers={
99114
"Authorization": self._api_key,
@@ -128,12 +143,12 @@ def _request(
128143
json: t.Optional[t.Dict[str, t.Any]] = None,
129144
headers: t.Optional[t.Dict[str, t.Any]] = None,
130145
) -> requests.Response:
131-
url = urljoin("https://api.flare.io", url)
146+
url = urljoin(f"https://{self.api_domain}", url)
132147

133148
netloc: str = urlparse(url).netloc
134-
if not netloc == "api.flare.io":
149+
if not netloc == self.api_domain:
135150
raise Exception(
136-
f"Client was used to access {netloc=} at {url=}. Only the domain api.flare.io is supported."
151+
f"Client was used to access {netloc=} at {url=}. Only the domain {self.api_domain} is supported."
137152
)
138153

139154
headers = {

tests/test_api_client_creation.py

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

1616

17+
def test_create_client_eu() -> None:
18+
FlareApiClient(api_key="test", api_domain="api.eu.flare.io")
19+
20+
21+
def test_create_client_bad_api_domain() -> None:
22+
with pytest.raises(Exception, match="Invalid API domain"):
23+
FlareApiClient(api_key="test", api_domain="bad.com")
24+
25+
1726
def test_create_client_empty_api_key() -> None:
1827
with pytest.raises(Exception, match="API Key cannot be empty."):
1928
FlareApiClient(

tests/test_api_client_endpoints.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ 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+
)
89+
with requests_mock.Mocker() as mocker:
90+
mocker.register_uri(
91+
"GET",
92+
"https://api.eu.flare.io/hello/test",
93+
status_code=200,
94+
)
95+
client.get("/hello/test")
96+
assert mocker.last_request.url == "https://api.eu.flare.io/hello/test"
97+
98+
8599
def test_get_user_agent() -> None:
86100
client = get_test_client()
87101
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,
1213
) -> FlareApiClient:
1314
client = FlareApiClient(
1415
api_key="test-api-key",
1516
tenant_id=tenant_id,
17+
api_domain=api_domain,
1618
)
1719

20+
mocked_api_domain = api_domain or "api.flare.io"
21+
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://{mocked_api_domain}/tokens/generate",
2327
json={
2428
"token": "test-token-hello",
2529
},

0 commit comments

Comments
 (0)