|
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: t.Tuple[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: t.Optional[str] = None, |
| 33 | + _enable_beta_features: bool = False, |
25 | 34 | ) -> None: |
26 | 35 | if not api_key: |
27 | 36 | 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 | + |
28 | 47 | self._api_key: str = api_key |
29 | 48 | self._tenant_id: t.Optional[int] = tenant_id |
30 | 49 |
|
@@ -93,7 +112,7 @@ def generate_token(self) -> str: |
93 | 112 | } |
94 | 113 |
|
95 | 114 | resp = self._session.post( |
96 | | - "https://api.flare.io/tokens/generate", |
| 115 | + f"https://{self._api_domain}/tokens/generate", |
97 | 116 | json=payload, |
98 | 117 | headers={ |
99 | 118 | "Authorization": self._api_key, |
@@ -128,12 +147,12 @@ def _request( |
128 | 147 | json: t.Optional[t.Dict[str, t.Any]] = None, |
129 | 148 | headers: t.Optional[t.Dict[str, t.Any]] = None, |
130 | 149 | ) -> requests.Response: |
131 | | - url = urljoin("https://api.flare.io", url) |
| 150 | + url = urljoin(f"https://{self._api_domain}", url) |
132 | 151 |
|
133 | 152 | netloc: str = urlparse(url).netloc |
134 | | - if not netloc == "api.flare.io": |
| 153 | + if not netloc == self._api_domain: |
135 | 154 | 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." |
137 | 156 | ) |
138 | 157 |
|
139 | 158 | headers = { |
|
0 commit comments