Skip to content

Commit

Permalink
fix(exceptions): Update headers.get to check 'Retry-After' (#130)
Browse files Browse the repository at this point in the history
* bug: Upate retry-after header to match API

dictionary looks up are case sensitive. Since we return the header as Retry-After we need to have the .get match

* write simple test to verify

* remove support for 3.7 - add support for 3.11 & 3.12

* build release version to 3.11
  • Loading branch information
JacobAndrewSmith92 authored Mar 14, 2024
1 parent 5d342e6 commit 40f642b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ addons:
language: python

python:
- '3.7'
- '3.8'
- '3.9'
- '3.10'
- '3.11'
- '3.12'

if: tag IS blank # do not build tags

Expand All @@ -35,7 +36,7 @@ script:
jobs:
include:
- stage: publish
python: '3.9'
python: '3.11'
services: []
addons:
firefox: 'skip'
Expand Down
4 changes: 2 additions & 2 deletions smartcar/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def exception_factory(status_code: int, headers: dict, body: str):
# v2.0
elif response.get("type"):
# Set retry_after only for Vehicle Rate Limit errors
if headers.get("retry-after") is not None:
if headers.get("Retry-After") is not None:
return SmartcarException(
status_code=response.get("statusCode"),
request_id=response.get("requestId"),
Expand All @@ -87,7 +87,7 @@ def exception_factory(status_code: int, headers: dict, body: str):
doc_url=response.get("docURL"),
resolution=response.get("resolution"),
detail=response.get("detail"),
retry_after=headers.get("retry-after"),
retry_after=headers.get("Retry-After"),
)
else:
return SmartcarException(
Expand Down
12 changes: 6 additions & 6 deletions smartcar/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,9 @@ def batch(self, paths: List[str]) -> namedtuple:
"sc-request-id"
)
# use lambda default args to avoid issues with closures
batch_dict[
attribute
] = lambda p=path, r=res_dict: types.select_named_tuple(p, r)
batch_dict[attribute] = (
lambda p=path, r=res_dict: types.select_named_tuple(p, r)
)
else:
# if individual response is erroneous, attach a lambda that returns a SmartcarException
def _attribute_raise_exception(smartcar_exception):
Expand All @@ -455,9 +455,9 @@ def _attribute_raise_exception(smartcar_exception):
headers = response.headers
body = json.dumps(res_dict.get("body"))
sc_exception = sce.exception_factory(code, headers, body)
batch_dict[
attribute
] = lambda e=sc_exception: _attribute_raise_exception(e)
batch_dict[attribute] = (
lambda e=sc_exception: _attribute_raise_exception(e)
)

# STEP 3 - Attach Meta to batch_dict
batch_dict["meta"] = types.build_meta(response.headers)
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/test_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,18 @@ def test_json_cant_be_parsed():
assert e.message == "diggity"
assert e.status_code == 900
assert e.type == "SDK_ERROR"


def test_retry_after_found():
"""
test that we can get the retry_after amount
"""
try:
raise exception_factory(
429,
{"Retry-After": 5000, "Content-Type": "application/json"},
'{"statusCode":429,"type":"RATE_LIMIT","code":"Vehicle","resolution":{"type":"RETRY_LATER"},"requestId":"e0027f5f-4411-4247-a54d-e34c157d84c1"}',
)
except Exception as e:
assert isinstance(e, SmartcarException)
assert e.retry_after == 5000

0 comments on commit 40f642b

Please sign in to comment.