def __call__( self, chunk_size: int = None, name: str = None, mime_type: str = None, metadata: Dict[str, Any] = None, asset_ids: List[int] = None, source: str = None, created_time: Dict[str, Any] = None, last_updated_time: Dict[str, Any] = None, source_created_time: Dict[str, Any] = None, source_modified_time: Dict[str, Any] = None, uploaded_time: Dict[str, Any] = None, external_id_prefix: str = None, uploaded: bool = None, limit: int = None, ) -> Generator[Union[FileMetadata, FileMetadataList], None, None]: """Iterate over files Fetches file metadata objects as they are iterated over, so you keep a limited number of metadata objects in memory. Args: chunk_size (int, optional): Number of files to return in each chunk. Defaults to yielding one event a time. name (str): Name of the file. mime_type (str): File type. E.g. text/plain, application/pdf, .. metadata (Dict[str, Any]): Custom, application specific metadata. String key -> String value asset_ids (List[int]): Only include files that reference these specific asset IDs. source (str): The source of this event. source_created_time (Dict[str, Any]): Filter for files where the sourceCreatedTime field has been set and is within the specified range. source_modified_time (Dict[str, Any]): Filter for files where the sourceModifiedTime field has been set and is within the specified range. created_time (Dict[str, Any]): Range between two timestamps last_updated_time (Dict[str, Any]): Range between two timestamps uploaded_time (Dict[str, Any]): Range between two timestamps external_id_prefix (str): External Id provided by client. Should be unique within the project. uploaded (bool): Whether or not the actual file is uploaded. This field is returned only by the API, it has no effect in a post body. limit (int, optional): Maximum number of assets to return. Defaults to 25. Set to -1, float("inf") or None to return all items. Yields: Union[FileMetadata, FileMetadataList]: yields FileMetadata one by one if chunk is not specified, else FileMetadataList objects. """ filter = FileMetadataFilter( name, mime_type, metadata, asset_ids, source, created_time, last_updated_time, uploaded_time, source_created_time, source_modified_time, external_id_prefix, uploaded, ).dump(camel_case=True) return self._list_generator(method="POST", chunk_size=chunk_size, filter=filter, limit=limit)
def test_search(self, mock_files_response): res = FILES_API.search(filter=FileMetadataFilter( external_id_prefix="abc")) assert mock_files_response.calls[0].response.json( )["items"] == res.dump(camel_case=True) assert { "search": { "name": None }, "filter": { "externalIdPrefix": "abc" }, "limit": 100 } == jsgz_load(mock_files_response.calls[0].request.body)
def test_search(self): res = COGNITE_CLIENT.files.search( name="big.txt", filter=FileMetadataFilter(created_time={"min": 0})) assert len(res) > 0
def test_aggregate(self): res = COGNITE_CLIENT.files.aggregate(filter=FileMetadataFilter( name="big.txt")) assert res[0].count > 0
def list( self, name: str = None, mime_type: str = None, metadata: Dict[str, Any] = None, asset_ids: List[int] = None, root_asset_ids: List[int] = None, root_asset_external_ids: List[str] = None, source: str = None, created_time: Dict[str, Any] = None, last_updated_time: Dict[str, Any] = None, source_created_time: Dict[str, Any] = None, source_modified_time: Dict[str, Any] = None, uploaded_time: Dict[str, Any] = None, external_id_prefix: str = None, uploaded: bool = None, limit: int = 25, ) -> FileMetadataList: """List files Args: name (str): Name of the file. mime_type (str): File type. E.g. text/plain, application/pdf, .. metadata (Dict[str, Any]): Custom, application specific metadata. String key -> String value asset_ids (List[int]): Only include files that reference these specific asset IDs. root_asset_ids (List[int]): The IDs of the root assets that the related assets should be children of. root_asset_external_ids (List[str]): The external IDs of the root assets that the related assets should be children of. source (str): The source of this event. created_time (Dict[str, Any]): Range between two timestamps last_updated_time (Dict[str, Any]): Range between two timestamps uploaded_time (Dict[str, Any]): Range between two timestamps source_created_time (Dict[str, Any]): Filter for files where the sourceCreatedTime field has been set and is within the specified range. source_modified_time (Dict[str, Any]): Filter for files where the sourceModifiedTime field has been set and is within the specified range. external_id_prefix (str): External Id provided by client. Should be unique within the project. uploaded (bool): Whether or not the actual file is uploaded. This field is returned only by the API, it has no effect in a post body. limit (int, optional): Max number of files to return. Defaults to 25. Set to -1, float("inf") or None to return all items. Returns: FileMetadataList: The requested files. Examples: List files metadata and filter on external id prefix:: >>> from cognite.client import CogniteClient >>> c = CogniteClient() >>> file_list = c.files.list(limit=5, external_id_prefix="prefix") Iterate over files metadata:: >>> from cognite.client import CogniteClient >>> c = CogniteClient() >>> for file_metadata in c.files: ... file_metadata # do something with the file metadata Iterate over chunks of files metadata to reduce memory load:: >>> from cognite.client import CogniteClient >>> c = CogniteClient() >>> for file_list in c.files(chunk_size=2500): ... file_list # do something with the files """ if root_asset_ids or root_asset_external_ids: root_asset_ids = self._process_ids(root_asset_ids, root_asset_external_ids, wrap_ids=True) filter = FileMetadataFilter( name=name, mime_type=mime_type, metadata=metadata, asset_ids=asset_ids, root_asset_ids=root_asset_ids, source=source, created_time=created_time, last_updated_time=last_updated_time, uploaded_time=uploaded_time, source_created_time=source_created_time, source_modified_time=source_modified_time, external_id_prefix=external_id_prefix, uploaded=uploaded, ).dump(camel_case=True) return self._list(method="POST", limit=limit, filter=filter)
def __call__( self, chunk_size: int = None, name: str = None, mime_type: str = None, metadata: Dict[str, str] = None, asset_ids: List[int] = None, asset_external_ids: List[str] = None, root_asset_ids: List[int] = None, root_asset_external_ids: List[str] = None, asset_subtree_ids: List[int] = None, asset_subtree_external_ids: List[str] = None, data_set_ids: List[int] = None, data_set_external_ids: List[str] = None, source: str = None, created_time: Union[Dict[str, Any], TimestampRange] = None, last_updated_time: Union[Dict[str, Any], TimestampRange] = None, source_created_time: Union[Dict[str, Any], TimestampRange] = None, source_modified_time: Union[Dict[str, Any], TimestampRange] = None, uploaded_time: Union[Dict[str, Any], TimestampRange] = None, external_id_prefix: str = None, uploaded: bool = None, limit: int = None, ) -> Generator[Union[FileMetadata, FileMetadataList], None, None]: """Iterate over files Fetches file metadata objects as they are iterated over, so you keep a limited number of metadata objects in memory. Args: chunk_size (int, optional): Number of files to return in each chunk. Defaults to yielding one event a time. name (str): Name of the file. mime_type (str): File type. E.g. text/plain, application/pdf, .. metadata (Dict[str, str]): Custom, application specific metadata. String key -> String value asset_ids (List[int]): Only include files that reference these specific asset IDs. asset_subtree_external_ids (List[str]): Only include files that reference these specific asset external IDs. root_asset_ids (List[int]): The IDs of the root assets that the related assets should be children of. root_asset_external_ids (List[str]): The external IDs of the root assets that the related assets should be children of. asset_subtree_ids (List[int]): List of asset subtrees ids to filter on. asset_subtree_external_ids (List[str]): List of asset subtrees external ids to filter on. data_set_ids (List[int]): Return only files in the specified data sets with these ids. data_set_external_ids (List[str]): Return only files in the specified data sets with these external ids. source (str): The source of this event. source_created_time (Union[Dict[str, Any], TimestampRange]): Filter for files where the sourceCreatedTime field has been set and is within the specified range. source_modified_time (Union[Dict[str, Any], TimestampRange]): Filter for files where the sourceModifiedTime field has been set and is within the specified range. created_time (Union[Dict[str, int], TimestampRange]): Range between two timestamps. Possible keys are `min` and `max`, with values given as time stamps in ms. last_updated_time (Union[Dict[str, int], TimestampRange]): Range between two timestamps. Possible keys are `min` and `max`, with values given as time stamps in ms. uploaded_time (Union[Dict[str, Any], TimestampRange]): Range between two timestamps external_id_prefix (str): External Id provided by client. Should be unique within the project. uploaded (bool): Whether or not the actual file is uploaded. This field is returned only by the API, it has no effect in a post body. limit (int, optional): Maximum number of files to return. Defaults to return all items. Yields: Union[FileMetadata, FileMetadataList]: yields FileMetadata one by one if chunk is not specified, else FileMetadataList objects. """ if root_asset_ids or root_asset_external_ids: root_asset_ids = self._process_ids(root_asset_ids, root_asset_external_ids, wrap_ids=True) if asset_subtree_ids or asset_subtree_external_ids: asset_subtree_ids = self._process_ids(asset_subtree_ids, asset_subtree_external_ids, wrap_ids=True) if data_set_ids or data_set_external_ids: data_set_ids = self._process_ids(data_set_ids, data_set_external_ids, wrap_ids=True) filter = FileMetadataFilter( name=name, mime_type=mime_type, metadata=metadata, asset_ids=asset_ids, asset_external_ids=asset_external_ids, root_asset_ids=root_asset_ids, asset_subtree_ids=asset_subtree_ids, source=source, created_time=created_time, last_updated_time=last_updated_time, uploaded_time=uploaded_time, source_created_time=source_created_time, source_modified_time=source_modified_time, external_id_prefix=external_id_prefix, uploaded=uploaded, data_set_ids=data_set_ids, ).dump(camel_case=True) return self._list_generator(method="POST", chunk_size=chunk_size, filter=filter, limit=limit)