-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfile_attachments.py
More file actions
293 lines (256 loc) · 11.5 KB
/
file_attachments.py
File metadata and controls
293 lines (256 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Any
from sift_client._internal.low_level_wrappers.remote_files import RemoteFilesLowLevelClient
from sift_client._internal.low_level_wrappers.upload import UploadLowLevelClient
from sift_client._internal.util.executor import run_sync_function
from sift_client._internal.util.file import download_file
from sift_client.resources._base import ResourceBase
from sift_client.util import cel_utils as cel
if TYPE_CHECKING:
import re
from sift_client.client import SiftClient
from sift_client.sift_types.asset import Asset
from sift_client.sift_types.file_attachment import (
FileAttachment,
FileAttachmentUpdate,
RemoteFileEntityType,
)
from sift_client.sift_types.run import Run
from sift_client.sift_types.test_report import TestReport, TestStep
class FileAttachmentsAPIAsync(ResourceBase):
"""High-level API for interacting with file attachments (remote files).
This class provides a Pythonic interface for managing file attachments
on Sift entities like runs, assets, and test reports.
"""
def __init__(self, sift_client: SiftClient):
"""Initialize the FileAttachmentsAPIAsync.
Args:
sift_client: The Sift client to use.
"""
super().__init__(sift_client)
self._low_level_client = RemoteFilesLowLevelClient(grpc_client=self.client.grpc_client)
self._upload_client = UploadLowLevelClient(rest_client=self.client.rest_client)
def _build_name_cel_filters(
self,
*,
name: str | None = None,
names: list[str] | None = None,
name_contains: str | None = None,
name_regex: str | re.Pattern | None = None,
) -> list[str]:
"""Override base implementation to use 'file_name' field instead of 'name'."""
filter_parts = []
if name:
filter_parts.append(cel.equals("file_name", name))
if names:
filter_parts.append(cel.in_("file_name", names))
if name_contains:
filter_parts.append(cel.contains("file_name", name_contains))
if name_regex:
filter_parts.append(cel.match("file_name", name_regex))
return filter_parts
async def get(self, *, file_attachment_id: str) -> FileAttachment:
"""Get a file attachment by ID.
Args:
file_attachment_id: The ID of the file attachment to retrieve.
Returns:
The FileAttachment.
"""
file_attachment = await self._low_level_client.get_remote_file(
remote_file_id=file_attachment_id,
sift_client=self.client,
)
return self._apply_client_to_instance(file_attachment)
async def list_(
self,
*,
name: str | None = None,
names: list[str] | None = None,
name_contains: str | None = None,
name_regex: str | re.Pattern | None = None,
# self ids
remote_file_ids: list[str] | None = None,
# created/modified ranges TODO: Add to backend
# created_after: datetime | None = None,
# created_before: datetime | None = None,
# modified_after: datetime | None = None,
# modified_before: datetime | None = None,
# created/modified users TODO: Add to backend
# created_by: Any | str | None = None,
# modified_by: Any | str | None = None,
# metadata TODO: Add to backend
# metadata: list[Any] | None = None,
# file specific
entities: list[Run | Asset | TestReport | TestStep] | None = None,
entity_type: RemoteFileEntityType | None = None,
entity_ids: list[str] | None = None,
# common filters
description_contains: str | None = None,
filter_query: str | None = None,
order_by: str | None = None,
limit: int | None = None,
page_size: int | None = None,
) -> list[FileAttachment]:
"""List file attachments with optional filtering.
Args:
name: Exact name of the file attachment.
names: List of file attachment names to filter by.
name_contains: Partial name of the file attachment.
name_regex: Regular expression to filter file attachments by name.
remote_file_ids: Filter to file attachments with any of these IDs.
entities: Filter to file attachments associated with these entities.
entity_type: Filter to file attachments associated with this entity type.
entity_ids: Filter to file attachments associated with these entity IDs.
description_contains: Partial description of the file attachment.
filter_query: Explicit CEL query to filter file attachments.
order_by: Field and direction to order results by. Note: Not supported by the backend, but it is here for API consistency.
limit: Maximum number of file attachments to return. If None, returns all matches.
page_size: Number of results to fetch per request. Lower this if you hit gRPC
message size limits on responses. If None, uses the server default.
Returns:
A list of FileAttachment objects that match the filter criteria.
"""
filter_parts = [
*self._build_name_cel_filters(
name=name, names=names, name_contains=name_contains, name_regex=name_regex
),
# *self._build_time_cel_filters(
# created_after=created_after,
# created_before=created_before,
# modified_after=modified_after,
# modified_before=modified_before,
# created_by=created_by,
# modified_by=modified_by,
# ),
# *self._build_tags_metadata_cel_filters(metadata=metadata),
*self._build_common_cel_filters(
description_contains=description_contains,
filter_query=filter_query,
),
]
if not entity_ids:
entity_ids = []
if entities:
entity_ids += [entity._id_or_error for entity in entities]
if entity_ids:
filter_parts.append(cel.in_("entity_id", entity_ids))
if entity_type:
filter_parts.append(cel.equals("entity_type", entity_type.name.lower()))
if remote_file_ids:
filter_parts.append(cel.in_("remote_file_id", remote_file_ids))
query_filter = cel.and_(*filter_parts)
file_attachments = await self._low_level_client.list_all_remote_files(
query_filter=query_filter or None,
max_results=limit,
**({"page_size": page_size} if page_size is not None else {}), # type: ignore[arg-type]
)
return self._apply_client_to_instances(file_attachments)
async def update(
self,
*,
file_attachment: FileAttachmentUpdate | dict,
) -> FileAttachment:
"""Update a file attachment.
Args:
file_attachment: The FileAttachmentUpdate with fields to update.
Returns:
The updated FileAttachment.
"""
from sift_client.sift_types.file_attachment import FileAttachmentUpdate
if isinstance(file_attachment, dict):
file_attachment = FileAttachmentUpdate.model_validate(file_attachment)
updated = await self._low_level_client.update_remote_file(
update=file_attachment,
sift_client=self.client,
)
return self._apply_client_to_instance(updated)
async def delete(
self, *, file_attachments: list[FileAttachment | str] | FileAttachment | str
) -> None:
"""Batch delete multiple file attachments.
Args:
file_attachments: List of FileAttachments or the IDs of the file attachments to delete (up to 1000).
"""
from sift_client.sift_types.file_attachment import FileAttachment
file_attachment_ids: list[str] = []
if isinstance(file_attachments, FileAttachment):
file_attachment_ids.append(file_attachments._id_or_error)
elif isinstance(file_attachments, str):
file_attachment_ids.append(file_attachments)
elif isinstance(file_attachments, list):
for file_attachment in file_attachments:
if isinstance(file_attachment, FileAttachment):
file_attachment_ids.append(file_attachment._id_or_error)
elif isinstance(file_attachment, str):
file_attachment_ids.append(file_attachment)
else:
raise ValueError(
"file_attachments must be a list of FileAttachment or list of str"
)
else:
raise ValueError(
"file_attachments must be a FileAttachment, a string, or a list of FileAttachment or strings"
)
await self._low_level_client.batch_delete_remote_files(remote_file_ids=file_attachment_ids)
async def get_download_url(self, *, file_attachment: FileAttachment | str) -> str:
"""Get a download URL for a file attachment.
Args:
file_attachment: The FileAttachment or the ID of the file attachment.
Returns:
The download URL for the file attachment.
"""
from sift_client.sift_types.file_attachment import FileAttachment
attachment_id = (
file_attachment._id_or_error
if isinstance(file_attachment, FileAttachment)
else file_attachment
)
return await self._low_level_client.get_remote_file_download_url(
remote_file_id=attachment_id
)
async def download(
self, *, file_attachment: FileAttachment | str, output_path: str | Path
) -> None:
"""Download a file attachment to a local path.
Args:
file_attachment: The FileAttachment or the ID of the file attachment to download.
output_path: The path to download the file attachment to.
"""
if isinstance(file_attachment, str):
file_attachment = await self.get(file_attachment_id=file_attachment)
url = await self._low_level_client.get_remote_file_download_url(
file_attachment._id_or_error
)
await run_sync_function(
lambda: download_file(url, Path(output_path), rest_client=self.client.rest_client)
)
async def upload(
self,
*,
path: str | Path,
entity: Asset | Run | TestReport | TestStep,
metadata: dict[str, Any] | None = None,
description: str | None = None,
organization_id: str | None = None,
) -> FileAttachment:
"""Upload a file attachment to a remote file.
Args:
path: The path to the file to upload.
entity: The entity that the file is attached to.
metadata: Optional metadata for the file (e.g., video/image metadata).
description: Optional description of the file.
organization_id: Optional organization ID.
Returns:
The uploaded FileAttachment.
"""
remote_file_id = await self._upload_client.upload_attachment(
path=path,
entity_id=entity._id_or_error,
entity_type=entity._get_entity_type_name(),
metadata=metadata,
description=description,
organization_id=organization_id,
)
# Should be able to remove await
return await self.get(file_attachment_id=remote_file_id)