|
| 1 | +from typing import AsyncIterator |
| 2 | + |
| 3 | +import universalasync |
| 4 | + |
| 5 | +from armis_sdk.core import response_utils |
| 6 | +from armis_sdk.core.armis_error import ArmisError |
| 7 | +from armis_sdk.core.base_entity_client import BaseEntityClient |
| 8 | +from armis_sdk.entities.device_custom_property import DeviceCustomProperty |
| 9 | + |
| 10 | + |
| 11 | +@universalasync.wrap |
| 12 | +class DeviceCustomPropertiesClient(BaseEntityClient): |
| 13 | + """ |
| 14 | + A client for interacting with device custom properties. |
| 15 | +
|
| 16 | + The primary entity for this client is |
| 17 | + [DeviceCustomProperty][armis_sdk.entities.device_custom_property.DeviceCustomProperty]. |
| 18 | + """ |
| 19 | + |
| 20 | + async def create(self, property_: DeviceCustomProperty) -> DeviceCustomProperty: |
| 21 | + # pylint: disable=line-too-long |
| 22 | + """Create a `DeviceCustomProperty`. |
| 23 | +
|
| 24 | + Args: |
| 25 | + property_: The `DeviceCustomProperty` to create. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + The same property as the input with the addition of id. |
| 29 | +
|
| 30 | + Example: |
| 31 | + Example: |
| 32 | + ```python linenums="1" hl_lines="10" |
| 33 | + import asyncio |
| 34 | +
|
| 35 | + from armis_sdk.clients.device_custom_properties_client import DeviceCustomPropertiesClient |
| 36 | + from armis_sdk.entities.device_custom_property import DeviceCustomProperty |
| 37 | +
|
| 38 | +
|
| 39 | + async def main(): |
| 40 | + client = DeviceCustomPropertiesClient() |
| 41 | + property_ = DeviceCustomProperty(name="MyConfig", type="string") |
| 42 | + print(await client.create(property_)) |
| 43 | +
|
| 44 | + asyncio.run(main()) |
| 45 | + ``` |
| 46 | + Will output: |
| 47 | + ```python linenums="1" |
| 48 | + DeviceCustomPropertiesClient(id=1, name="MyConfig", type="string") |
| 49 | + ``` |
| 50 | + """ |
| 51 | + if property_.id is not None: |
| 52 | + raise ArmisError( |
| 53 | + "Can't create a property that already has an id. " |
| 54 | + "Did you mean to call `.update(property_)`?", |
| 55 | + ) |
| 56 | + |
| 57 | + if not property_.name: |
| 58 | + raise ArmisError("Can't create a property without a name.") |
| 59 | + |
| 60 | + if not property_.type: |
| 61 | + raise ArmisError("Can't create a property without a type.") |
| 62 | + |
| 63 | + payload = property_.model_dump(exclude_none=True) |
| 64 | + |
| 65 | + async with self._armis_client.client() as client: |
| 66 | + response = await client.post( |
| 67 | + "/v3/settings/device-custom-properties", json=payload |
| 68 | + ) |
| 69 | + data = response_utils.get_data_dict(response) |
| 70 | + return DeviceCustomProperty.model_validate(data) |
| 71 | + |
| 72 | + async def delete(self, property_: DeviceCustomProperty): |
| 73 | + # pylint: disable=line-too-long |
| 74 | + """Delete a `DeviceCustomProperty`. |
| 75 | +
|
| 76 | + Args: |
| 77 | + property_: The `DeviceCustomProperty` to delete. |
| 78 | +
|
| 79 | + Example: |
| 80 | + Example: |
| 81 | + ```python linenums="1" hl_lines="10" |
| 82 | + import asyncio |
| 83 | +
|
| 84 | + from armis_sdk.clients.device_custom_properties_client import DeviceCustomPropertiesClient |
| 85 | + from armis_sdk.entities.device_custom_property import DeviceCustomProperty |
| 86 | +
|
| 87 | +
|
| 88 | + async def main(): |
| 89 | + client = DeviceCustomPropertiesClient() |
| 90 | + property_ = DeviceCustomProperty(id=1, name="MyConfig", type="string") |
| 91 | + await client.delete(property_) |
| 92 | +
|
| 93 | + asyncio.run(main()) |
| 94 | + ``` |
| 95 | + """ |
| 96 | + if property_.id is None: |
| 97 | + raise ArmisError("Can't delete a property without an id.") |
| 98 | + |
| 99 | + async with self._armis_client.client() as client: |
| 100 | + response = await client.delete( |
| 101 | + f"/v3/settings/device-custom-properties/{property_.id}" |
| 102 | + ) |
| 103 | + response_utils.raise_for_status(response) |
| 104 | + |
| 105 | + async def get(self, property_id: int) -> DeviceCustomProperty: |
| 106 | + # pylint: disable=line-too-long |
| 107 | + """Get a `DeviceCustomProperty` by its ID. |
| 108 | +
|
| 109 | + Args: |
| 110 | + property_id: The ID of the `DeviceCustomProperty` to get. |
| 111 | +
|
| 112 | + Returns: |
| 113 | + A `DeviceCustomProperty` object. |
| 114 | +
|
| 115 | + Example: |
| 116 | + Example: |
| 117 | + ```python linenums="1" hl_lines="8" |
| 118 | + import asyncio |
| 119 | +
|
| 120 | + from armis_sdk.clients.device_custom_properties_client import DeviceCustomPropertiesClient |
| 121 | +
|
| 122 | +
|
| 123 | + async def main(): |
| 124 | + client = DeviceCustomPropertiesClient() |
| 125 | + print(await client.get(1)) |
| 126 | +
|
| 127 | + asyncio.run(main()) |
| 128 | + ``` |
| 129 | + Will output: |
| 130 | + ```python linenums="1" |
| 131 | + DeviceCustomPropertiesClient(id=1, name="MyConfig", type="string") |
| 132 | + ``` |
| 133 | + """ |
| 134 | + async with self._armis_client.client() as client: |
| 135 | + response = await client.get( |
| 136 | + f"/v3/settings/device-custom-properties/{property_id}" |
| 137 | + ) |
| 138 | + data = response_utils.get_data_dict(response) |
| 139 | + return DeviceCustomProperty.model_validate(data) |
| 140 | + |
| 141 | + async def list(self) -> AsyncIterator[DeviceCustomProperty]: |
| 142 | + # pylint: disable=line-too-long |
| 143 | + """List all the tenant's `DeviceCustomProperty`s. |
| 144 | + This method takes care of pagination, so you don't have to deal with it. |
| 145 | +
|
| 146 | + Returns: |
| 147 | + An (async) iterator of `DeviceCustomProperty` object. |
| 148 | +
|
| 149 | + Example: |
| 150 | + ```python linenums="1" hl_lines="8" |
| 151 | + import asyncio |
| 152 | +
|
| 153 | + from armis_sdk.clients.device_custom_properties_client import DeviceCustomPropertiesClient |
| 154 | +
|
| 155 | +
|
| 156 | + async def main(): |
| 157 | + client = DeviceCustomPropertiesClient() |
| 158 | + async for property_ in client.list() |
| 159 | + print(property_) |
| 160 | +
|
| 161 | + asyncio.run(main()) |
| 162 | + ``` |
| 163 | + Will output: |
| 164 | + ```python linenums="1" |
| 165 | + DeviceCustomPropertiesClient(id=1, name="MyConfig", type="string") |
| 166 | + DeviceCustomPropertiesClient(id=1, name="MyOtherConfig", type="integer") |
| 167 | + ``` |
| 168 | + """ |
| 169 | + |
| 170 | + async with self._armis_client.client() as client: |
| 171 | + # endpoint doesn't support paging |
| 172 | + response = await client.get("/v3/settings/device-custom-properties") |
| 173 | + data = response_utils.get_data_dict(response) |
| 174 | + for item in data["items"]: |
| 175 | + yield DeviceCustomProperty.model_validate(item) |
| 176 | + |
| 177 | + async def update(self, property_: DeviceCustomProperty) -> DeviceCustomProperty: |
| 178 | + # pylint: disable=line-too-long |
| 179 | + """Update a `DeviceCustomProperty`. |
| 180 | + Only `description` and `allowed_values` are updatable. |
| 181 | +
|
| 182 | + Args: |
| 183 | + property_: The `DeviceCustomProperty` to update. |
| 184 | +
|
| 185 | + Raises: |
| 186 | + ResponseError: If an error occurs while communicating with the API. |
| 187 | +
|
| 188 | + Example: |
| 189 | + ```python linenums="1" hl_lines="15" |
| 190 | + import asyncio |
| 191 | +
|
| 192 | + from armis_sdk.clients.device_custom_properties_client import DeviceCustomPropertiesClient |
| 193 | + from armis_sdk.entities.device_custom_property import DeviceCustomProperty |
| 194 | +
|
| 195 | +
|
| 196 | + async def main(): |
| 197 | + client = DeviceCustomPropertiesClient() |
| 198 | + property_ = DeviceCustomProperty( |
| 199 | + id=1, |
| 200 | + name="MyConfig", |
| 201 | + type="string", |
| 202 | + description="New description", |
| 203 | + ) |
| 204 | + await client.update(property_) |
| 205 | +
|
| 206 | + asyncio.run(main()) |
| 207 | + ``` |
| 208 | + """ |
| 209 | + if property_.id is None: |
| 210 | + raise ArmisError( |
| 211 | + "Can't update a property without an id. " |
| 212 | + "Did you mean to call `.create(property_)`?", |
| 213 | + ) |
| 214 | + |
| 215 | + data = property_.model_dump( |
| 216 | + exclude={"id", "name", "creation_time", "created_by", "type"}, |
| 217 | + exclude_none=True, |
| 218 | + ) |
| 219 | + |
| 220 | + if not data: |
| 221 | + return property_ |
| 222 | + |
| 223 | + async with self._armis_client.client() as client: |
| 224 | + response = await client.patch( |
| 225 | + f"/v3/settings/device-custom-properties/{property_.id}", |
| 226 | + json=data, |
| 227 | + ) |
| 228 | + response_utils.raise_for_status(response) |
| 229 | + |
| 230 | + return property_ |
0 commit comments