Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
dca2466e25 | |||
f24d664c62 | |||
e9de91a02e | |||
e25f636ebc | |||
1a9ddbe93e | |||
3e49e5adbd | |||
92bd17ce30 |
@ -1,5 +1,11 @@
|
||||
# UTE API Wrapper 🇺🇾
|
||||
|
||||
# THIS API NO LONGER WORKS
|
||||
|
||||
UTE deprecated the API that this wrapper uses on April 15th 2024. More information [here](https://github.com/rogsme/ute_homeassistant_integration/issues/3#issuecomment-2054332575).
|
||||
|
||||
I'll archive this repository in a few days.
|
||||
|
||||
<p align="center">
|
||||
<img src="https://gitlab.com/uploads/-/system/project/avatar/48558040/icon.png" alt="ute-wrapper"/>
|
||||
</p>
|
||||
|
@ -1,7 +1,7 @@
|
||||
[tool.poetry]
|
||||
name = "ute-wrapper"
|
||||
version = "2.3.1"
|
||||
description = "A wrapper to interact with UTE's API"
|
||||
version = "2.5.1"
|
||||
description = "[DEPRECATED] A wrapper to interact with UTE's API"
|
||||
authors = ["Roger Gonzalez <roger@rogs.me>"]
|
||||
license = "GPL-3.0-or-later"
|
||||
readme = "README.md"
|
||||
|
@ -112,7 +112,7 @@ class UTEClient:
|
||||
delay: float = 2,
|
||||
) -> requests.Response:
|
||||
"""
|
||||
Make a HTTP request with retries.
|
||||
Make a HTTP request with retries and handle expired authorization.
|
||||
|
||||
Args:
|
||||
method (str): The HTTP method to use. Accepted methods are ``GET``, ``POST``.
|
||||
@ -134,12 +134,15 @@ class UTEClient:
|
||||
"Connection": "Keep-Alive",
|
||||
}
|
||||
|
||||
if self.authorization:
|
||||
headers["Authorization"] = f"Bearer {self.authorization}"
|
||||
|
||||
for attempt in range(retries):
|
||||
if self.authorization:
|
||||
headers["Authorization"] = f"Bearer {self.authorization}"
|
||||
|
||||
try:
|
||||
response = getattr(requests, method.lower(), self._method_not_supported)(url, headers=headers, json=data)
|
||||
if response.status_code == requests.codes.unauthorized:
|
||||
self._login()
|
||||
continue
|
||||
response.raise_for_status()
|
||||
return response
|
||||
except (requests.RequestException, Exception):
|
||||
|
@ -50,6 +50,18 @@ def test_make_request_no_authorization(ute_client, mocker: MockerFixture):
|
||||
assert response.json()["success"] is True
|
||||
|
||||
|
||||
def test_make_request_expired_authorization(ute_client, mocker: MockerFixture):
|
||||
"""Test the _make_request method when the authorization code has expired."""
|
||||
mocked_unauthorized_response = mocker.Mock(status_code=requests.codes.unauthorized)
|
||||
mocked_success_response = mocker.Mock(status_code=requests.codes.ok, json=lambda: {"success": True})
|
||||
mocker.patch("requests.get", side_effect=[mocked_unauthorized_response, mocked_success_response])
|
||||
mocker.patch.object(ute_client, "_login")
|
||||
response = ute_client._make_request("GET", "http://example.com")
|
||||
assert response.status_code == requests.codes.ok
|
||||
assert response.json()["success"] is True
|
||||
ute_client._login.assert_called_once()
|
||||
|
||||
|
||||
def test_make_request_all_retries_failed(ute_client, mocker: MockerFixture):
|
||||
"""Test the _make_request method when all retries fail."""
|
||||
mocked_response = mocker.Mock()
|
||||
|
Reference in New Issue
Block a user