|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import TYPE_CHECKING, Any, cast |
| 5 | + |
| 6 | +from sift.unit.v2.unit_pb2 import ( |
| 7 | + CreateUnitRequest, |
| 8 | + CreateUnitResponse, |
| 9 | + ListUnitsRequest, |
| 10 | + ListUnitsResponse, |
| 11 | +) |
| 12 | +from sift.unit.v2.unit_pb2 import Unit as UnitProto |
| 13 | +from sift.unit.v2.unit_pb2_grpc import UnitServiceStub |
| 14 | + |
| 15 | +from sift_client._internal.low_level_wrappers.base import DEFAULT_PAGE_SIZE, LowLevelClientBase |
| 16 | +from sift_client.transport import WithGrpcClient |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from sift_client.transport.grpc_transport import GrpcClient |
| 20 | + |
| 21 | +# Configure logging |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +class UnitsLowLevelClient(LowLevelClientBase, WithGrpcClient): |
| 26 | + """Low-level client for the Units service. |
| 27 | +
|
| 28 | + This class provides a thin wrapper around the autogenerated bindings for the Units service. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, grpc_client: GrpcClient): |
| 32 | + """Initialize the UnitsLowLevelClient. |
| 33 | +
|
| 34 | + Args: |
| 35 | + grpc_client: The gRPC client to use for making API calls. |
| 36 | + """ |
| 37 | + super().__init__(grpc_client) |
| 38 | + |
| 39 | + async def create_unit(self, name: str) -> UnitProto: |
| 40 | + """Create a new unit. |
| 41 | +
|
| 42 | + If a unit with the same name already exists, it is returned instead of creating a duplicate. |
| 43 | +
|
| 44 | + Args: |
| 45 | + name: The name of the unit. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + The created unit proto, whose unit_id is used to reference the unit. |
| 49 | +
|
| 50 | + Raises: |
| 51 | + ValueError: If name is not provided. |
| 52 | + """ |
| 53 | + if not name: |
| 54 | + raise ValueError("name must be provided") |
| 55 | + |
| 56 | + request = CreateUnitRequest(name=name) |
| 57 | + response = await self._grpc_client.get_stub(UnitServiceStub).CreateUnit(request) |
| 58 | + return cast("CreateUnitResponse", response).unit |
| 59 | + |
| 60 | + async def list_units( |
| 61 | + self, |
| 62 | + *, |
| 63 | + page_size: int | None = DEFAULT_PAGE_SIZE, |
| 64 | + page_token: str | None = None, |
| 65 | + query_filter: str | None = None, |
| 66 | + order_by: str | None = None, |
| 67 | + ) -> tuple[list[UnitProto], str]: |
| 68 | + """List units with optional filtering and pagination. |
| 69 | +
|
| 70 | + Args: |
| 71 | + page_size: The maximum number of units to return. |
| 72 | + page_token: A page token for pagination. |
| 73 | + query_filter: A CEL filter string (e.g. filtering on unit_id or abbreviated_name). |
| 74 | + order_by: How to order the retrieved units. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + A tuple of (unit protos, next_page_token). |
| 78 | + """ |
| 79 | + request_kwargs: dict[str, Any] = {} |
| 80 | + if page_size is not None: |
| 81 | + request_kwargs["page_size"] = page_size |
| 82 | + if page_token is not None: |
| 83 | + request_kwargs["page_token"] = page_token |
| 84 | + if query_filter is not None: |
| 85 | + request_kwargs["filter"] = query_filter |
| 86 | + if order_by is not None: |
| 87 | + request_kwargs["order_by"] = order_by |
| 88 | + |
| 89 | + request = ListUnitsRequest(**request_kwargs) |
| 90 | + response = await self._grpc_client.get_stub(UnitServiceStub).ListUnits(request) |
| 91 | + response = cast("ListUnitsResponse", response) |
| 92 | + |
| 93 | + return list(response.units), response.next_page_token |
0 commit comments