Skip to content

Commit 4003b91

Browse files
authored
python(refactor): refactor files using run_in_executor to use the int… (#518)
1 parent 093d5c3 commit 4003b91

3 files changed

Lines changed: 11 additions & 31 deletions

File tree

python/lib/sift_client/_internal/low_level_wrappers/remote_files.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from __future__ import annotations
22

3-
import asyncio
43
from typing import TYPE_CHECKING, Any, cast
54

6-
import requests
75
from sift.remote_files.v1.remote_files_pb2 import (
86
BatchDeleteRemoteFilesRequest,
97
DeleteRemoteFileRequest,
@@ -182,24 +180,3 @@ async def get_remote_file_download_url(self, remote_file_id: str) -> str:
182180
request
183181
)
184182
return response.download_url
185-
186-
async def download_remote_file(self, file_attachment: FileAttachment) -> bytes:
187-
"""Download a remote file.
188-
189-
Args:
190-
file_attachment: The FileAttachment to download.
191-
192-
Returns:
193-
The downloaded file.
194-
"""
195-
url = await self.get_remote_file_download_url(file_attachment._id_or_error)
196-
197-
# Run the synchronous requests.get in a thread pool to avoid blocking
198-
def _download():
199-
response = requests.get(url)
200-
response.raise_for_status()
201-
return response.content
202-
203-
# Use run_in_executor for Python 3.8 compatibility (asyncio.to_thread was added in 3.9)
204-
loop = asyncio.get_event_loop()
205-
return await loop.run_in_executor(None, _download)

python/lib/sift_client/_internal/low_level_wrappers/upload.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import asyncio
43
import logging
54
import mimetypes
65
from pathlib import Path
@@ -9,6 +8,7 @@
98
from requests_toolbelt import MultipartEncoder
109

1110
from sift_client._internal.low_level_wrappers.base import LowLevelClientBase
11+
from sift_client._internal.util.executor import run_sync_function
1212
from sift_client.transport import WithRestClient
1313

1414
if TYPE_CHECKING:
@@ -100,9 +100,7 @@ async def upload_attachment(
100100
mimetype = "application/octet-stream" # fallback to generic 'binary data' MIME type
101101

102102
# Run the synchronous file upload in a thread pool to avoid blocking the event loop
103-
loop = asyncio.get_event_loop()
104-
return await loop.run_in_executor(
105-
None,
103+
return await run_sync_function(
106104
self._upload_file_sync,
107105
posix_path,
108106
file_name,

python/lib/sift_client/resources/file_attachments.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from __future__ import annotations
22

3+
from pathlib import Path
34
from typing import TYPE_CHECKING, Any
45

56
from sift_client._internal.low_level_wrappers.remote_files import RemoteFilesLowLevelClient
67
from sift_client._internal.low_level_wrappers.upload import UploadLowLevelClient
8+
from sift_client._internal.util.executor import run_sync_function
9+
from sift_client._internal.util.file import download_file
710
from sift_client.resources._base import ResourceBase
811
from sift_client.util import cel_utils as cel
912

1013
if TYPE_CHECKING:
1114
import re
12-
from pathlib import Path
1315

1416
from sift_client.client import SiftClient
1517
from sift_client.sift_types.asset import Asset
@@ -247,9 +249,12 @@ async def download(
247249
"""
248250
if isinstance(file_attachment, str):
249251
file_attachment = await self.get(file_attachment_id=file_attachment)
250-
content = await self._low_level_client.download_remote_file(file_attachment=file_attachment)
251-
with open(output_path, "wb") as f:
252-
f.write(content)
252+
url = await self._low_level_client.get_remote_file_download_url(
253+
file_attachment._id_or_error
254+
)
255+
await run_sync_function(
256+
lambda: download_file(url, Path(output_path), rest_client=self.client.rest_client)
257+
)
253258

254259
async def upload(
255260
self,

0 commit comments

Comments
 (0)