|
| 1 | +# SPDX-FileCopyrightText: 2025 Greenbone AG |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +""" |
| 6 | +API wrapper for accessing the /health endpoints of the openvasd HTTP API. |
| 7 | +""" |
| 8 | + |
| 9 | +import httpx |
| 10 | + |
| 11 | + |
| 12 | +class HealthAPI: |
| 13 | + """ |
| 14 | + Provides access to the openvasd /health endpoints, which expose the |
| 15 | + operational state of the scanner. |
| 16 | +
|
| 17 | + All methods return the HTTP status code of the response and raise an exception |
| 18 | + if the server returns an error response (4xx or 5xx). |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self, client: httpx.Client): |
| 22 | + """ |
| 23 | + Create a new HealthAPI instance. |
| 24 | +
|
| 25 | + Args: |
| 26 | + client: An initialized `httpx.Client` configured for communicating |
| 27 | + with the openvasd server. |
| 28 | + """ |
| 29 | + self._client = client |
| 30 | + |
| 31 | + def get_alive(self, safe: bool = False) -> int: |
| 32 | + """ |
| 33 | + Check if the scanner process is alive. |
| 34 | +
|
| 35 | + Args: |
| 36 | + safe: If True, suppress exceptions and return structured error responses. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + HTTP status code (e.g., 200 if alive). |
| 40 | +
|
| 41 | + Raises: |
| 42 | + httpx.HTTPStatusError: If the server response indicates failure and safe is False. |
| 43 | +
|
| 44 | + See: GET /health/alive in the openvasd API documentation. |
| 45 | + """ |
| 46 | + try: |
| 47 | + response = self._client.get("/health/alive") |
| 48 | + response.raise_for_status() |
| 49 | + return response.status_code |
| 50 | + except httpx.HTTPStatusError as e: |
| 51 | + if safe: |
| 52 | + return e.response.status_code |
| 53 | + raise |
| 54 | + |
| 55 | + def get_ready(self, safe: bool = False) -> int: |
| 56 | + """ |
| 57 | + Check if the scanner is ready to accept requests (e.g., feed loaded). |
| 58 | +
|
| 59 | + Args: |
| 60 | + safe: If True, suppress exceptions and return structured error responses. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + HTTP status code (e.g., 200 if ready). |
| 64 | +
|
| 65 | + Raises: |
| 66 | + httpx.HTTPStatusError: If the server response indicates failure and safe is False. |
| 67 | +
|
| 68 | + See: GET /health/ready in the openvasd API documentation. |
| 69 | + """ |
| 70 | + try: |
| 71 | + response = self._client.get("/health/ready") |
| 72 | + response.raise_for_status() |
| 73 | + return response.status_code |
| 74 | + except httpx.HTTPStatusError as e: |
| 75 | + if safe: |
| 76 | + return e.response.status_code |
| 77 | + raise |
| 78 | + |
| 79 | + def get_started(self, safe: bool = False) -> int: |
| 80 | + """ |
| 81 | + Check if the scanner has fully started. |
| 82 | +
|
| 83 | + Args: |
| 84 | + safe: If True, suppress exceptions and return structured error responses. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + HTTP status code (e.g., 200 if started). |
| 88 | +
|
| 89 | + Raises: |
| 90 | + httpx.HTTPStatusError: If the server response indicates failure and safe is False. |
| 91 | +
|
| 92 | + See: GET /health/started in the openvasd API documentation. |
| 93 | + """ |
| 94 | + try: |
| 95 | + response = self._client.get("/health/started") |
| 96 | + response.raise_for_status() |
| 97 | + return response.status_code |
| 98 | + except httpx.HTTPStatusError as e: |
| 99 | + if safe: |
| 100 | + return e.response.status_code |
| 101 | + raise |
0 commit comments