|
| 1 | +import contextlib |
| 2 | +from typing import AsyncIterator |
| 3 | +from typing import Generator |
| 4 | +from typing import IO |
| 5 | +from typing import Union |
| 6 | + |
| 7 | +import httpx |
| 8 | +import universalasync |
| 9 | + |
| 10 | +from armis_sdk.core import response_utils |
| 11 | +from armis_sdk.core.base_entity_client import BaseEntityClient |
| 12 | +from armis_sdk.entities.collector_image import CollectorImage |
| 13 | +from armis_sdk.entities.download_progress import DownloadProgress |
| 14 | +from armis_sdk.enums.collector_image_type import CollectorImageType |
| 15 | + |
| 16 | + |
| 17 | +@universalasync.wrap |
| 18 | +class CollectorsClient(BaseEntityClient): |
| 19 | + # pylint: disable=line-too-long |
| 20 | + """ |
| 21 | + A client for interacting with Armis collectors. |
| 22 | +
|
| 23 | + The primary entity for this client is [CollectorImage][armis_sdk.entities.collector_image.CollectorImage]. |
| 24 | + """ |
| 25 | + |
| 26 | + async def download_image( |
| 27 | + self, |
| 28 | + destination: Union[str, IO[bytes]], |
| 29 | + image_type: CollectorImageType = CollectorImageType.OVA, |
| 30 | + ) -> AsyncIterator[DownloadProgress]: |
| 31 | + """Download a collector image to a specified destination path / file. |
| 32 | +
|
| 33 | + Args: |
| 34 | + destination: The file path or file-like object where the collector image will be saved. |
| 35 | + image_type: The type of collector image to download. Defaults to "OVA". |
| 36 | +
|
| 37 | + Returns: |
| 38 | + An (async) iterator of `DownloadProgress` object. |
| 39 | +
|
| 40 | + Example: |
| 41 | + ```python linenums="1" hl_lines="10 15" |
| 42 | + import asyncio |
| 43 | +
|
| 44 | + from armis_sdk.clients.collectors_client import CollectorsClient |
| 45 | +
|
| 46 | +
|
| 47 | + async def main(): |
| 48 | + collectors_client = CollectorsClient() |
| 49 | +
|
| 50 | + # Download to a path |
| 51 | + async for progress in armis_sdk.collectors.download_image("/tmp/collector.ova"): |
| 52 | + print(progress.percent) |
| 53 | +
|
| 54 | + # Download to a file |
| 55 | + with open("/tmp/collector.ova", "wb") as file: |
| 56 | + async for progress in armis_sdk.collectors.download_image(file): |
| 57 | + print(progress.percent) |
| 58 | +
|
| 59 | + asyncio.run(main()) |
| 60 | + ``` |
| 61 | + Will output: |
| 62 | + ```python linenums="1" |
| 63 | + 1% |
| 64 | + 2% |
| 65 | + 3% |
| 66 | + ``` |
| 67 | + etc. |
| 68 | + """ |
| 69 | + collector_image = await self.get_image(image_type=image_type) |
| 70 | + async with httpx.AsyncClient() as client: |
| 71 | + async with client.stream("GET", collector_image.url) as response: |
| 72 | + response.raise_for_status() |
| 73 | + total_size = int(response.headers.get("Content-Length", "0")) |
| 74 | + with self.open_file(destination) as file: |
| 75 | + async for chunk in response.aiter_bytes(): |
| 76 | + file.write(chunk) |
| 77 | + yield DownloadProgress(downloaded=file.tell(), total=total_size) |
| 78 | + |
| 79 | + async def get_image( |
| 80 | + self, image_type: CollectorImageType = CollectorImageType.OVA |
| 81 | + ) -> CollectorImage: |
| 82 | + """Get collector image information including download URL and credentials. |
| 83 | +
|
| 84 | + Args: |
| 85 | + image_type: The type of collector image to retrieve. Defaults to "OVA". |
| 86 | +
|
| 87 | + Returns: |
| 88 | + A `CollectorImage` object. |
| 89 | +
|
| 90 | + Example: |
| 91 | + ```python linenums="1" hl_lines="8" |
| 92 | + import asyncio |
| 93 | +
|
| 94 | + from armis_sdk.clients.collectors_client import CollectorsClient |
| 95 | +
|
| 96 | +
|
| 97 | + async def main(): |
| 98 | + collectors_client = CollectorsClient() |
| 99 | + print(await collectors_client.get_image(image_type="OVA")) |
| 100 | +
|
| 101 | + asyncio.run(main()) |
| 102 | + ``` |
| 103 | + Will output: |
| 104 | + ```python linenums="1" |
| 105 | + CollectorImage(url="...", ...) |
| 106 | + ``` |
| 107 | + """ |
| 108 | + async with self._armis_client.client() as client: |
| 109 | + response = await client.get( |
| 110 | + "/v3/collectors/_image", params={"image_type": image_type.value} |
| 111 | + ) |
| 112 | + data = response_utils.get_data_dict(response) |
| 113 | + return CollectorImage.model_validate(data) |
| 114 | + |
| 115 | + @classmethod |
| 116 | + @contextlib.contextmanager |
| 117 | + def open_file( |
| 118 | + cls, destination: Union[str, IO[bytes]] |
| 119 | + ) -> Generator[IO[bytes], None, None]: |
| 120 | + if isinstance(destination, str): |
| 121 | + with open(destination, "wb") as file: |
| 122 | + yield file |
| 123 | + else: |
| 124 | + yield destination |
0 commit comments