Skip to content

Commit e3b1d5e

Browse files
committed
Clean up type hints for HTTP APIs
1 parent 10fadd7 commit e3b1d5e

File tree

7 files changed

+38
-21
lines changed

7 files changed

+38
-21
lines changed

gvm/http/core/connector.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
import urllib.parse
10-
from typing import Any, Dict, Optional, Tuple
10+
from typing import Any, Dict, Optional, Tuple, Union
1111

1212
from requests import Session
1313

@@ -44,7 +44,7 @@ def __init__(
4444
base_url: str,
4545
*,
4646
server_ca_path: Optional[str] = None,
47-
client_cert_paths: Optional[str | Tuple[str]] = None,
47+
client_cert_paths: Optional[Union[str, Tuple[str]]] = None,
4848
):
4949
"""
5050
Create a new HTTP API Connector.

gvm/http/core/headers.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"""
88

99
from dataclasses import dataclass
10-
from typing import Dict, Optional, TypeVar
11-
10+
from typing import Dict, Optional, TypeVar, Type, Union
1211

1312
Self = TypeVar("Self", bound="ContentType")
1413

@@ -22,18 +21,18 @@ class ContentType:
2221
media_type: str
2322
'The MIME media type, e.g. "application/json"'
2423

25-
params: Dict[str, str]
24+
params: Dict[str, Union[bool, str]]
2625
"Dictionary of parameters in the content type header"
2726

2827
charset: Optional[str]
2928
"The charset parameter in the content type header if it is set"
3029

3130
@classmethod
3231
def from_string(
33-
cls,
34-
header_string: str,
35-
fallback_media_type: Optional[str] = "application/octet-stream",
36-
) -> Self:
32+
cls: Type[Self],
33+
header_string: Optional[str],
34+
fallback_media_type: str = "application/octet-stream",
35+
) -> "ContentType":
3736
"""
3837
Parse the content of content type header into a ContentType object.
3938
@@ -42,12 +41,13 @@ def from_string(
4241
fallback_media_type: The media type to use if the `header_string` is `None` or empty.
4342
"""
4443
media_type = fallback_media_type
45-
params = {}
44+
params: Dict[str, Union[bool, str]] = {}
4645
charset = None
4746

4847
if header_string:
4948
parts = header_string.split(";")
50-
media_type = parts[0].strip()
49+
if len(parts) > 0:
50+
media_type = parts[0].strip()
5151
for part in parts[1:]:
5252
param = part.strip()
5353
if "=" in param:

gvm/http/core/response.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"""
88

99
from dataclasses import dataclass
10-
from typing import Any, Dict, Optional, TypeVar
10+
from typing import Any, Dict, Optional, TypeVar, Type, Union
1111

1212
from requests import Response
13+
from requests.structures import CaseInsensitiveDict
1314

1415
from gvm.http.core.headers import ContentType
1516

@@ -29,14 +30,14 @@ class HttpResponse:
2930
status: int
3031
"HTTP status code of the response"
3132

32-
headers: Dict[str, str]
33+
headers: Union[Dict[str, str], CaseInsensitiveDict[str]]
3334
"Dict containing the headers of the response"
3435

3536
content_type: Optional[ContentType]
3637
"The content type of the response if it was included in the headers"
3738

3839
@classmethod
39-
def from_requests_lib(cls, r: Response) -> Self:
40+
def from_requests_lib(cls: Type[Self], r: Response) -> "HttpResponse":
4041
"""
4142
Creates a new HTTP response object from a Request object created by the "Requests" library.
4243

gvm/http/openvasd/openvasd1.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
import urllib.parse
10-
from typing import Any, Optional
10+
from typing import Any, Optional, Union
1111

1212
from gvm.errors import InvalidArgumentType
1313
from gvm.http.core._api import GvmHttpApi
@@ -150,7 +150,7 @@ def get_scan_preferences(
150150
def create_scan(
151151
self,
152152
target: dict[str, Any],
153-
vt_selection: dict[str, Any],
153+
vt_selection: list[dict[str, Any]],
154154
scanner_params: Optional[dict[str, Any]] = None,
155155
*,
156156
raise_for_status: bool = False,
@@ -288,7 +288,7 @@ def get_scan_results(
288288
def get_scan_result(
289289
self,
290290
scan_id: str,
291-
result_id: str | int,
291+
result_id: Union[str, int],
292292
*,
293293
raise_for_status: bool = False,
294294
) -> HttpResponse:

poetry.lock

+16-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ python = "^3.9.2"
3333
paramiko = ">=2.7.1"
3434
lxml = ">=4.5.0"
3535
requests = "^2.32.3"
36+
types-requests = "^2.32.0.20250328"
3637

3738
[tool.poetry.group.dev.dependencies]
3839
coverage = ">=7.2"

tests/http/openvasd/test_openvasd1.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def new_mock_empty_response(
2020
if status is None:
2121
status = int(HTTPStatus.NO_CONTENT)
2222
if headers is None:
23-
headers = []
23+
headers = {}
2424
content_type = ContentType.from_string(None)
2525
return HttpResponse(
2626
body=None, status=status, headers=headers, content_type=content_type
@@ -126,7 +126,7 @@ def test_create_scan(self, mock_connector: Mock):
126126
# minimal scan
127127
response = api.create_scan(
128128
{"hosts": "somehost"},
129-
["some_vt", "another_vt"],
129+
[{"oid": "some_vt"}, {"oid": "another_vt"}],
130130
)
131131
mock_connector.post_json.assert_called_once_with(
132132
"/scans",
@@ -142,7 +142,7 @@ def test_create_scan(self, mock_connector: Mock):
142142
mock_connector.post_json.reset_mock()
143143
response = api.create_scan(
144144
{"hosts": "somehost"},
145-
["some_vt", "another_vt"],
145+
[{"oid": "some_vt"}, {"oid": "another_vt"}],
146146
{"my_scanner_param": "abc"},
147147
)
148148
mock_connector.post_json.assert_called_once_with(

0 commit comments

Comments
 (0)