|
15 | 15 | class PosthogAPIClientError(Exception):
|
16 | 16 | pass
|
17 | 17 |
|
| 18 | +class RateLimitError(Exception): |
| 19 | + pass |
| 20 | + |
18 | 21 | class PosthogAPIClient:
|
19 | 22 | def __init__(self, api_base=None, api_key=None, project_id=None):
|
20 | 23 | if api_base is None:
|
@@ -185,28 +188,39 @@ def _get_headers(self):
|
185 | 188 | def _check_status_ok(self, code):
|
186 | 189 | return code == 200 or code == 201
|
187 | 190 |
|
| 191 | + def _check_status_too_many_requests(self, code): |
| 192 | + return code == 429 |
| 193 | + |
188 | 194 | def _map_single_response(self, method, path, response):
|
189 | 195 | ret = None
|
190 | 196 | if self._check_status_ok(response.status_code):
|
191 |
| - data = response.json() |
192 |
| - self.logger.info("request successful", method=method, path=path, status_code=response.status_code, response=data) |
193 |
| - ret = self._map_single_response_success(data) |
| 197 | + data = response.json() |
| 198 | + self.logger.info("request successful", method=method, path=path, status_code=response.status_code, response=data) |
| 199 | + ret = self._map_single_response_success(data) |
| 200 | + elif self._check_status_too_many_requests(response.status_code): |
| 201 | + data = response.json() |
| 202 | + self.logger.info("request failed", method=method, path=path, status_code=response.status_code, response=data) |
| 203 | + raise RateLimitError(f"{data['detail']}") |
194 | 204 | else:
|
195 |
| - data = response.json() |
196 |
| - self.logger.info("request failed", method=method, path=path, status_code=response.status_code, response=data) |
197 |
| - ret = self._map_error_response(response.status_code, data) |
| 205 | + data = response.json() |
| 206 | + self.logger.info("request failed", method=method, path=path, status_code=response.status_code, response=data) |
| 207 | + ret = self._map_error_response(response.status_code, data) |
198 | 208 | return ret
|
199 | 209 |
|
200 | 210 | def _map_list_response(self, method, path, response):
|
201 | 211 | ret = None
|
202 | 212 | if self._check_status_ok(response.status_code):
|
203 |
| - data = response.json() |
204 |
| - self.logger.info("request successful", method=method, path=path, status_code=response.status_code, response=data) |
205 |
| - ret = self._map_list_response_success(data) |
| 213 | + data = response.json() |
| 214 | + self.logger.info("request successful", method=method, path=path, status_code=response.status_code, response=data) |
| 215 | + ret = self._map_list_response_success(data) |
| 216 | + elif self._check_status_too_many_requests(response.status_code): |
| 217 | + data = response.json() |
| 218 | + self.logger.info("request failed", method=method, path=path, status_code=response.status_code, response=data) |
| 219 | + raise RateLimitError(f"{data['detail']}") |
206 | 220 | else:
|
207 |
| - data = response.json() |
208 |
| - self.logger.info("request failed", method=method, path=path, status_code=response.status_code, response=data) |
209 |
| - ret = self._map_error_response(response.status_code, data) |
| 221 | + data = response.json() |
| 222 | + self.logger.info("request failed", method=method, path=path, status_code=response.status_code, response=data) |
| 223 | + ret = self._map_error_response(response.status_code, data) |
210 | 224 | return ret
|
211 | 225 |
|
212 | 226 | def _map_error_response(self, code, data):
|
@@ -238,3 +252,7 @@ def _map_list_response_success(self, data):
|
238 | 252 | def _log_posthog_connection_error(self, error):
|
239 | 253 | self.logger.error(f"Posthog connection error - {error}")
|
240 | 254 | raise PosthogAPIClientError(f"Posthog connection error - {error}")
|
| 255 | + |
| 256 | + def _log_posthog_rate_limit_error(self, error): |
| 257 | + self.logger.error(f"Posthog rate limit error - {error}") |
| 258 | + raise RateLimitError(f"Posthog rate limit error error - {error}") |
0 commit comments