from datetime import datetime
from typing import TYPE_CHECKING, List
from tdw_catalog import Catalog
from tdw_catalog.entity import Entity, Property, EntityBase
from tdw_catalog.errors import _convert_error, _raise_error
from tdw_catalog.relations import _OrganizationRelation
if TYPE_CHECKING:
import tdw_catalog.organization as organization
[docs]@Entity([
Property("id", str, serialize=True),
Property("organization_id",
str,
relation="tdw_catalog.organization.Organization",
serialize=True),
Property("title", str, writable=True),
Property("created_at", datetime),
Property("updated_at", datetime),
])
class Topic(EntityBase, _OrganizationRelation):
"""
:class:`.Topic`\\ s are used to classify Datasets within an :class:`.Organization`.
Classification can be used as a means to apply a grouping label
to one or more :class:`.Dataset`\\ s.
Attributes
----------
id : str
Topic's unique id
organization_id : str
The unique ID of the :class:`.Organization` to which this :class:`.Topic` belongs
created_by : str
The unique user ID of the user who created this :class:`.Topic`
title : str
The title for this :class:`.Topic`
created_at : datetime
The datetime at which this :class:`.Topic` was created
updated_at : datetime
The datetime at which this :class:`.Topic` was last updated
"""
_client: 'Catalog'
id: str
title: str
organization: 'organization.Organization'
organization_id: str
created_by: str
created_at: datetime
updated_at: datetime
def __str__(self) -> str:
return f"<Topic id={self.id} title={self.title}>"
[docs] @classmethod
def get(cls, client: 'Catalog', organization_id: str, id: str):
"""
Retrieve a :class:`.Topic`
Parameters
----------
client : Catalog
The :class:`.Catalog` client to use to get the :class:`.Topic`
organization_id : str
The ID of the :class:`.Organization` in which this :class:`.Topic` exists
id : str
The unique ID of the :class:`.Topic`
Returns
-------
Topic
The :class:`.Topic` associated with the given ID
Raises
------
CatalogPermissionDeniedException
If the caller is not allowed to retrieve the given :class:`.Topic`
CatalogNotFoundException
If the given :class:`.Topic` ID does not exist
CatalogException
If call to the :class:`.Catalog` server fails
"""
try:
res = client._get_topic(organization_id=organization_id, id=id)
return cls(client, **res)
except Exception as e:
err = _raise_error(e, "Unable to fetch Topic {}".format(id))
[docs] def save(self) -> None:
"""
Update this :class:`.Topic`, saving any changes to its title
Parameters
----------
None
Returns
-------
None
Raises
------
CatalogPermissionDeniedException
If the caller is not allowed to update this :class:`.Topic`, or if the given :class:`.Topic` ID does not exist
CatalogException
If call to the :class:`.Catalog` server fails
"""
try:
res = self._client._update_topic(topic=self.serialize())
self.deserialize(res)
except Exception as e:
raise _convert_error(e)
[docs] def delete(self) -> None:
"""
Delete this :class:`.Topic`. This :class:`.Topic` object should not be
used after `delete()` has successfully returned
Parameters
----------
None
Returns
-------
None
Raises
------
CatalogPermissionDeniedException
If the caller is not allowed to delete this :class:`.Topic`, or if the given :class:`.Topic` ID does not exist
CatalogException
If call to the :class:`.Catalog` server fails
"""
try:
self._client._delete_topic(topic=self.serialize())
except Exception as e:
raise _convert_error(e)