|
| 1 | +from typing import List, Optional, Type, TypeVar |
| 2 | + |
| 3 | +from pydantic import ValidationError, BaseModel |
| 4 | + |
| 5 | +from .._config import Config |
| 6 | +from .._execution_context import ExecutionContext |
| 7 | +from ._base_service import BaseService |
| 8 | +from .._utils import infer_bindings, RequestSpec, Endpoint, header_folder |
| 9 | +from ..models.entities import EntityGetByIdResponse, EntityRecord |
| 10 | +from ..tracing import traced |
| 11 | + |
| 12 | +T = TypeVar("T", bound=BaseModel) |
| 13 | + |
| 14 | +class EntitiesService(BaseService): |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + config: Config, |
| 19 | + execution_context: ExecutionContext |
| 20 | + ) -> None: |
| 21 | + super().__init__(config=config, execution_context=execution_context) |
| 22 | + |
| 23 | + @traced(name="entity_retrieve", run_type="uipath") |
| 24 | + def retrieve( |
| 25 | + self, |
| 26 | + entity_key: str) -> EntityGetByIdResponse: |
| 27 | + |
| 28 | + spec = self._retrieve_spec(entity_key) |
| 29 | + response = self.request(spec.method, spec.endpoint) |
| 30 | + |
| 31 | + return EntityGetByIdResponse.model_validate(response.json()) |
| 32 | + |
| 33 | + @traced(name="entity_retrieve", run_type="uipath") |
| 34 | + async def retrieve_async( |
| 35 | + self, |
| 36 | + entity_key: str) -> EntityGetByIdResponse: |
| 37 | + spec = self._retrieve_spec(entity_key) |
| 38 | + |
| 39 | + response = await self.request_async(spec.method, spec.endpoint) |
| 40 | + |
| 41 | + return EntityGetByIdResponse.model_validate(response.json()) |
| 42 | + |
| 43 | + @traced(name="entity_list_records", run_type="uipath") |
| 44 | + def list_records( |
| 45 | + self, |
| 46 | + entity_key: str, |
| 47 | + schema: Optional[Type[T]] = None, # Optional schema |
| 48 | + start: Optional[int] = None, |
| 49 | + limit: Optional[int] = None |
| 50 | + ) -> List[EntityRecord]: |
| 51 | + |
| 52 | + # Example method to generate the API request specification (mocked here) |
| 53 | + spec = self._list_records_spec(entity_key, start, limit) |
| 54 | + |
| 55 | + # Make the HTTP request (assumes self.request exists) |
| 56 | + response = self.request(spec.method, spec.endpoint) |
| 57 | + |
| 58 | + # Parse the response JSON and extract the "value" field |
| 59 | + records_data = response.json().get("value", []) |
| 60 | + |
| 61 | + # Validate and wrap records |
| 62 | + validated_records = [] |
| 63 | + for record in records_data: |
| 64 | + try: |
| 65 | + # Validate & wrap the record using EntityRecord.from_data |
| 66 | + validated_record = EntityRecord.from_data(data=record, model=schema) |
| 67 | + validated_records.append(validated_record) |
| 68 | + except ValueError as e: |
| 69 | + print(f"Failed to validate record: {record} => {e}") |
| 70 | + continue |
| 71 | + |
| 72 | + return validated_records |
| 73 | + |
| 74 | + def _retrieve_spec( |
| 75 | + self, |
| 76 | + entity_key: str, |
| 77 | + ) -> RequestSpec: |
| 78 | + |
| 79 | + return RequestSpec( |
| 80 | + method="GET", |
| 81 | + endpoint=Endpoint(f"datafabric_/api/Entity/{entity_key}"), |
| 82 | + ) |
| 83 | + |
| 84 | + def _list_records_spec( |
| 85 | + self, |
| 86 | + entity_key: str, |
| 87 | + start: Optional[int] = None, |
| 88 | + limit: Optional[int] = None, |
| 89 | + ) -> RequestSpec: |
| 90 | + |
| 91 | + return RequestSpec( |
| 92 | + method="GET", |
| 93 | + endpoint=Endpoint(f"datafabric_/api/EntityService/entity/{entity_key}/read?start={start}&limit={limit}"), |
| 94 | + ) |
| 95 | + |
| 96 | + def _create_spec( |
| 97 | + self, |
| 98 | + entity_key: str, |
| 99 | + ) -> RequestSpec: |
| 100 | + |
| 101 | + return RequestSpec( |
| 102 | + method="POST", |
| 103 | + endpoint=Endpoint(f"datafabric_/api/EntityService/entity/{entity_key}/insert-batch"), |
| 104 | + ) |
| 105 | + |
| 106 | + def _update_spec( |
| 107 | + self, |
| 108 | + entity_key: str, |
| 109 | + ) -> RequestSpec: |
| 110 | + |
| 111 | + return RequestSpec( |
| 112 | + method="PUT", |
| 113 | + endpoint=Endpoint(f"datafabric_/api/EntityService/entity/{entity_key}/update-batch"), |
| 114 | + ) |
| 115 | + |
| 116 | + def _delete_spec( |
| 117 | + self, |
| 118 | + entity_key: str, |
| 119 | + ) -> RequestSpec: |
| 120 | + |
| 121 | + return RequestSpec( |
| 122 | + method="DELETE", |
| 123 | + endpoint=Endpoint(f"datafabric_/api/EntityService/entity/{entity_key}/delete-batch"), |
| 124 | + ) |
0 commit comments