Skip to content

Commit

Permalink
Update API usage (#755)
Browse files Browse the repository at this point in the history
* Update API usage

* Adjust delete API

* lint
  • Loading branch information
ludeeus authored Dec 5, 2024
1 parent 2f89d0c commit 6ade26a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 43 deletions.
14 changes: 4 additions & 10 deletions hass_nabucasa/cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,9 @@ async def async_files_download_details(
if TYPE_CHECKING:
assert cloud.id_token is not None
resp = await cloud.websession.get(
f"https://{cloud.servicehandlers_server}/files/download_details",
f"https://{cloud.servicehandlers_server}/files"
f"/download_details/{storage_type}/{filename}",
headers={"authorization": cloud.id_token, USER_AGENT: cloud.client.client_name},
json={
"storage_type": storage_type,
"filename": filename,
},
)

data: dict[str, Any] = await resp.json()
Expand All @@ -235,11 +232,8 @@ async def async_files_list(
if TYPE_CHECKING:
assert cloud.id_token is not None
resp = await cloud.websession.get(
f"https://{cloud.servicehandlers_server}/files/list",
f"https://{cloud.servicehandlers_server}/files/{storage_type}",
headers={"authorization": cloud.id_token, USER_AGENT: cloud.client.client_name},
json={
"storage_type": storage_type,
},
)

data: dict[str, Any] | list[dict[str, Any]] = await resp.json()
Expand Down Expand Up @@ -298,7 +292,7 @@ async def async_files_delete_file(
if TYPE_CHECKING:
assert cloud.id_token is not None
resp = await cloud.websession.delete(
f"https://{cloud.servicehandlers_server}/files/delete",
f"https://{cloud.servicehandlers_server}/files",
headers={"authorization": cloud.id_token, USER_AGENT: cloud.client.client_name},
json={
"storage_type": storage_type,
Expand Down
49 changes: 16 additions & 33 deletions tests/test_cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async def test_async_files_download_details(
):
"""Test the async_files_download_details function."""
aioclient_mock.get(
"https://example.com/files/download_details",
"https://example.com/files/download_details/test/test.txt",
json={
"url": "https://example.com/some/path",
},
Expand All @@ -205,16 +205,13 @@ async def test_async_files_download_details(
)

assert len(aioclient_mock.mock_calls) == 1
# 2 is the body
assert aioclient_mock.mock_calls[0][2] == {
"filename": "test.txt",
"storage_type": "test",
}

assert details == {
"url": "https://example.com/some/path",
}
assert "Fetched https://example.com/files/download_details (200)" in caplog.text
assert (
"Fetched https://example.com/files/download_details/test/test.txt (200)"
in caplog.text
)


async def test_async_files_download_details_error(
Expand All @@ -224,7 +221,7 @@ async def test_async_files_download_details_error(
):
"""Test the async_files_download_details function with error."""
aioclient_mock.get(
"https://example.com/files/download_details",
"https://example.com/files/download_details/test/test.txt",
status=400,
json={"message": "Boom!"},
)
Expand All @@ -239,14 +236,9 @@ async def test_async_files_download_details_error(
)

assert len(aioclient_mock.mock_calls) == 1
# 2 is the body
assert aioclient_mock.mock_calls[0][2] == {
"filename": "test.txt",
"storage_type": "test",
}

assert (
"Fetched https://example.com/files/download_details (400) Boom!" in caplog.text
"Fetched https://example.com/files/download_details/test/test.txt (400) Boom!"
in caplog.text
)


Expand All @@ -257,7 +249,7 @@ async def test_async_files_list(
):
"""Test the async_files_list function."""
aioclient_mock.get(
"https://example.com/files/list",
"https://example.com/files/test",
json=[{"Key": "test.txt", "LastModified": "2021-01-01T00:00:00Z", "Size": 2}],
)
auth_cloud_mock.id_token = "mock-id-token"
Expand All @@ -269,19 +261,14 @@ async def test_async_files_list(
)

assert len(aioclient_mock.mock_calls) == 1
# 2 is the body
assert aioclient_mock.mock_calls[0][2] == {
"storage_type": "test",
}

assert details == [
{
"Key": "test.txt",
"LastModified": "2021-01-01T00:00:00Z",
"Size": 2,
},
]
assert "Fetched https://example.com/files/list (200)" in caplog.text
assert "Fetched https://example.com/files/test (200)" in caplog.text


async def test_async_files_list_error(
Expand All @@ -291,7 +278,7 @@ async def test_async_files_list_error(
):
"""Test the async_files_list function with error listing files."""
aioclient_mock.get(
"https://example.com/files/list",
"https://example.com/files/test",
status=400,
json={"message": "Boom!"},
)
Expand All @@ -305,12 +292,8 @@ async def test_async_files_list_error(
)

assert len(aioclient_mock.mock_calls) == 1
# 2 is the body
assert aioclient_mock.mock_calls[0][2] == {
"storage_type": "test",
}

assert "Fetched https://example.com/files/list (400) Boom!" in caplog.text
assert "Fetched https://example.com/files/test (400) Boom!" in caplog.text


async def test_async_files_upload_details(
Expand Down Expand Up @@ -402,7 +385,7 @@ async def test_async_files_delete_file(
):
"""Test the async_files_delete_file function."""
aioclient_mock.delete(
"https://example.com/files/delete",
"https://example.com/files",
)
auth_cloud_mock.id_token = "mock-id-token"
auth_cloud_mock.servicehandlers_server = "example.com"
Expand All @@ -420,7 +403,7 @@ async def test_async_files_delete_file(
"storage_type": "test",
}

assert "Fetched https://example.com/files/delete (200)" in caplog.text
assert "Fetched https://example.com/files (200)" in caplog.text


async def test_async_files_delete_file_error(
Expand All @@ -430,7 +413,7 @@ async def test_async_files_delete_file_error(
):
"""Test the async_files_delete_file function with error."""
aioclient_mock.delete(
"https://example.com/files/delete",
"https://example.com/files",
status=400,
json={"message": "Boom!"},
)
Expand All @@ -451,4 +434,4 @@ async def test_async_files_delete_file_error(
"storage_type": "test",
}

assert "Fetched https://example.com/files/delete (400) Boom!" in caplog.text
assert "Fetched https://example.com/files (400) Boom!" in caplog.text

0 comments on commit 6ade26a

Please sign in to comment.