Skip to content

Commit c6b6848

Browse files
refactor: fold download encoding into _prepare_download_request
Drop the _prepare_file_download wrapper; encode the url and decode the returned filename directly in the shared (files-only) _prepare_download_request.
1 parent 552f842 commit c6b6848

2 files changed

Lines changed: 10 additions & 22 deletions

File tree

aidial_client/helpers/storage_resource.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ def _prepare_download_request(
189189
url: str | PurePosixPath,
190190
etag_if_match: str | None,
191191
) -> tuple[FinalRequestOptions, str]:
192-
storage_resource = self.get_storage_resource(str(url))
192+
storage_resource = self.get_storage_resource(
193+
percent_encode_resource_url(str(url))
194+
)
193195

194196
if storage_resource.filename is None:
195197
raise InvalidDialURLError("URL points to a directory, not a file")
@@ -204,4 +206,5 @@ def _prepare_download_request(
204206
),
205207
)
206208

207-
return options, storage_resource.filename
209+
# api_path is percent-encoded; return a human-readable filename.
210+
return options, unquote(storage_resource.filename)

aidial_client/resources/files.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from contextlib import asynccontextmanager
33
from pathlib import PurePosixPath
44
from typing import Literal
5-
from urllib.parse import unquote, urljoin
5+
from urllib.parse import urljoin
66

77
import httpx
88

@@ -18,28 +18,13 @@
1818
FinalRequestOptions,
1919
)
2020
from aidial_client._utils._dict import remove_none
21-
from aidial_client.helpers.storage_resource import (
22-
DialStorageResourceMixin,
23-
percent_encode_resource_url,
24-
)
21+
from aidial_client.helpers.storage_resource import DialStorageResourceMixin
2522
from aidial_client.resources.base import AsyncResource, Resource
2623
from aidial_client.resources.metadata import AsyncMetadata, Metadata
2724
from aidial_client.types.file import FileDownloadResponse
2825
from aidial_client.types.metadata import FileItem, FileMetadata
2926

3027

31-
def _prepare_file_download(
32-
resource: DialStorageResourceMixin,
33-
url: str | PurePosixPath,
34-
etag_if_match: str | None,
35-
) -> tuple[FinalRequestOptions, str]:
36-
"""Build a download request from an encoded path, decoded filename."""
37-
options, filename = resource._prepare_download_request(
38-
percent_encode_resource_url(str(url)), etag_if_match
39-
)
40-
return options, unquote(filename)
41-
42-
4328
def _move_copy_body(
4429
resource: DialStorageResourceMixin,
4530
source: str | PurePosixPath,
@@ -99,7 +84,7 @@ def download(
9984
url: str | PurePosixPath,
10085
etag_if_match: str | None = None,
10186
) -> FileDownloadResponse:
102-
options, filename = _prepare_file_download(self, url, etag_if_match)
87+
options, filename = self._prepare_download_request(url, etag_if_match)
10388
response = self.http_client.request(
10489
cast_to=httpx.Response,
10590
options=options,
@@ -205,7 +190,7 @@ async def download(
205190
url: str | PurePosixPath,
206191
etag_if_match: str | None = None,
207192
) -> FileDownloadResponse:
208-
options, filename = _prepare_file_download(self, url, etag_if_match)
193+
options, filename = self._prepare_download_request(url, etag_if_match)
209194
response = await self.http_client.request(
210195
cast_to=httpx.Response,
211196
options=options,
@@ -219,7 +204,7 @@ async def stream_download(
219204
url: str | PurePosixPath,
220205
etag_if_match: str | None = None,
221206
) -> AsyncIterator[FileDownloadResponse]:
222-
options, filename = _prepare_file_download(self, url, etag_if_match)
207+
options, filename = self._prepare_download_request(url, etag_if_match)
223208
async with self.http_client.stream(
224209
options=options,
225210
on_http_error=_files_error_processor,

0 commit comments

Comments
 (0)