Skip to content

Commit

Permalink
Update quota exceeded recognition (#545)
Browse files Browse the repository at this point in the history
Co-authored-by: rikroe <rikroe@users.noreply.github.com>
  • Loading branch information
rikroe and rikroe authored Jul 1, 2023
1 parent 770b1fa commit 48d4c14
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bimmer_connected/api/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def async_auth_flow(self, request: httpx.Request) -> AsyncGenerator[httpx.
request.headers["bmw-session-id"] = self.session_id
yield request
# Quota errors can either be 429 Too Many Requests or 403 Quota Exceeded (instead of 403 Forbidden)
elif response.status_code == 429 or (response.status_code == 403 and "quota exceeded" in response.text.lower()):
elif response.status_code == 429 or (response.status_code == 403 and "quota" in response.text.lower()):
for _ in range(3):
if response.status_code == 429:
await response.aread()
Expand Down
4 changes: 1 addition & 3 deletions bimmer_connected/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ async def handle_httpstatuserror(
_ex_to_raise = MyBMWAuthError

# Quota errors can either be 429 Too Many Requests or 403 Quota Exceeded (instead of 401 Forbidden)
if ex.response.status_code == 429 or (
ex.response.status_code == 403 and "quota exceeded" in ex.response.text.lower()
):
if ex.response.status_code == 429 or (ex.response.status_code == 403 and "quota" in ex.response.text.lower()):
_ex_to_raise = MyBMWQuotaError

try:
Expand Down
9 changes: 7 additions & 2 deletions test/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,19 @@ async def test_403_quota_exceeded_vehicles_usa(caplog):
# get vehicles once
await account.get_vehicles()

mock_api.get("/eadrax-vcs/v4/vehicles/state").mock(return_value=httpx.Response(403, text="403 Quota Exceeded"))
mock_api.get("/eadrax-vcs/v4/vehicles/state").mock(
return_value=httpx.Response(
403,
json={"statusCode": 403, "message": "Out of call volume quota. Quota will be replenished in 02:12:20."},
)
)
caplog.set_level(logging.DEBUG)

with mock.patch("asyncio.sleep", new_callable=mock.AsyncMock):
with pytest.raises(MyBMWQuotaError):
await account.get_vehicles()

log_quota = [r for r in caplog.records if "Quota Exceeded" in r.message]
log_quota = [r for r in caplog.records if "quota" in r.message]
assert len(log_quota) == 1


Expand Down

0 comments on commit 48d4c14

Please sign in to comment.