sift_py.file_attachment.service
1from pathlib import Path 2from typing import List, Optional, Union, cast 3 4from sift.remote_files.v1.remote_files_pb2 import ( 5 BatchDeleteRemoteFilesRequest, 6 GetRemoteFileDownloadUrlRequest, 7 GetRemoteFileDownloadUrlResponse, 8 GetRemoteFileRequest, 9 GetRemoteFileResponse, 10 ListRemoteFilesRequest, 11 ListRemoteFilesResponse, 12 RemoteFile, 13) 14from sift.remote_files.v1.remote_files_pb2_grpc import RemoteFileServiceStub 15 16from sift_py.file_attachment._internal.download import download_remote_file 17from sift_py.file_attachment._internal.upload import UploadService 18from sift_py.file_attachment.entity import Entity 19from sift_py.file_attachment.metadata import Metadata 20from sift_py.grpc.transport import SiftChannel 21from sift_py.rest import SiftRestConfig 22 23 24class FileAttachmentService: 25 """ 26 Service used to retrieve, upload, download, and delete file attachments. Seee `sift_py.file_attachment` 27 for more information and examples on how to use this service. 28 """ 29 30 _remote_file_service_stub: RemoteFileServiceStub 31 _upload_service: UploadService 32 33 def __init__(self, channel: SiftChannel, restconf: SiftRestConfig): 34 self._remote_file_service_stub = RemoteFileServiceStub(channel) 35 self._upload_service = UploadService(restconf) 36 37 def retrieve_attachments(self, entity: Entity) -> List[RemoteFile]: 38 """ 39 Retrieves all file attachments for the provided `entity`. 40 """ 41 42 filter = f'entity_id=="{entity.entity_id}" && entity_type=="{entity.entity_type.value}"' 43 page_size = 1_000 44 next_page_token = "" 45 46 remote_files: List[RemoteFile] = [] 47 48 while True: 49 req = ListRemoteFilesRequest( 50 filter=filter, 51 page_size=page_size, 52 page_token=next_page_token, 53 ) 54 res = cast(ListRemoteFilesResponse, self._remote_file_service_stub.ListRemoteFiles(req)) 55 remote_files.extend(res.remote_files) 56 next_page_token = res.next_page_token 57 58 if not next_page_token: 59 break 60 61 return remote_files 62 63 def upload_attachment( 64 self, 65 path: Union[str, Path], 66 entity: Entity, 67 metadata: Optional[Metadata], 68 description: Optional[str] = None, 69 organization_id: Optional[str] = None, 70 ) -> RemoteFile: 71 """ 72 Uploads a file pointed to by `path` and attaches it to the provided `entity`. 73 74 - `path`: A path to the file to upload to Sift as a file attachment. 75 - `entity`: The entity to attach the file to. 76 - `metadata`: Optional metadata to include with the specific file. 77 - `description`: An optional description to provide for the file attachment. 78 - `organization_id`: Only required if your user belongs to multiple organizations. 79 """ 80 remote_file_id = self._upload_service.upload_attachment( 81 path, 82 entity, 83 metadata, 84 description, 85 organization_id, 86 ) 87 req = GetRemoteFileRequest(remote_file_id=remote_file_id) 88 res = cast(GetRemoteFileResponse, self._remote_file_service_stub.GetRemoteFile(req)) 89 return res.remote_file 90 91 def download_attachment( 92 self, 93 file: Union[RemoteFile, str], 94 out: Optional[Union[str, Path]] = None, 95 ) -> Path: 96 """ 97 Downloads a file attachment and saves it locally. 98 99 - `remote_file`: Could either be an instance of `RemoteFile` or the ID of the remote file to download. 100 - `out`: If unspecified, then the file will be downloaded to the current working directory with the original name. 101 """ 102 103 if isinstance(file, RemoteFile): 104 remote_file = file 105 else: 106 req = GetRemoteFileRequest(remote_file_id=file) 107 res = cast(GetRemoteFileResponse, self._remote_file_service_stub.GetRemoteFile(req)) 108 remote_file = res.remote_file 109 110 output_file_path = ( 111 Path(out) if isinstance(out, str) else Path(remote_file.file_name).resolve() 112 ) 113 114 download_url_req = GetRemoteFileDownloadUrlRequest( 115 remote_file_id=remote_file.remote_file_id 116 ) 117 download_url_res = cast( 118 GetRemoteFileDownloadUrlResponse, 119 self._remote_file_service_stub.GetRemoteFileDownloadUrl(download_url_req), 120 ) 121 url = download_url_res.download_url 122 123 download_remote_file(url, output_file_path) 124 125 return output_file_path 126 127 def delete_file_attachments(self, *to_delete: Union[str, RemoteFile]): 128 """ 129 Deletes remote files given a set of arguments that could either be instances of `RemoteFile` or the ID 130 of remote files to delete 131 """ 132 remote_file_ids = [ 133 remote_file.remote_file_id if isinstance(remote_file, RemoteFile) else remote_file 134 for remote_file in to_delete 135 ] 136 137 batch_size = 1_000 138 for i in range(0, len(remote_file_ids), batch_size): 139 batch = remote_file_ids[i : i + batch_size] 140 self._remote_file_service_stub.BatchDeleteRemoteFiles( 141 BatchDeleteRemoteFilesRequest(remote_file_ids=batch) 142 )
class
FileAttachmentService:
25class FileAttachmentService: 26 """ 27 Service used to retrieve, upload, download, and delete file attachments. Seee `sift_py.file_attachment` 28 for more information and examples on how to use this service. 29 """ 30 31 _remote_file_service_stub: RemoteFileServiceStub 32 _upload_service: UploadService 33 34 def __init__(self, channel: SiftChannel, restconf: SiftRestConfig): 35 self._remote_file_service_stub = RemoteFileServiceStub(channel) 36 self._upload_service = UploadService(restconf) 37 38 def retrieve_attachments(self, entity: Entity) -> List[RemoteFile]: 39 """ 40 Retrieves all file attachments for the provided `entity`. 41 """ 42 43 filter = f'entity_id=="{entity.entity_id}" && entity_type=="{entity.entity_type.value}"' 44 page_size = 1_000 45 next_page_token = "" 46 47 remote_files: List[RemoteFile] = [] 48 49 while True: 50 req = ListRemoteFilesRequest( 51 filter=filter, 52 page_size=page_size, 53 page_token=next_page_token, 54 ) 55 res = cast(ListRemoteFilesResponse, self._remote_file_service_stub.ListRemoteFiles(req)) 56 remote_files.extend(res.remote_files) 57 next_page_token = res.next_page_token 58 59 if not next_page_token: 60 break 61 62 return remote_files 63 64 def upload_attachment( 65 self, 66 path: Union[str, Path], 67 entity: Entity, 68 metadata: Optional[Metadata], 69 description: Optional[str] = None, 70 organization_id: Optional[str] = None, 71 ) -> RemoteFile: 72 """ 73 Uploads a file pointed to by `path` and attaches it to the provided `entity`. 74 75 - `path`: A path to the file to upload to Sift as a file attachment. 76 - `entity`: The entity to attach the file to. 77 - `metadata`: Optional metadata to include with the specific file. 78 - `description`: An optional description to provide for the file attachment. 79 - `organization_id`: Only required if your user belongs to multiple organizations. 80 """ 81 remote_file_id = self._upload_service.upload_attachment( 82 path, 83 entity, 84 metadata, 85 description, 86 organization_id, 87 ) 88 req = GetRemoteFileRequest(remote_file_id=remote_file_id) 89 res = cast(GetRemoteFileResponse, self._remote_file_service_stub.GetRemoteFile(req)) 90 return res.remote_file 91 92 def download_attachment( 93 self, 94 file: Union[RemoteFile, str], 95 out: Optional[Union[str, Path]] = None, 96 ) -> Path: 97 """ 98 Downloads a file attachment and saves it locally. 99 100 - `remote_file`: Could either be an instance of `RemoteFile` or the ID of the remote file to download. 101 - `out`: If unspecified, then the file will be downloaded to the current working directory with the original name. 102 """ 103 104 if isinstance(file, RemoteFile): 105 remote_file = file 106 else: 107 req = GetRemoteFileRequest(remote_file_id=file) 108 res = cast(GetRemoteFileResponse, self._remote_file_service_stub.GetRemoteFile(req)) 109 remote_file = res.remote_file 110 111 output_file_path = ( 112 Path(out) if isinstance(out, str) else Path(remote_file.file_name).resolve() 113 ) 114 115 download_url_req = GetRemoteFileDownloadUrlRequest( 116 remote_file_id=remote_file.remote_file_id 117 ) 118 download_url_res = cast( 119 GetRemoteFileDownloadUrlResponse, 120 self._remote_file_service_stub.GetRemoteFileDownloadUrl(download_url_req), 121 ) 122 url = download_url_res.download_url 123 124 download_remote_file(url, output_file_path) 125 126 return output_file_path 127 128 def delete_file_attachments(self, *to_delete: Union[str, RemoteFile]): 129 """ 130 Deletes remote files given a set of arguments that could either be instances of `RemoteFile` or the ID 131 of remote files to delete 132 """ 133 remote_file_ids = [ 134 remote_file.remote_file_id if isinstance(remote_file, RemoteFile) else remote_file 135 for remote_file in to_delete 136 ] 137 138 batch_size = 1_000 139 for i in range(0, len(remote_file_ids), batch_size): 140 batch = remote_file_ids[i : i + batch_size] 141 self._remote_file_service_stub.BatchDeleteRemoteFiles( 142 BatchDeleteRemoteFilesRequest(remote_file_ids=batch) 143 )
Service used to retrieve, upload, download, and delete file attachments. Seee sift_py.file_attachment
for more information and examples on how to use this service.
FileAttachmentService(channel: grpc.Channel, restconf: sift_py.rest.SiftRestConfig)
def
retrieve_attachments( self, entity: sift_py.file_attachment.entity.Entity) -> List[sift.remote_files.v1.remote_files_pb2.RemoteFile]:
38 def retrieve_attachments(self, entity: Entity) -> List[RemoteFile]: 39 """ 40 Retrieves all file attachments for the provided `entity`. 41 """ 42 43 filter = f'entity_id=="{entity.entity_id}" && entity_type=="{entity.entity_type.value}"' 44 page_size = 1_000 45 next_page_token = "" 46 47 remote_files: List[RemoteFile] = [] 48 49 while True: 50 req = ListRemoteFilesRequest( 51 filter=filter, 52 page_size=page_size, 53 page_token=next_page_token, 54 ) 55 res = cast(ListRemoteFilesResponse, self._remote_file_service_stub.ListRemoteFiles(req)) 56 remote_files.extend(res.remote_files) 57 next_page_token = res.next_page_token 58 59 if not next_page_token: 60 break 61 62 return remote_files
Retrieves all file attachments for the provided entity
.
def
upload_attachment( self, path: Union[str, pathlib.Path], entity: sift_py.file_attachment.entity.Entity, metadata: Union[sift_py.file_attachment.metadata.Metadata, NoneType], description: Union[str, NoneType] = None, organization_id: Union[str, NoneType] = None) -> sift.remote_files.v1.remote_files_pb2.RemoteFile:
64 def upload_attachment( 65 self, 66 path: Union[str, Path], 67 entity: Entity, 68 metadata: Optional[Metadata], 69 description: Optional[str] = None, 70 organization_id: Optional[str] = None, 71 ) -> RemoteFile: 72 """ 73 Uploads a file pointed to by `path` and attaches it to the provided `entity`. 74 75 - `path`: A path to the file to upload to Sift as a file attachment. 76 - `entity`: The entity to attach the file to. 77 - `metadata`: Optional metadata to include with the specific file. 78 - `description`: An optional description to provide for the file attachment. 79 - `organization_id`: Only required if your user belongs to multiple organizations. 80 """ 81 remote_file_id = self._upload_service.upload_attachment( 82 path, 83 entity, 84 metadata, 85 description, 86 organization_id, 87 ) 88 req = GetRemoteFileRequest(remote_file_id=remote_file_id) 89 res = cast(GetRemoteFileResponse, self._remote_file_service_stub.GetRemoteFile(req)) 90 return res.remote_file
Uploads a file pointed to by path
and attaches it to the provided entity
.
path
: A path to the file to upload to Sift as a file attachment.entity
: The entity to attach the file to.metadata
: Optional metadata to include with the specific file.description
: An optional description to provide for the file attachment.organization_id
: Only required if your user belongs to multiple organizations.
def
download_attachment( self, file: Union[sift.remote_files.v1.remote_files_pb2.RemoteFile, str], out: Union[str, pathlib.Path, NoneType] = None) -> pathlib.Path:
92 def download_attachment( 93 self, 94 file: Union[RemoteFile, str], 95 out: Optional[Union[str, Path]] = None, 96 ) -> Path: 97 """ 98 Downloads a file attachment and saves it locally. 99 100 - `remote_file`: Could either be an instance of `RemoteFile` or the ID of the remote file to download. 101 - `out`: If unspecified, then the file will be downloaded to the current working directory with the original name. 102 """ 103 104 if isinstance(file, RemoteFile): 105 remote_file = file 106 else: 107 req = GetRemoteFileRequest(remote_file_id=file) 108 res = cast(GetRemoteFileResponse, self._remote_file_service_stub.GetRemoteFile(req)) 109 remote_file = res.remote_file 110 111 output_file_path = ( 112 Path(out) if isinstance(out, str) else Path(remote_file.file_name).resolve() 113 ) 114 115 download_url_req = GetRemoteFileDownloadUrlRequest( 116 remote_file_id=remote_file.remote_file_id 117 ) 118 download_url_res = cast( 119 GetRemoteFileDownloadUrlResponse, 120 self._remote_file_service_stub.GetRemoteFileDownloadUrl(download_url_req), 121 ) 122 url = download_url_res.download_url 123 124 download_remote_file(url, output_file_path) 125 126 return output_file_path
Downloads a file attachment and saves it locally.
remote_file
: Could either be an instance ofRemoteFile
or the ID of the remote file to download.out
: If unspecified, then the file will be downloaded to the current working directory with the original name.
def
delete_file_attachments( self, *to_delete: Union[str, sift.remote_files.v1.remote_files_pb2.RemoteFile]):
128 def delete_file_attachments(self, *to_delete: Union[str, RemoteFile]): 129 """ 130 Deletes remote files given a set of arguments that could either be instances of `RemoteFile` or the ID 131 of remote files to delete 132 """ 133 remote_file_ids = [ 134 remote_file.remote_file_id if isinstance(remote_file, RemoteFile) else remote_file 135 for remote_file in to_delete 136 ] 137 138 batch_size = 1_000 139 for i in range(0, len(remote_file_ids), batch_size): 140 batch = remote_file_ids[i : i + batch_size] 141 self._remote_file_service_stub.BatchDeleteRemoteFiles( 142 BatchDeleteRemoteFilesRequest(remote_file_ids=batch) 143 )
Deletes remote files given a set of arguments that could either be instances of RemoteFile
or the ID
of remote files to delete