6
6
API wrapper for retrieving metadata from the openvasd HTTP API using HEAD requests.
7
7
"""
8
8
9
+ from dataclasses import dataclass
9
10
from typing import Union
10
11
11
12
import httpx
12
13
13
14
from ._api import OpenvasdAPI
14
15
15
16
17
+ @dataclass
18
+ class Metadata :
19
+ """
20
+ Represents metadata returned by the openvasd API.
21
+
22
+ Attributes:
23
+ api_version: Comma separated list of available API versions
24
+ feed_version: Version of the feed.
25
+ authentication: Supported authentication methods
26
+ """
27
+
28
+ api_version : str
29
+ feed_version : str
30
+ authentication : str
31
+
32
+
33
+ @dataclass
34
+ class MetadataError :
35
+ """
36
+ Represents an error response from the metadata API.
37
+
38
+ Attributes:
39
+ error: Error message.
40
+ status_code: HTTP status code of the error response.
41
+ """
42
+
43
+ error : str
44
+ status_code : int
45
+
46
+
16
47
class MetadataAPI (OpenvasdAPI ):
17
48
"""
18
49
Provides access to metadata endpoints exposed by the openvasd server
@@ -27,20 +58,12 @@ class MetadataAPI(OpenvasdAPI):
27
58
is handled gracefully.
28
59
"""
29
60
30
- def get (self ) -> dict [ str , Union [str , int ] ]:
61
+ def get (self ) -> Union [Metadata , MetadataError ]:
31
62
"""
32
63
Perform a HEAD request to `/` to retrieve top-level API metadata.
33
64
34
65
Returns:
35
- A dictionary containing:
36
-
37
- - "api-version"
38
- - "feed-version"
39
- - "authentication"
40
-
41
- Or if exceptions are suppressed and error occurs:
42
-
43
- - {"error": str, "status_code": int}
66
+ A Metadata instance or MetadataError if exceptions are suppressed and an error occurs.
44
67
45
68
Raises:
46
69
httpx.HTTPStatusError: For non-401 HTTP errors if exceptions are not suppressed.
@@ -50,30 +73,24 @@ def get(self) -> dict[str, Union[str, int]]:
50
73
try :
51
74
response = self ._client .head ("/" )
52
75
response .raise_for_status ()
53
- return {
54
- "api-version" : response .headers .get ("api-version" ),
55
- "feed-version" : response .headers .get ("feed-version" ),
56
- " authentication" : response .headers .get ("authentication" ),
57
- }
76
+ return Metadata (
77
+ api_version = response .headers .get ("api-version" ),
78
+ feed_version = response .headers .get ("feed-version" ),
79
+ authentication = response .headers .get ("authentication" ),
80
+ )
58
81
except httpx .HTTPStatusError as e :
59
82
if self ._suppress_exceptions :
60
- return {"error" : str (e ), "status_code" : e .response .status_code }
83
+ return MetadataError (
84
+ error = str (e ), status_code = e .response .status_code
85
+ )
61
86
raise
62
87
63
- def get_scans (self ) -> dict [ str , Union [str , int ] ]:
88
+ def get_scans (self ) -> Union [Metadata , MetadataError ]:
64
89
"""
65
- Perform a HEAD request to `/scans` to retrieve scan endpoint metadata.
90
+ Perform a HEAD request to `/scans` to retrieve scan endpoint metadata.
66
91
67
92
Returns:
68
- A dictionary containing:
69
-
70
- - "api-version"
71
- - "feed-version"
72
- - "authentication"
73
-
74
- Or if safe=True and error occurs:
75
-
76
- - {"error": str, "status_code": int}
93
+ A Metadata instance or MetadataError if exceptions are suppressed and an error occurs.
77
94
78
95
Raises:
79
96
httpx.HTTPStatusError: For non-401 HTTP errors if exceptions are not suppressed.
@@ -83,12 +100,14 @@ def get_scans(self) -> dict[str, Union[str, int]]:
83
100
try :
84
101
response = self ._client .head ("/scans" )
85
102
response .raise_for_status ()
86
- return {
87
- "api-version" : response .headers .get ("api-version" ),
88
- "feed-version" : response .headers .get ("feed-version" ),
89
- " authentication" : response .headers .get ("authentication" ),
90
- }
103
+ return Metadata (
104
+ api_version = response .headers .get ("api-version" ),
105
+ feed_version = response .headers .get ("feed-version" ),
106
+ authentication = response .headers .get ("authentication" ),
107
+ )
91
108
except httpx .HTTPStatusError as e :
92
109
if self ._suppress_exceptions :
93
- return {"error" : str (e ), "status_code" : e .response .status_code }
110
+ return MetadataError (
111
+ error = str (e ), status_code = e .response .status_code
112
+ )
94
113
raise
0 commit comments