|
15 | 15 | from flareio.version import __version__ as _flareio_version |
16 | 16 |
|
17 | 17 |
|
| 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 | + |
18 | 25 | class FlareApiClient: |
19 | 26 | def __init__( |
20 | 27 | self, |
21 | 28 | *, |
22 | 29 | api_key: str, |
23 | 30 | tenant_id: t.Optional[int] = None, |
24 | 31 | session: t.Optional[requests.Session] = None, |
| 32 | + api_domain: str | None = None, |
25 | 33 | ) -> None: |
26 | 34 | if not api_key: |
27 | 35 | 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 | + |
28 | 44 | self._api_key: str = api_key |
29 | 45 | self._tenant_id: t.Optional[int] = tenant_id |
30 | 46 |
|
@@ -93,7 +109,7 @@ def generate_token(self) -> str: |
93 | 109 | } |
94 | 110 |
|
95 | 111 | resp = self._session.post( |
96 | | - "https://api.flare.io/tokens/generate", |
| 112 | + f"https://{self._api_domain}/tokens/generate", |
97 | 113 | json=payload, |
98 | 114 | headers={ |
99 | 115 | "Authorization": self._api_key, |
@@ -128,12 +144,12 @@ def _request( |
128 | 144 | json: t.Optional[t.Dict[str, t.Any]] = None, |
129 | 145 | headers: t.Optional[t.Dict[str, t.Any]] = None, |
130 | 146 | ) -> requests.Response: |
131 | | - url = urljoin("https://api.flare.io", url) |
| 147 | + url = urljoin(f"https://{self._api_domain}", url) |
132 | 148 |
|
133 | 149 | netloc: str = urlparse(url).netloc |
134 | | - if not netloc == "api.flare.io": |
| 150 | + if not netloc == self._api_domain: |
135 | 151 | 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." |
137 | 153 | ) |
138 | 154 |
|
139 | 155 | headers = { |
|
0 commit comments