Skip to content

Commit bce5e26

Browse files
committed
feat: add AssetsService.exists() with mocked tests
1 parent 66dbaa8 commit bce5e26

5 files changed

Lines changed: 97 additions & 3 deletions

File tree

packages/uipath-platform/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "uipath-platform"
3-
version = "0.1.84"
3+
version = "0.1.85"
44
description = "HTTP client library for programmatic access to UiPath Platform"
55
readme = { file = "README.md", content-type = "text/markdown" }
66
requires-python = ">=3.11"

packages/uipath-platform/src/uipath/platform/orchestrator/_assets_service.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,51 @@ async def update_async(
602602

603603
return response.json()
604604

605+
@traced(name="assets_exists", run_type="uipath")
606+
def exists(
607+
self,
608+
name: str,
609+
*,
610+
folder_key: Optional[str] = None,
611+
folder_path: Optional[str] = None,
612+
) -> bool:
613+
"""Check whether an asset with the given name exists.
614+
615+
Args:
616+
name (str): The name of the asset.
617+
folder_key (Optional[str]): The key of the folder. Override the default one set in the SDK config.
618+
folder_path (Optional[str]): The path of the folder. Override the default one set in the SDK config.
619+
620+
Returns:
621+
bool: True if an asset with the given name exists, False otherwise.
622+
623+
Examples:
624+
```python
625+
from uipath.platform import UiPath
626+
627+
client = UiPath()
628+
629+
client.assets.exists(name="MyAsset")
630+
```
631+
"""
632+
spec = self._list_spec(
633+
folder_path=folder_path,
634+
folder_key=folder_key,
635+
filter=f"Name eq '{name}'",
636+
orderby=None,
637+
skip=0,
638+
top=1,
639+
)
640+
response = self.request(
641+
spec.method,
642+
url=spec.endpoint,
643+
params=spec.params,
644+
content=spec.content,
645+
headers=spec.headers,
646+
json=spec.json,
647+
)
648+
return len(response.json().get("value", [])) > 0
649+
605650
@property
606651
def custom_headers(self) -> Dict[str, str]:
607652
return self.folder_headers

packages/uipath-platform/tests/services/test_assets_service.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,55 @@ def test_retrieve_asset(
177177
== f"UiPath.Python.Sdk/UiPath.Python.Sdk.Activities.AssetsService.retrieve/{version}"
178178
)
179179

180+
class TestExistsAsset:
181+
def test_exists_returns_true(
182+
self,
183+
httpx_mock: HTTPXMock,
184+
base_url: str,
185+
org: str,
186+
tenant: str,
187+
version: str,
188+
config: UiPathApiConfig,
189+
monkeypatch: pytest.MonkeyPatch,
190+
) -> None:
191+
monkeypatch.delenv("UIPATH_ROBOT_KEY", raising=False)
192+
service = AssetsService(
193+
config=config,
194+
execution_context=UiPathExecutionContext(),
195+
)
196+
197+
httpx_mock.add_response(
198+
url=f"{base_url}{org}{tenant}/orchestrator_/odata/Assets/UiPath.Server.Configuration.OData.GetFiltered?$skip=0&$top=1&$filter=Name eq 'MyAsset'",
199+
status_code=200,
200+
json={"value": [{"Key": "asset-key-1", "Name": "MyAsset"}]},
201+
)
202+
203+
assert service.exists(name="MyAsset") is True
204+
205+
def test_exists_returns_false(
206+
self,
207+
httpx_mock: HTTPXMock,
208+
base_url: str,
209+
org: str,
210+
tenant: str,
211+
version: str,
212+
config: UiPathApiConfig,
213+
monkeypatch: pytest.MonkeyPatch,
214+
) -> None:
215+
monkeypatch.delenv("UIPATH_ROBOT_KEY", raising=False)
216+
service = AssetsService(
217+
config=config,
218+
execution_context=UiPathExecutionContext(),
219+
)
220+
221+
httpx_mock.add_response(
222+
url=f"{base_url}{org}{tenant}/orchestrator_/odata/Assets/UiPath.Server.Configuration.OData.GetFiltered?$skip=0&$top=1&$filter=Name eq 'Missing'",
223+
status_code=200,
224+
json={"value": []},
225+
)
226+
227+
assert service.exists(name="Missing") is False
228+
180229
class TestListAssets:
181230
def test_list_assets(
182231
self,

packages/uipath-platform/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/uipath/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)