|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, cast |
| 4 | + |
| 5 | +from sift.data_imports.v2.data_imports_pb2 import ( |
| 6 | + CreateDataImportFromUploadRequest, |
| 7 | + CreateDataImportFromUploadResponse, |
| 8 | + DetectConfigRequest, |
| 9 | + DetectConfigResponse, |
| 10 | + GetDataImportRequest, |
| 11 | + GetDataImportResponse, |
| 12 | +) |
| 13 | +from sift.data_imports.v2.data_imports_pb2_grpc import DataImportServiceStub |
| 14 | + |
| 15 | +from sift_client._internal.low_level_wrappers.base import LowLevelClientBase |
| 16 | +from sift_client.sift_types.data_import import ( |
| 17 | + CsvImportConfig, |
| 18 | + Hdf5ImportConfig, |
| 19 | + ImportConfig, |
| 20 | + ParquetFlatDatasetImportConfig, |
| 21 | + ParquetSingleChannelPerRowImportConfig, |
| 22 | + TdmsImportConfig, |
| 23 | +) |
| 24 | +from sift_client.transport import WithGrpcClient |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from sift.data_imports.v2.data_imports_pb2 import DataTypeKey |
| 28 | + |
| 29 | + from sift_client.transport.grpc_transport import GrpcClient |
| 30 | + |
| 31 | + |
| 32 | +def _set_config_on_request( |
| 33 | + request: CreateDataImportFromUploadRequest, |
| 34 | + config: ImportConfig, |
| 35 | +) -> None: |
| 36 | + """Set the appropriate config field on a proto request based on the config type.""" |
| 37 | + if isinstance(config, CsvImportConfig): |
| 38 | + request.csv_config.CopyFrom(config._to_proto()) |
| 39 | + elif isinstance( |
| 40 | + config, (ParquetFlatDatasetImportConfig, ParquetSingleChannelPerRowImportConfig) |
| 41 | + ): |
| 42 | + request.parquet_config.CopyFrom(config._to_proto()) |
| 43 | + elif isinstance(config, TdmsImportConfig): |
| 44 | + request.tdms_config.CopyFrom(config._to_proto()) |
| 45 | + elif isinstance(config, Hdf5ImportConfig): |
| 46 | + request.hdf5_config.CopyFrom(config._to_proto()) |
| 47 | + else: |
| 48 | + raise TypeError(f"Unsupported import config type: {type(config).__name__}") |
| 49 | + |
| 50 | + |
| 51 | +class DataImportsLowLevelClient(LowLevelClientBase, WithGrpcClient): |
| 52 | + """Low-level client for the DataImportService. |
| 53 | +
|
| 54 | + This class provides a thin wrapper around the autogenerated bindings for the DataImportsAPI. |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__(self, grpc_client: GrpcClient): |
| 58 | + WithGrpcClient.__init__(self, grpc_client=grpc_client) |
| 59 | + |
| 60 | + async def create_from_upload(self, config: ImportConfig) -> tuple[str, str]: |
| 61 | + """Create a data import and get back a presigned upload URL. |
| 62 | +
|
| 63 | + Args: |
| 64 | + config: The import configuration. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + A tuple of (data_import_id, upload_url). |
| 68 | + """ |
| 69 | + request = CreateDataImportFromUploadRequest() |
| 70 | + _set_config_on_request(request, config) |
| 71 | + response = await self._grpc_client.get_stub( |
| 72 | + DataImportServiceStub |
| 73 | + ).CreateDataImportFromUpload(request) |
| 74 | + response = cast("CreateDataImportFromUploadResponse", response) |
| 75 | + return response.data_import_id, response.upload_url |
| 76 | + |
| 77 | + async def get(self, data_import_id: str) -> GetDataImportResponse: |
| 78 | + """Get a data import by ID. |
| 79 | +
|
| 80 | + Args: |
| 81 | + data_import_id: The ID of the data import. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + The GetDataImportResponse proto. |
| 85 | + """ |
| 86 | + request = GetDataImportRequest(data_import_id=data_import_id) |
| 87 | + response = await self._grpc_client.get_stub(DataImportServiceStub).GetDataImport(request) |
| 88 | + return cast("GetDataImportResponse", response) |
| 89 | + |
| 90 | + async def detect_config( |
| 91 | + self, data: bytes, data_type_key: DataTypeKey.ValueType |
| 92 | + ) -> DetectConfigResponse: |
| 93 | + """Call the DetectConfig RPC to auto-detect import configuration. |
| 94 | +
|
| 95 | + Args: |
| 96 | + data: A sample of the file content (e.g. the first 64 KiB). |
| 97 | + data_type_key: The file type hint. |
| 98 | +
|
| 99 | + Returns: |
| 100 | + The raw DetectConfigResponse proto. |
| 101 | + """ |
| 102 | + request = DetectConfigRequest(data=data, type=data_type_key) |
| 103 | + response = await self._grpc_client.get_stub(DataImportServiceStub).DetectConfig(request) |
| 104 | + return cast("DetectConfigResponse", response) |
0 commit comments