7
7
"""
8
8
9
9
import urllib .parse
10
- from typing import Any , Dict , Optional , Tuple , Union
10
+ from typing import Any , MutableMapping , Optional , Tuple , Union
11
11
12
- from requests import Session
12
+ from httpx import Client
13
13
14
14
from gvm .http .core .response import HttpResponse
15
15
@@ -20,11 +20,18 @@ class HttpApiConnector:
20
20
"""
21
21
22
22
@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
+ ):
24
28
"""
25
- Creates a new session
29
+ Creates a new httpx client
26
30
"""
27
- return Session ()
31
+ return Client (
32
+ verify = server_ca_path if server_ca_path else False ,
33
+ cert = client_cert_paths ,
34
+ )
28
35
29
36
@classmethod
30
37
def url_join (cls , base : str , rel_path : str ) -> str :
@@ -63,29 +70,26 @@ def __init__(
63
70
self .base_url = base_url
64
71
"The base server URL to which request-specific paths will be appended for the requests"
65
72
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
+ )
72
76
73
- def update_headers (self , new_headers : Dict [str , str ]) -> None :
77
+ def update_headers (self , new_headers : MutableMapping [str , str ]) -> None :
74
78
"""
75
79
Updates the headers sent with each request, e.g. for passing an API key
76
80
77
81
Args:
78
- new_headers: Dict containing the new headers
82
+ new_headers: MutableMapping, e.g. dict, containing the new headers
79
83
"""
80
- self ._session .headers .update (new_headers )
84
+ self ._client .headers .update (new_headers )
81
85
82
86
def delete (
83
87
self ,
84
88
rel_path : str ,
85
89
* ,
86
90
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 ,
89
93
) -> HttpResponse :
90
94
"""
91
95
Sends a ``DELETE`` request and returns the response.
@@ -94,14 +98,14 @@ def delete(
94
98
rel_path: The relative path for the request
95
99
raise_for_status: Whether to raise an error if response has a
96
100
non-success HTTP status code
97
- params: Optional dict of URL-encoded parameters
101
+ params: Optional MutableMapping, e.g. dict of URL-encoded parameters
98
102
headers: Optional additional headers added to the request
99
103
100
104
Return:
101
105
The HTTP response.
102
106
"""
103
107
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 )
105
109
if raise_for_status :
106
110
r .raise_for_status ()
107
111
return HttpResponse .from_requests_lib (r )
@@ -111,8 +115,8 @@ def get(
111
115
rel_path : str ,
112
116
* ,
113
117
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 ,
116
120
) -> HttpResponse :
117
121
"""
118
122
Sends a ``GET`` request and returns the response.
@@ -128,7 +132,7 @@ def get(
128
132
The HTTP response.
129
133
"""
130
134
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 )
132
136
if raise_for_status :
133
137
r .raise_for_status ()
134
138
return HttpResponse .from_requests_lib (r )
@@ -139,8 +143,8 @@ def post_json(
139
143
json : Any ,
140
144
* ,
141
145
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 ,
144
148
) -> HttpResponse :
145
149
"""
146
150
Sends a ``POST`` request, using the given JSON-compatible object as the
@@ -151,14 +155,14 @@ def post_json(
151
155
json: The object to use as the request body.
152
156
raise_for_status: Whether to raise an error if response has a
153
157
non-success HTTP status code
154
- params: Optional dict of URL-encoded parameters
158
+ params: Optional MutableMapping, e.g. dict of URL-encoded parameters
155
159
headers: Optional additional headers added to the request
156
160
157
161
Return:
158
162
The HTTP response.
159
163
"""
160
164
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 )
162
166
if raise_for_status :
163
167
r .raise_for_status ()
164
168
return HttpResponse .from_requests_lib (r )
0 commit comments