Skip to content

Commit 3aff822

Browse files
authored
Merge pull request #297 from ynput/enhancement/add-download-file-helpers
Download helpers: Added helper methods to download project files
2 parents b7d097e + 8b0f23b commit 3aff822

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

ayon_api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
delete,
7171
download_file_to_stream,
7272
download_file,
73+
download_project_file,
74+
download_project_file_to_stream,
7375
upload_file_from_stream,
7476
upload_file,
7577
upload_reviewable,
@@ -349,6 +351,8 @@
349351
"delete",
350352
"download_file_to_stream",
351353
"download_file",
354+
"download_project_file",
355+
"download_project_file_to_stream",
352356
"upload_file_from_stream",
353357
"upload_file",
354358
"upload_reviewable",

ayon_api/_api.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,80 @@ def download_file(
992992
)
993993

994994

995+
def download_project_file(
996+
project_name: str,
997+
file_id: str,
998+
filepath: str,
999+
*,
1000+
chunk_size: Optional[int] = None,
1001+
progress: Optional[TransferProgress] = None,
1002+
) -> TransferProgress:
1003+
"""Download project file to filepath.
1004+
1005+
Project files are usually binary files, such as images, videos,
1006+
or other media files that can be accessed via api endpoint
1007+
'{server url}/api/projects/{project_name}/files/{file_id}'.
1008+
1009+
Args:
1010+
project_name (str): Project name.
1011+
file_id (str): File id.
1012+
filepath (str): Path where file will be downloaded.
1013+
chunk_size (Optional[int]): Size of chunks that are received
1014+
in single loop.
1015+
progress (Optional[TransferProgress]): Object that gives ability
1016+
to track download progress.
1017+
1018+
Returns:
1019+
TransferProgress: Progress object.
1020+
1021+
"""
1022+
con = get_server_api_connection()
1023+
return con.download_project_file(
1024+
project_name=project_name,
1025+
file_id=file_id,
1026+
filepath=filepath,
1027+
chunk_size=chunk_size,
1028+
progress=progress,
1029+
)
1030+
1031+
1032+
def download_project_file_to_stream(
1033+
project_name: str,
1034+
file_id: str,
1035+
stream: StreamType,
1036+
*,
1037+
chunk_size: Optional[int] = None,
1038+
progress: Optional[TransferProgress] = None,
1039+
) -> TransferProgress:
1040+
"""Download project file to a stream.
1041+
1042+
Project files are usually binary files, such as images, videos,
1043+
or other media files that can be accessed via api endpoint
1044+
'{server url}/api/projects/{project_name}/files/{file_id}'.
1045+
1046+
Args:
1047+
project_name (str): Project name.
1048+
file_id (str): File id.
1049+
stream (StreamType): Stream where output will be stored.
1050+
chunk_size (Optional[int]): Size of chunks that are received
1051+
in single loop.
1052+
progress (Optional[TransferProgress]): Object that gives ability
1053+
to track download progress.
1054+
1055+
Returns:
1056+
TransferProgress: Progress object.
1057+
1058+
"""
1059+
con = get_server_api_connection()
1060+
return con.download_project_file_to_stream(
1061+
project_name=project_name,
1062+
file_id=file_id,
1063+
stream=stream,
1064+
chunk_size=chunk_size,
1065+
progress=progress,
1066+
)
1067+
1068+
9951069
def upload_file_from_stream(
9961070
endpoint: str,
9971071
stream: StreamType,

ayon_api/server_api.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,76 @@ def download_file(
14981498

14991499
return progress
15001500

1501+
def download_project_file(
1502+
self,
1503+
project_name: str,
1504+
file_id: str,
1505+
filepath: str,
1506+
*,
1507+
chunk_size: Optional[int] = None,
1508+
progress: Optional[TransferProgress] = None,
1509+
) -> TransferProgress:
1510+
"""Download project file to filepath.
1511+
1512+
Project files are usually binary files, such as images, videos,
1513+
or other media files that can be accessed via api endpoint
1514+
'{server url}/api/projects/{project_name}/files/{file_id}'.
1515+
1516+
Args:
1517+
project_name (str): Project name.
1518+
file_id (str): File id.
1519+
filepath (str): Path where file will be downloaded.
1520+
chunk_size (Optional[int]): Size of chunks that are received
1521+
in single loop.
1522+
progress (Optional[TransferProgress]): Object that gives ability
1523+
to track download progress.
1524+
1525+
Returns:
1526+
TransferProgress: Progress object.
1527+
1528+
"""
1529+
return self.download_file(
1530+
f"api/projects/{project_name}/files/{file_id}",
1531+
filepath,
1532+
chunk_size=chunk_size,
1533+
progress=progress,
1534+
)
1535+
1536+
def download_project_file_to_stream(
1537+
self,
1538+
project_name: str,
1539+
file_id: str,
1540+
stream: StreamType,
1541+
*,
1542+
chunk_size: Optional[int] = None,
1543+
progress: Optional[TransferProgress] = None,
1544+
) -> TransferProgress:
1545+
"""Download project file to a stream.
1546+
1547+
Project files are usually binary files, such as images, videos,
1548+
or other media files that can be accessed via api endpoint
1549+
'{server url}/api/projects/{project_name}/files/{file_id}'.
1550+
1551+
Args:
1552+
project_name (str): Project name.
1553+
file_id (str): File id.
1554+
stream (StreamType): Stream where output will be stored.
1555+
chunk_size (Optional[int]): Size of chunks that are received
1556+
in single loop.
1557+
progress (Optional[TransferProgress]): Object that gives ability
1558+
to track download progress.
1559+
1560+
Returns:
1561+
TransferProgress: Progress object.
1562+
1563+
"""
1564+
return self.download_file_to_stream(
1565+
f"api/projects/{project_name}/files/{file_id}",
1566+
stream,
1567+
chunk_size=chunk_size,
1568+
progress=progress,
1569+
)
1570+
15011571
@staticmethod
15021572
def _upload_chunks_iter(
15031573
file_stream: StreamType,

0 commit comments

Comments
 (0)