Skip to content

Commit 1da0d99

Browse files
fix async client handle files
1 parent fa65d7d commit 1da0d99

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

indico/http/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import asyncio
2-
import aiohttp
32
import http.cookiejar
43
import logging
54
from contextlib import contextmanager
65
from copy import deepcopy
76
from pathlib import Path
8-
from typing import Union, Optional
7+
from typing import Optional, Union
98

9+
import aiohttp
1010
import requests
11+
1112
from indico.client.request import HTTPRequest
1213
from indico.config import IndicoConfig
1314
from indico.errors import (
1415
IndicoAuthenticationFailed,
15-
IndicoRequestError,
1616
IndicoHibernationError,
17+
IndicoRequestError,
1718
)
18-
from indico.http.serialization import deserialize, aio_deserialize
19+
from indico.http.serialization import aio_deserialize, deserialize
20+
1921
from .retry import aioretry
2022

2123
logger = logging.getLogger(__file__)
@@ -87,7 +89,6 @@ def execute_request(self, request: HTTPRequest):
8789

8890
@contextmanager
8991
def _handle_files(self, req_kwargs):
90-
9192
streams = None
9293
# deepcopying buffers is not supported
9394
# so, remove "streams" before the deepcopy.
@@ -256,7 +257,7 @@ def _handle_files(self, req_kwargs):
256257
)
257258
dup_counts[path.stem] += 1
258259
else:
259-
data.add_field("file", fd, filename=path.stem)
260+
data.add_field("file", fd, filename=path.name)
260261
dup_counts[path.stem] = 1
261262
file_args.append(data)
262263

@@ -313,7 +314,6 @@ async def _make_request(
313314
verify_ssl=self.config.verify_ssl,
314315
**request_kwargs,
315316
) as response:
316-
317317
# If auth expired refresh
318318
if response.status == 401 and not _refreshed:
319319
await self.get_short_lived_access_token()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from pathlib import Path
2+
from unittest.mock import MagicMock, patch
3+
4+
import pytest
5+
6+
from indico.client import AIOHTTPClient
7+
from indico.config import IndicoConfig
8+
9+
10+
@pytest.fixture
11+
async def aiohttp_client():
12+
config = IndicoConfig(protocol="https", host="example.com", api_token="dummy_token")
13+
client = AIOHTTPClient(config=config)
14+
yield client
15+
await client.request_session.close()
16+
17+
18+
@pytest.mark.asyncio
19+
async def test_handle_files_correct_filename(aiohttp_client):
20+
file_path = Path("/tmp/testfile.txt")
21+
22+
request_kwargs = {"files": [file_path]}
23+
24+
with patch("pathlib.Path.open", new_callable=MagicMock) as mock_open:
25+
mock_open.return_value = MagicMock()
26+
with aiohttp_client._handle_files(request_kwargs) as file_args:
27+
assert len(file_args) == 1
28+
for arg in file_args:
29+
fields = arg._fields
30+
for field in fields:
31+
field_dict = field[0]
32+
filename = field_dict.get("filename")
33+
assert filename == "testfile.txt"
34+
await aiohttp_client.request_session.close()

0 commit comments

Comments
 (0)