Skip to content

Commit 9d3da00

Browse files
committed
Use httpx library instead of requests
1 parent ec59911 commit 9d3da00

File tree

5 files changed

+186
-156
lines changed

5 files changed

+186
-156
lines changed

gvm/http/core/connector.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"""
88

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

12-
from requests import Session
12+
from httpx import Client
1313

1414
from gvm.http.core.response import HttpResponse
1515

@@ -20,11 +20,18 @@ class HttpApiConnector:
2020
"""
2121

2222
@classmethod
23-
def _new_session(cls):
23+
def _new_client(
24+
cls,
25+
server_ca_path: Optional[str] = None,
26+
client_cert_paths: Optional[Union[str, Tuple[str]]] = None,
27+
):
2428
"""
25-
Creates a new session
29+
Creates a new httpx client
2630
"""
27-
return Session()
31+
return Client(
32+
verify=server_ca_path if server_ca_path else False,
33+
cert=client_cert_paths,
34+
)
2835

2936
@classmethod
3037
def url_join(cls, base: str, rel_path: str) -> str:
@@ -63,29 +70,26 @@ def __init__(
6370
self.base_url = base_url
6471
"The base server URL to which request-specific paths will be appended for the requests"
6572

66-
self._session = self._new_session()
67-
"Internal session handling the HTTP requests"
68-
if server_ca_path:
69-
self._session.verify = server_ca_path
70-
if client_cert_paths:
71-
self._session.cert = client_cert_paths
73+
self._client: Client = self._new_client(
74+
server_ca_path, client_cert_paths
75+
)
7276

73-
def update_headers(self, new_headers: Dict[str, str]) -> None:
77+
def update_headers(self, new_headers: MutableMapping[str, str]) -> None:
7478
"""
7579
Updates the headers sent with each request, e.g. for passing an API key
7680
7781
Args:
78-
new_headers: Dict containing the new headers
82+
new_headers: MutableMapping, e.g. dict, containing the new headers
7983
"""
80-
self._session.headers.update(new_headers)
84+
self._client.headers.update(new_headers)
8185

8286
def delete(
8387
self,
8488
rel_path: str,
8589
*,
8690
raise_for_status: bool = True,
87-
params: Optional[Dict[str, str]] = None,
88-
headers: Optional[Dict[str, str]] = None,
91+
params: Optional[MutableMapping[str, str]] = None,
92+
headers: Optional[MutableMapping[str, str]] = None,
8993
) -> HttpResponse:
9094
"""
9195
Sends a ``DELETE`` request and returns the response.
@@ -94,14 +98,14 @@ def delete(
9498
rel_path: The relative path for the request
9599
raise_for_status: Whether to raise an error if response has a
96100
non-success HTTP status code
97-
params: Optional dict of URL-encoded parameters
101+
params: Optional MutableMapping, e.g. dict of URL-encoded parameters
98102
headers: Optional additional headers added to the request
99103
100104
Return:
101105
The HTTP response.
102106
"""
103107
url = self.url_join(self.base_url, rel_path)
104-
r = self._session.delete(url, params=params, headers=headers)
108+
r = self._client.delete(url, params=params, headers=headers)
105109
if raise_for_status:
106110
r.raise_for_status()
107111
return HttpResponse.from_requests_lib(r)
@@ -111,8 +115,8 @@ def get(
111115
rel_path: str,
112116
*,
113117
raise_for_status: bool = True,
114-
params: Optional[Dict[str, str]] = None,
115-
headers: Optional[Dict[str, str]] = None,
118+
params: Optional[MutableMapping[str, str]] = None,
119+
headers: Optional[MutableMapping[str, str]] = None,
116120
) -> HttpResponse:
117121
"""
118122
Sends a ``GET`` request and returns the response.
@@ -128,7 +132,7 @@ def get(
128132
The HTTP response.
129133
"""
130134
url = self.url_join(self.base_url, rel_path)
131-
r = self._session.get(url, params=params, headers=headers)
135+
r = self._client.get(url, params=params, headers=headers)
132136
if raise_for_status:
133137
r.raise_for_status()
134138
return HttpResponse.from_requests_lib(r)
@@ -139,8 +143,8 @@ def post_json(
139143
json: Any,
140144
*,
141145
raise_for_status: bool = True,
142-
params: Optional[Dict[str, str]] = None,
143-
headers: Optional[Dict[str, str]] = None,
146+
params: Optional[MutableMapping[str, str]] = None,
147+
headers: Optional[MutableMapping[str, str]] = None,
144148
) -> HttpResponse:
145149
"""
146150
Sends a ``POST`` request, using the given JSON-compatible object as the
@@ -151,14 +155,14 @@ def post_json(
151155
json: The object to use as the request body.
152156
raise_for_status: Whether to raise an error if response has a
153157
non-success HTTP status code
154-
params: Optional dict of URL-encoded parameters
158+
params: Optional MutableMapping, e.g. dict of URL-encoded parameters
155159
headers: Optional additional headers added to the request
156160
157161
Return:
158162
The HTTP response.
159163
"""
160164
url = self.url_join(self.base_url, rel_path)
161-
r = self._session.post(url, json=json, params=params, headers=headers)
165+
r = self._client.post(url, json=json, params=params, headers=headers)
162166
if raise_for_status:
163167
r.raise_for_status()
164168
return HttpResponse.from_requests_lib(r)

gvm/http/core/response.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
"""
88

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

12-
from requests import Response
13-
from requests.structures import CaseInsensitiveDict
12+
from httpx import Response
1413

1514
from gvm.http.core.headers import ContentType
1615

@@ -29,7 +28,7 @@ class HttpResponse:
2928
status: int
3029
"HTTP status code of the response"
3130

32-
headers: Union[Dict[str, str], CaseInsensitiveDict[str]]
31+
headers: MutableMapping[str, str]
3332
"Dict containing the headers of the response"
3433

3534
content_type: Optional[ContentType]

poetry.lock

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ packages = [{ include = "gvm" }, { include = "tests", format = "sdist" }]
3232
python = "^3.9.2"
3333
paramiko = ">=2.7.1"
3434
lxml = ">=4.5.0"
35-
requests = "^2.32.3"
35+
httpx = "^0.28.1"
3636

3737
[tool.poetry.group.dev.dependencies]
3838
coverage = ">=7.2"

0 commit comments

Comments
 (0)