Skip to content

Commit f791138

Browse files
SSadaiyappan-NISuganth Sadaiyappan
andauthored
feat: Add link files API route in Asset Management Client (#141)
Co-authored-by: Suganth Sadaiyappan <[email protected]>
1 parent d321f00 commit f791138

File tree

7 files changed

+67
-3
lines changed

7 files changed

+67
-3
lines changed

docs/api_reference/assetmanagement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ nisystemlink.clients.assetmanagement
1010
.. automethod:: create_assets
1111
.. automethod:: query_assets
1212
.. automethod:: delete_assets
13+
.. automethod:: link_files
1314

1415
.. automodule:: nisystemlink.clients.assetmanagement.models
1516
:members:

docs/getting_started.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,12 @@ default connection. The default connection depends on your environment.
336336

337337
With a :class:`.AssetManagementClient` object, you can:
338338

339-
* Create, delete and get the list of assets.
339+
* Create, delete, get the list of assets and link files to assets.
340340

341341
Examples
342342
~~~~~~~~
343343

344-
create, delete and query asset
344+
create, delete, query asset and link files to assets.
345345

346346
.. literalinclude:: ../examples/assetmanagement/assets.py
347347
:language: python

examples/assetmanagement/assets.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
)
8585
client.query_assets(query=query_asset_request)
8686

87+
# Link files to the created asset.
88+
file_ids = ["sameple-file-id"]
89+
if created_asset_id:
90+
link_files_response = client.link_files(
91+
asset_id=created_asset_id, file_ids=file_ids
92+
)
93+
8794
# Delete the created asset.
8895
if created_asset_id is not None:
8996
client.delete_assets(ids=[created_asset_id])

nisystemlink/clients/assetmanagement/_asset_management_client.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from nisystemlink.clients.core._http_configuration import HttpConfiguration
77
from nisystemlink.clients.core._uplink._base_client import BaseClient
88
from nisystemlink.clients.core._uplink._methods import post
9-
from uplink import Field, retry
9+
from uplink import Field, Path, retry
1010

1111
from . import models
1212

@@ -122,3 +122,22 @@ def delete_assets(self, ids: List[str]) -> models.DeleteAssetsResponse:
122122
ApiException: If unable to communicate with the asset management service or if there are invalid arguments.
123123
"""
124124
...
125+
126+
@post("assets/{assetId}/file", args=[Path("assetId"), Field("fileIds")])
127+
def link_files(
128+
self, asset_id: str, file_ids: List[str]
129+
) -> Optional[models.LinkFilesPartialSuccessResponse]:
130+
"""Link files to an asset.
131+
132+
Args:
133+
asset_id: The ID of the asset to which files should be linked.
134+
file_ids: The list of file IDs to link.
135+
136+
Returns:
137+
None if all link files succeed, otherwise a response containing the IDs of files that were
138+
successfully linked and those that failed.
139+
140+
Raises:
141+
ApiException: If unable to communicate with the asset management service or if there are invalid arguments.
142+
"""
143+
...

nisystemlink/clients/assetmanagement/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
ExternalCalibration,
2121
)
2222
from ._asset_types import AssetBusType, AssetDiscoveryType, AssetType
23+
from ._link_files_partial_success_response import LinkFilesPartialSuccessResponse
2324

2425
# flake8: noqa
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List, Optional
2+
3+
from nisystemlink.clients.core._api_error import ApiError
4+
from nisystemlink.clients.core._uplink._json_model import JsonModel
5+
6+
7+
class LinkFilesPartialSuccessResponse(JsonModel):
8+
"""Model for a Link Files Partial Success Response."""
9+
10+
succeeded: Optional[List[str]] = None
11+
"""Gets or sets array of file IDs that were successfully linked to assets."""
12+
13+
failed: Optional[List[str]] = None
14+
"""Gets or sets array of file IDs that failed to link to assets."""
15+
16+
error: Optional[ApiError] = None
17+
"""Gets or sets error if the link file operation failed."""

tests/integration/assetmanagement/test_asset_management.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ def test__create_asset__returns_created_asset(
123123
asset_id = assets[0].id
124124
assert asset_id is not None
125125

126+
def test__link_files__link_succeded(
127+
self, client: AssetManagementClient, create_asset
128+
):
129+
self._create_assets_request[0].model_number = 101
130+
create_response = create_asset(self._create_assets_request)
131+
132+
asset_id = (
133+
create_response.assets[0].id
134+
if create_response.assets and create_response.assets[0].id
135+
else None
136+
)
137+
138+
assert asset_id is not None
139+
140+
file_ids = ["608a5684800e325b48837c2a"]
141+
link_files_response = client.link_files(asset_id=asset_id, file_ids=file_ids)
142+
143+
assert link_files_response is None
144+
126145
def test__delete_asset__returns_deleted_asset(self, client: AssetManagementClient):
127146
self._create_assets_request[0].model_number = 102
128147
create_response: CreateAssetsPartialSuccessResponse = client.create_assets(

0 commit comments

Comments
 (0)