Skip to content

Commit 95a0620

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

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

flareio/api_client.py

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

1717

18+
_API_DOMAIN_DEFAULT: str = "api.flare.io"
19+
_ALLOWED_API_DOMAINS: set[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: str | None = None,
2533
) -> None:
2634
if not api_key:
2735
raise Exception("API Key cannot be empty.")
36+
37+
api_domain = api_domain or _API_DOMAIN_DEFAULT
38+
if api_domain not in _ALLOWED_API_DOMAINS:
39+
raise Exception(
40+
f"Invalid API domain: {api_domain}. Only {_ALLOWED_API_DOMAINS} are supported."
41+
)
42+
self._api_domain: str = api_domain
43+
2844
self._api_key: str = api_key
2945
self._tenant_id: t.Optional[int] = tenant_id
3046

@@ -93,7 +109,7 @@ def generate_token(self) -> str:
93109
}
94110

95111
resp = self._session.post(
96-
"https://api.flare.io/tokens/generate",
112+
f"https://{self._api_domain}/tokens/generate",
97113
json=payload,
98114
headers={
99115
"Authorization": self._api_key,
@@ -128,12 +144,12 @@ def _request(
128144
json: t.Optional[t.Dict[str, t.Any]] = None,
129145
headers: t.Optional[t.Dict[str, t.Any]] = None,
130146
) -> requests.Response:
131-
url = urljoin("https://api.flare.io", url)
147+
url = urljoin(f"https://{self._api_domain}", url)
132148

133149
netloc: str = urlparse(url).netloc
134-
if not netloc == "api.flare.io":
150+
if not netloc == self._api_domain:
135151
raise Exception(
136-
f"Client was used to access {netloc=} at {url=}. Only the domain api.flare.io is supported."
152+
f"Client was used to access {netloc=} at {url=}. Only the domain {self._api_domain} is supported."
137153
)
138154

139155
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ 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

1820
if authenticated:
1921
with requests_mock.Mocker() as mocker:
2022
mocker.register_uri(
2123
"POST",
22-
"https://api.flare.io/tokens/generate",
24+
f"https://{client._api_domain}/tokens/generate",
2325
json={
2426
"token": "test-token-hello",
2527
},

0 commit comments

Comments
 (0)