Skip to content

Commit

Permalink
[v2.0.4] Extended exceptions for Cloudflare error codes (#33)
Browse files Browse the repository at this point in the history
* bumped version

* type hinting for exception mapping

* retry for cloudflare error codes

* circleci config update for pytest
  • Loading branch information
fatihkurtoglu authored Apr 16, 2021
1 parent c037ca1 commit 09cbad5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- run:
name: Pytest Test Cases
command: | # Run test suite, uses SCALE_TEST_API_KEY env variable
pytest -v
pytest -v -s
- run:
name: Twine PyPI Check
command: | # Validate distribution and setup.py configuration
Expand Down
2 changes: 1 addition & 1 deletion scaleapi/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "2.0.3"
__version__ = "2.0.4"
__package_name__ = "scaleapi"
21 changes: 15 additions & 6 deletions scaleapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@
# Parameters for HTTP retry
HTTP_TOTAL_RETRIES = 3 # Number of total retries
HTTP_RETRY_BACKOFF_FACTOR = 2 # Wait 1, 2, 4 seconds between retries
HTTP_STATUS_FORCE_LIST = [429, 500, 503, 504] # Status codes to force retry
HTTP_STATUS_FORCE_LIST = [
429,
500,
502,
503,
504,
520,
521,
522,
523,
524,
525,
] # Status codes to force retry
HTTP_RETRY_ALLOWED_METHODS = frozenset({"GET", "POST"})


Expand Down Expand Up @@ -73,11 +85,8 @@ def _raise_on_respose(res: Response):
except ValueError:
message = res.text

try:
exception = ExceptionMap[res.status_code]
raise exception(message)
except KeyError as err:
raise ScaleException(message, res.status_code) from err
exception = ExceptionMap.get(res.status_code, ScaleException)
raise exception(message, res.status_code)

def _api_request(
self, method, endpoint, headers=None, auth=None, params=None, body=None
Expand Down
5 changes: 4 additions & 1 deletion scaleapi/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Dict


class ScaleException(Exception):
"""Generic ScaleException class"""

Expand Down Expand Up @@ -81,7 +84,7 @@ class ScaleTimeoutError(ScaleException):
code = 504


ExceptionMap = {
ExceptionMap: Dict[int, ScaleException] = {
ScaleInvalidRequest.code: ScaleInvalidRequest,
ScaleUnauthorized.code: ScaleUnauthorized,
ScaleNotEnabled.code: ScaleNotEnabled,
Expand Down

0 comments on commit 09cbad5

Please sign in to comment.