|
| 1 | +import dataclasses |
| 2 | +import dotenv |
| 3 | +import logging |
| 4 | +import os |
| 5 | + |
| 6 | +from typing import Optional |
| 7 | +from uipath import UiPath |
| 8 | +from uipath.tracing import traced |
| 9 | + |
| 10 | +dotenv.load_dotenv() |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | +UIPATH_CLIENT_ID = "client_id" |
| 14 | +UIPATH_CLIENT_SECRET = os.getenv("UIPATH_CLIENT_SECRET") |
| 15 | +UIPATH_SCOPE = "OR.Assets" |
| 16 | +UIPATH_URL = "base_url" |
| 17 | + |
| 18 | +uipath = UiPath( |
| 19 | + client_id=UIPATH_CLIENT_ID, |
| 20 | + client_secret=UIPATH_CLIENT_SECRET, |
| 21 | + scope=UIPATH_SCOPE, |
| 22 | + base_url=UIPATH_URL |
| 23 | +) |
| 24 | + |
| 25 | +@dataclasses.dataclass |
| 26 | +class AgentInput: |
| 27 | + """Input data structure for the UiPath agent. |
| 28 | +
|
| 29 | + Attributes: |
| 30 | + asset_name (str): The name of the UiPath asset. |
| 31 | + folder_path (str): The folder path where the asset is located. |
| 32 | + """ |
| 33 | + asset_name: str |
| 34 | + folder_path: str |
| 35 | + |
| 36 | +def get_asset(name: str, folder_path: str) -> Optional[object]: |
| 37 | + """Retrieve an asset from UiPath. |
| 38 | +
|
| 39 | + Args: |
| 40 | + name (str): The asset name. |
| 41 | + folder_path (str): The UiPath folder path. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + Optional[object]: The asset object if found, else None. |
| 45 | + """ |
| 46 | + return uipath.assets.retrieve(name=name, folder_path=folder_path) |
| 47 | + |
| 48 | +def check_asset(asset: object) -> str: |
| 49 | + """Check if an asset's IntValue is within a valid range. |
| 50 | +
|
| 51 | + Args: |
| 52 | + asset (object): The asset object. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + str: Result message depending on asset state. |
| 56 | + """ |
| 57 | + if asset is None: |
| 58 | + return "Asset not found." |
| 59 | + |
| 60 | + int_value = getattr(asset, "int_value", None) |
| 61 | + if int_value is None: |
| 62 | + return "Asset does not have an IntValue." |
| 63 | + |
| 64 | + if 100 <= int_value <= 1000: |
| 65 | + return f"Asset '{asset.name}' has a valid IntValue: {int_value}" |
| 66 | + else: |
| 67 | + return f"Asset '{asset.name}' has an out-of-range IntValue: {int_value}" |
| 68 | + |
| 69 | +@traced() |
| 70 | +def main(input: AgentInput) -> str: |
| 71 | + """Main entry point for the agent. |
| 72 | +
|
| 73 | + Args: |
| 74 | + input (AgentInput): The input containing asset details. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + str: Message with the result of the asset check. |
| 78 | + """ |
| 79 | + asset = get_asset(input.asset_name, input.folder_path) |
| 80 | + return check_asset(asset) |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + input_data = AgentInput(asset_name="test-asset", folder_path="TestFolder") |
| 84 | + result = main(input_data) |
| 85 | + print(result) |
0 commit comments