Skip to content

Commit 5fc4880

Browse files
refactor(ofrep): use elif chain and _HTTP_AUTH_ERRORS dict in _raise_for_http_status
1 parent 51cb2be commit 5fc4880

File tree

1 file changed

+26
-35
lines changed
  • providers/openfeature-provider-ofrep/src/openfeature/contrib/provider/ofrep

1 file changed

+26
-35
lines changed

providers/openfeature-provider-ofrep/src/openfeature/contrib/provider/ofrep/__init__.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
],
4343
]
4444

45+
_HTTP_AUTH_ERRORS: dict[int, str] = {401: "unauthorized", 403: "forbidden"}
46+
4547

4648
class OFREPProvider(AbstractProvider):
4749
def __init__(
@@ -183,57 +185,46 @@ def _handle_error(self, exception: requests.RequestException) -> NoReturn:
183185
except JSONDecodeError:
184186
raise ParseError(str(exception)) from exception
185187

186-
self._raise_for_error_code(data, exception)
188+
error_code = ErrorCode(data["errorCode"])
189+
error_details = data["errorDetails"]
190+
191+
if error_code == ErrorCode.PARSE_ERROR:
192+
raise ParseError(error_details) from exception
193+
if error_code == ErrorCode.TARGETING_KEY_MISSING:
194+
raise TargetingKeyMissingError(error_details) from exception
195+
if error_code == ErrorCode.INVALID_CONTEXT:
196+
raise InvalidContextError(error_details) from exception
197+
if error_code == ErrorCode.GENERAL:
198+
raise GeneralError(error_details) from exception
199+
200+
raise OpenFeatureError(error_code, error_details) from exception
187201

188202
def _raise_for_http_status(
189203
self,
190204
response: requests.Response,
191205
exception: requests.RequestException,
192206
) -> None:
193-
if response.status_code == 429:
207+
status = response.status_code
208+
209+
if status == 429:
194210
retry_after = response.headers.get("Retry-After")
195211
self.retry_after = _parse_retry_after(retry_after)
196212
raise GeneralError(
197213
f"Rate limited, retry after: {retry_after}"
198214
) from exception
199-
200-
if response.status_code == 401:
201-
raise OpenFeatureError(ErrorCode.GENERAL, "unauthorized") from exception
202-
203-
if response.status_code == 403:
204-
raise OpenFeatureError(ErrorCode.GENERAL, "forbidden") from exception
205-
206-
if response.status_code == 404:
215+
elif status in _HTTP_AUTH_ERRORS:
216+
raise OpenFeatureError(
217+
ErrorCode.GENERAL, _HTTP_AUTH_ERRORS[status]
218+
) from exception
219+
elif status == 404:
207220
try:
208-
data = response.json()
209-
error_details = data["errorDetails"]
210-
except JSONDecodeError:
221+
error_details = response.json()["errorDetails"]
222+
except (JSONDecodeError, KeyError):
211223
error_details = response.text
212224
raise FlagNotFoundError(error_details) from exception
213-
214-
if response.status_code > 400:
225+
elif status > 400:
215226
raise OpenFeatureError(ErrorCode.GENERAL, response.text) from exception
216227

217-
def _raise_for_error_code(
218-
self,
219-
data: dict[str, Any],
220-
exception: requests.RequestException,
221-
) -> NoReturn:
222-
error_code = ErrorCode(data["errorCode"])
223-
error_details = data["errorDetails"]
224-
225-
if error_code == ErrorCode.PARSE_ERROR:
226-
raise ParseError(error_details) from exception
227-
if error_code == ErrorCode.TARGETING_KEY_MISSING:
228-
raise TargetingKeyMissingError(error_details) from exception
229-
if error_code == ErrorCode.INVALID_CONTEXT:
230-
raise InvalidContextError(error_details) from exception
231-
if error_code == ErrorCode.GENERAL:
232-
raise GeneralError(error_details) from exception
233-
234-
raise OpenFeatureError(error_code, error_details) from exception
235-
236-
237228
def _build_request_data(
238229
evaluation_context: Optional[EvaluationContext],
239230
) -> dict[str, Any]:

0 commit comments

Comments
 (0)