Skip to main content

Tags

Definition

Tags allows the user to group assets based on requirements. This article showcases how to leverage Ion Software Development Kit(Ion SDK) to perform CRUD operations on Tags

API Payload

    {
"name":"Testing Tag", // String
"color": "warning", // String
"description":"created new tag" // String
}

Python Class

class Tag:
"""
A class to instanciate a Tag object.
...
Attributes
----------
id : str
Id of the tag (Provided by the API)
created_by : User
User who created the tag
company : Company
Company to which the tag belongs
name : str
Name of the tag
color : str
Color of the tag
Methods
-------
from_dict(obj: Any) -> Tag
Create a Tag object from a dict
to_dict(self) -> Dict[Any, Any]
Create a dict from a Tag object
"""
id: Optional[str] = None
created_by: Optional[User] = None
company: Optional[Company] = None
name: Optional[str] = None
color: Optional[str] = None
description: Optional[str] = None
created_at: Optional[datetime] = None
updated_at: Optional[datetime] = None
info
  • success: Green
  • brand: Purple
  • blue: Blue
  • warning: Yellow
  • danger: Pink
  • red: Red
  • orange: Orange
  • dark: Black

Client Setup

#Altergo parameters to connect with Client
ALTERGO_FACTORY_API= 'https://altergo.io/' #Default Factory API
ALTERGO_IOT_API= 'https://iot.altergo.io/' #Default IOT API
apiKey = "Your API KEY" #API key of the client

model_name='KB Demo' #Blueprint from which assset is made
serial_number='KB Demo_1'#Unique serial number to identify asset
info

API keys are user bound

from altergo_sdk.models.v2 import Tag
altergoApiKey = apiKey
_client = {
"apiKey": altergoApiKey,
"factory_api": ALTERGO_FACTORY_API,
"iot_api":ALTERGO_IOT_API
}
altergoApi = Client(**_client)

Create

The createTag method of ION SDK enables the user to create tags from a python environment.
This example demonstrates the creation of a tag called KB_tag

# Create Tag payload
tagToCreate = = Tag()
tagToCreate.name = 'Name of tag'
tagToCreate.description = 'Example description for the KB'
tagToCreate.color = 'danger' #options are documented at the top of this page: https://docs.altergo.io/docs/tags

createdTag = newAltergoClient.createTag(newTag= tagToCreate)
print(tagToCreate)

Get

The getTagsByName method of ION SDK allows the user to bring tags created on the platform to the python environment. This method takes in the tag's name as an argument and returns a list of tags with a similar name.

tag = altergoApi.getSingleTagByName('KB_tag') 

You can also get multiple tags sharing the same substring in their name using the getTagsWithSimilarName method

tags=altergoApi.getTagsWithSimilarName('KB_tag') 

Update

The updateTag method takes the Tag object as argument

# get tag to update
tag = altergoApi.getSingleTagByName('KB_tag')
#Update tag name to 'newName'
tag.name = "newName"
response = altergoApi.updatetag(tag)

You can link tags to assets utilizing the linkTagToAssets method of ION SDK. The method takes in Tag object and a list of assets as arguments. This example links (tag) KB_tag to (asset) KB Demo_1

# Get the asset to tag
retrievedAsset = altergoApi.getAssetBySerialNumber(serialNumber = "KB Demo_1")
#Get tag
tag = altergoApi.getSingleTagByName('KB_tag')
# link tag to asset
response = altergoApi.linkTagToAssets( existingTag=tag, assets=[retrievedAsset])

You can remove tags from assets utilizing the unlinkTagFromAssets method of ION SDK. The method takes in Tag object and a list of assets as arguments. This example unlinks (tag) KB_tag to (asset) KB Demo_1

# Get the asset to tag
retrievedAsset = altergoApi.getAssetBySerialNumber(serialNumber = "KB Demo_1")
#Get tag
tag = altergoApi.getSingleTagByName('KB_tag')
# link tag to asset
response = altergoApi.unlinkTagFromAssets( existingTag=tag, assets=[retrievedAsset])

Delete

danger

The deleteTag method deletes the tag PERMANENTLY

The deleteTag takes a Tag object as argument and deletes it from the server


# Fetch the tag to delete

tagToDelete = altergoApi.getTagByName('KB_tag')

response = altergoApi.deleteTag(existingTag = tagToDelete)
info

If a tag is linked to an asset it cannot be deleted