Skip to content

Commit ad20f1d

Browse files
committed
feat: add AssetsService.exists() with mocked tests
1 parent f234583 commit ad20f1d

5 files changed

Lines changed: 99 additions & 5 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.59"
3+
version = "0.1.60"
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
@@ -470,6 +470,51 @@ async def update_async(
470470

471471
return response.json()
472472

473+
@traced(name="assets_exists", run_type="uipath")
474+
def exists(
475+
self,
476+
name: str,
477+
*,
478+
folder_key: Optional[str] = None,
479+
folder_path: Optional[str] = None,
480+
) -> bool:
481+
"""Check whether an asset with the given name exists.
482+
483+
Args:
484+
name (str): The name of the asset.
485+
folder_key (Optional[str]): The key of the folder. Override the default one set in the SDK config.
486+
folder_path (Optional[str]): The path of the folder. Override the default one set in the SDK config.
487+
488+
Returns:
489+
bool: True if an asset with the given name exists, False otherwise.
490+
491+
Examples:
492+
```python
493+
from uipath.platform import UiPath
494+
495+
client = UiPath()
496+
497+
client.assets.exists(name="MyAsset")
498+
```
499+
"""
500+
spec = self._list_spec(
501+
folder_path=folder_path,
502+
folder_key=folder_key,
503+
filter=f"Name eq '{name}'",
504+
orderby=None,
505+
skip=0,
506+
top=1,
507+
)
508+
response = self.request(
509+
spec.method,
510+
url=spec.endpoint,
511+
params=spec.params,
512+
content=spec.content,
513+
headers=spec.headers,
514+
json=spec.json,
515+
)
516+
return len(response.json().get("value", [])) > 0
517+
473518
@property
474519
def custom_headers(self) -> Dict[str, str]:
475520
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: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/uipath/uv.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)