|
| 1 | +import json as json_module |
1 | 2 | import logging |
2 | 3 | from typing import Any, Dict, List, Optional, Type |
3 | 4 |
|
|
24 | 25 | fetch_resolved_entities_async, |
25 | 26 | ) |
26 | 27 | from .entities import ( |
| 28 | + ChoiceSetValue, |
27 | 29 | DataFabricEntityItem, |
28 | 30 | Entity, |
29 | 31 | EntityRecord, |
@@ -282,6 +284,99 @@ async def list_entities_async(self) -> List[Entity]: |
282 | 284 | entities_data = response.json() |
283 | 285 | return [Entity.model_validate(entity) for entity in entities_data] |
284 | 286 |
|
| 287 | + @traced(name="list_choicesets", run_type="uipath") |
| 288 | + def list_choicesets(self) -> List[Entity]: |
| 289 | + """List all choice sets in Data Service. |
| 290 | +
|
| 291 | + Returns: |
| 292 | + List[Entity]: A list of all choice set entities. |
| 293 | +
|
| 294 | + Examples: |
| 295 | + List all choice sets:: |
| 296 | +
|
| 297 | + choicesets = entities_service.list_choicesets() |
| 298 | + for cs in choicesets: |
| 299 | + print(f"{cs.display_name} ({cs.id})") |
| 300 | + """ |
| 301 | + spec = self._list_choicesets_spec() |
| 302 | + response = self.request(spec.method, spec.endpoint) |
| 303 | + return [Entity.model_validate(item) for item in response.json()] |
| 304 | + |
| 305 | + @traced(name="list_choicesets", run_type="uipath") |
| 306 | + async def list_choicesets_async(self) -> List[Entity]: |
| 307 | + """Asynchronously list all choice sets in Data Service. |
| 308 | +
|
| 309 | + Returns: |
| 310 | + List[Entity]: A list of all choice set entities. |
| 311 | + """ |
| 312 | + spec = self._list_choicesets_spec() |
| 313 | + response = await self.request_async(spec.method, spec.endpoint) |
| 314 | + return [Entity.model_validate(item) for item in response.json()] |
| 315 | + |
| 316 | + @traced(name="get_choiceset_values", run_type="uipath") |
| 317 | + def get_choiceset_values( |
| 318 | + self, |
| 319 | + choiceset_id: str, |
| 320 | + start: int | None = None, |
| 321 | + limit: int | None = None, |
| 322 | + ) -> List[ChoiceSetValue]: |
| 323 | + """Get the values of a choice set by its ID. |
| 324 | +
|
| 325 | + Args: |
| 326 | + choiceset_id: The unique identifier of the choice set. |
| 327 | + start: Optional offset for pagination. |
| 328 | + limit: Optional page size for pagination. |
| 329 | +
|
| 330 | + Returns: |
| 331 | + List[ChoiceSetValue]: The values in the choice set, each containing |
| 332 | + id, name, display_name, and number_id. |
| 333 | +
|
| 334 | + Examples: |
| 335 | + Get all values in a choice set:: |
| 336 | +
|
| 337 | + values = entities_service.get_choiceset_values("choiceset-id") |
| 338 | + for v in values: |
| 339 | + print(f"{v.number_id}: {v.display_name}") |
| 340 | + """ |
| 341 | + spec = self._get_choiceset_values_spec(choiceset_id, start=start, limit=limit) |
| 342 | + response = self.request( |
| 343 | + spec.method, spec.endpoint, params=spec.params, json=spec.json |
| 344 | + ) |
| 345 | + data = response.json() |
| 346 | + raw_values = data.get("jsonValue", "[]") |
| 347 | + items = ( |
| 348 | + json_module.loads(raw_values) if isinstance(raw_values, str) else raw_values |
| 349 | + ) |
| 350 | + return [ChoiceSetValue.model_validate(item) for item in items] |
| 351 | + |
| 352 | + @traced(name="get_choiceset_values", run_type="uipath") |
| 353 | + async def get_choiceset_values_async( |
| 354 | + self, |
| 355 | + choiceset_id: str, |
| 356 | + start: int | None = None, |
| 357 | + limit: int | None = None, |
| 358 | + ) -> List[ChoiceSetValue]: |
| 359 | + """Asynchronously get the values of a choice set by its ID. |
| 360 | +
|
| 361 | + Args: |
| 362 | + choiceset_id: The unique identifier of the choice set. |
| 363 | + start: Optional offset for pagination. |
| 364 | + limit: Optional page size for pagination. |
| 365 | +
|
| 366 | + Returns: |
| 367 | + List[ChoiceSetValue]: The values in the choice set. |
| 368 | + """ |
| 369 | + spec = self._get_choiceset_values_spec(choiceset_id, start=start, limit=limit) |
| 370 | + response = await self.request_async( |
| 371 | + spec.method, spec.endpoint, params=spec.params, json=spec.json |
| 372 | + ) |
| 373 | + data = response.json() |
| 374 | + raw_values = data.get("jsonValue", "[]") |
| 375 | + items = ( |
| 376 | + json_module.loads(raw_values) if isinstance(raw_values, str) else raw_values |
| 377 | + ) |
| 378 | + return [ChoiceSetValue.model_validate(item) for item in items] |
| 379 | + |
285 | 380 | @traced(name="entity_list_records", run_type="uipath") |
286 | 381 | def list_records( |
287 | 382 | self, |
@@ -1173,6 +1268,32 @@ def _delete_batch_spec(self, entity_key: str, record_ids: List[str]) -> RequestS |
1173 | 1268 | json=record_ids, |
1174 | 1269 | ) |
1175 | 1270 |
|
| 1271 | + def _list_choicesets_spec(self) -> RequestSpec: |
| 1272 | + return RequestSpec( |
| 1273 | + method="GET", |
| 1274 | + endpoint=Endpoint("datafabric_/api/Entity/choiceset"), |
| 1275 | + ) |
| 1276 | + |
| 1277 | + def _get_choiceset_values_spec( |
| 1278 | + self, |
| 1279 | + choiceset_id: str, |
| 1280 | + start: int | None = None, |
| 1281 | + limit: int | None = None, |
| 1282 | + ) -> RequestSpec: |
| 1283 | + params: dict[str, Any] = {} |
| 1284 | + if start is not None: |
| 1285 | + params["start"] = start |
| 1286 | + if limit is not None: |
| 1287 | + params["limit"] = limit |
| 1288 | + return RequestSpec( |
| 1289 | + method="POST", |
| 1290 | + endpoint=Endpoint( |
| 1291 | + f"datafabric_/api/EntityService/entity/{choiceset_id}/query_expansion" |
| 1292 | + ), |
| 1293 | + params=params, |
| 1294 | + json={}, |
| 1295 | + ) |
| 1296 | + |
1176 | 1297 | def _validate_sql_query(self, sql_query: str) -> None: |
1177 | 1298 | query = sql_query.strip().rstrip(";").strip() |
1178 | 1299 | if not query: |
|
0 commit comments