Repository: bimmerconnected/bimmer_connected
Pull request #545 (2023-06-30 20:08:24)

Update quota exceeded recognition
## Proposed change
<!--
  Describe the big picture of your changes here to communicate to the
  maintainers why we should accept this pull request. If it fixes a bug
  or resolves a feature request, be sure to link to that issue in the
  additional information section.
-->
Update quota handling.


## Type of change
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (which adds functionality to this library)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests

## Additional information
<!--
  Details are important, and help maintainers processing your PR.
  Please be sure to fill out additional details, if applicable.
-->

- This PR fixes or closes issue: fixes #
- This PR is related to issue: https://github.com/home-assistant/core/issues/85962#issuecomment-1613846057

## Checklist
<!--
  Put an `x` in the boxes that apply. You can also fill these out after
  creating the PR. If you're unsure about any of them, don't hesitate to ask.
  We're here to help! This is simply a reminder of what we are going to look
  for before merging your code.
-->

- [ ] The code change is tested and works locally.
- [ ] Tests have been added to verify that the new code works.


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/bimmerconnected/bimmer_connected/pull/545?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected) Report
Patch coverage: **`100.00`**% and no project coverage change.
> Comparison is base [(`770b1fa`)](https://app.codecov.io/gh/bimmerconnected/bimmer_connected/commit/770b1faf2d0ea0f0cc9de3afae72287c72b41281?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected) 100.00% compared to head [(`55d0f5e`)](https://app.codecov.io/gh/bimmerconnected/bimmer_connected/pull/545?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected) 100.00%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##            master      #545   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           18        18           
  Lines         1483      1483           
=========================================
  Hits          1483      1483           
```

| Flag | Coverage Δ | |
|---|---|---|
| 3.10 | `100.00% <100.00%> (ø)` | |
| 3.11 | `100.00% <100.00%> (ø)` | |
| 3.7 | `100.00% <100.00%> (ø)` | |
| 3.8 | `100.00% <100.00%> (ø)` | |
| 3.9 | `100.00% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://app.codecov.io/gh/bimmerconnected/bimmer_connected/pull/545?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected) | Coverage Δ | |
|---|---|---|
| [bimmer\_connected/api/authentication.py](https://app.codecov.io/gh/bimmerconnected/bimmer_connected/pull/545?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected#diff-YmltbWVyX2Nvbm5lY3RlZC9hcGkvYXV0aGVudGljYXRpb24ucHk=) | `100.00% <100.00%> (ø)` | |
| [bimmer\_connected/api/utils.py](https://app.codecov.io/gh/bimmerconnected/bimmer_connected/pull/545?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected#diff-YmltbWVyX2Nvbm5lY3RlZC9hcGkvdXRpbHMucHk=) | `100.00% <100.00%> (ø)` | |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/bimmerconnected/bimmer_connected/pull/545?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bimmerconnected).


Patch:
diff --git a/bimmer_connected/api/authentication.py b/bimmer_connected/api/authentication.py
index 06b07d7..1f763b1 100644
--- a/bimmer_connected/api/authentication.py
+++ b/bimmer_connected/api/authentication.py
@@ -94,7 +94,7 @@ class MyBMWAuthentication(httpx.Auth):
             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()
diff --git a/bimmer_connected/api/utils.py b/bimmer_connected/api/utils.py
index 050a660..d00472b 100644
--- a/bimmer_connected/api/utils.py
+++ b/bimmer_connected/api/utils.py
@@ -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:


Test patch:
diff --git a/test/test_account.py b/test/test_account.py
index 5a5e33e..0ddf9c5 100644
--- a/test/test_account.py
+++ b/test/test_account.py
@@ -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