28
28
except ImportError : # pragma: no cover
29
29
from urlparse import urljoin
30
30
31
- import requests
32
- from requests .adapters import HTTPAdapter
31
+ import httpx
33
32
34
33
from .exceptions import KeycloakConnectionError
35
34
@@ -67,26 +66,19 @@ def __init__(self, base_url, headers={}, timeout=60, verify=True, proxies=None):
67
66
self .headers = headers
68
67
self .timeout = timeout
69
68
self .verify = verify
70
- self ._s = requests . Session ( )
69
+ self ._s = httpx . AsyncClient ( verify = verify )
71
70
self ._s .auth = lambda x : x # don't let requests add auth headers
72
71
73
72
# retry once to reset connection with Keycloak after tomcat's ConnectionTimeout
74
73
# see https://github.com/marcospereirampj/python-keycloak/issues/36
75
- for protocol in ("https://" , "http://" ):
76
- adapter = HTTPAdapter (max_retries = 1 )
77
- # adds POST to retry whitelist
78
- allowed_methods = set (adapter .max_retries .allowed_methods )
79
- allowed_methods .add ("POST" )
80
- adapter .max_retries .allowed_methods = frozenset (allowed_methods )
81
-
82
- self ._s .mount (protocol , adapter )
74
+ self ._s .transport = httpx .AsyncHTTPTransport (retries = 1 )
83
75
84
76
if proxies :
85
77
self ._s .proxies .update (proxies )
86
78
87
- def __del__ (self ):
79
+ async def close (self ):
88
80
"""Del method."""
89
- self ._s .close ()
81
+ await self ._s .aclose ()
90
82
91
83
@property
92
84
def base_url (self ):
@@ -182,7 +174,7 @@ def del_param_headers(self, key):
182
174
"""
183
175
self .headers .pop (key , None )
184
176
185
- def raw_get (self , path , ** kwargs ):
177
+ async def raw_get (self , path , ** kwargs ):
186
178
"""Submit get request to the path.
187
179
188
180
:param path: Path for request.
@@ -194,17 +186,17 @@ def raw_get(self, path, **kwargs):
194
186
:raises KeycloakConnectionError: HttpError Can't connect to server.
195
187
"""
196
188
try :
197
- return self ._s .get (
189
+ return await self ._s .request (
190
+ "GET" ,
198
191
urljoin (self .base_url , path ),
199
192
params = kwargs ,
200
193
headers = self .headers ,
201
194
timeout = self .timeout ,
202
- verify = self .verify ,
203
195
)
204
196
except Exception as e :
205
197
raise KeycloakConnectionError ("Can't connect to server (%s)" % e )
206
198
207
- def raw_post (self , path , data , ** kwargs ):
199
+ async def raw_post (self , path , data , ** kwargs ):
208
200
"""Submit post request to the path.
209
201
210
202
:param path: Path for request.
@@ -218,18 +210,17 @@ def raw_post(self, path, data, **kwargs):
218
210
:raises KeycloakConnectionError: HttpError Can't connect to server.
219
211
"""
220
212
try :
221
- return self ._s .post (
213
+ return await self ._s .post (
222
214
urljoin (self .base_url , path ),
223
215
params = kwargs ,
224
216
data = data ,
225
217
headers = self .headers ,
226
218
timeout = self .timeout ,
227
- verify = self .verify ,
228
219
)
229
220
except Exception as e :
230
221
raise KeycloakConnectionError ("Can't connect to server (%s)" % e )
231
222
232
- def raw_put (self , path , data , ** kwargs ):
223
+ async def raw_put (self , path , data , ** kwargs ):
233
224
"""Submit put request to the path.
234
225
235
226
:param path: Path for request.
@@ -243,18 +234,17 @@ def raw_put(self, path, data, **kwargs):
243
234
:raises KeycloakConnectionError: HttpError Can't connect to server.
244
235
"""
245
236
try :
246
- return self ._s .put (
237
+ return await self ._s .put (
247
238
urljoin (self .base_url , path ),
248
239
params = kwargs ,
249
240
data = data ,
250
241
headers = self .headers ,
251
242
timeout = self .timeout ,
252
- verify = self .verify ,
253
243
)
254
244
except Exception as e :
255
245
raise KeycloakConnectionError ("Can't connect to server (%s)" % e )
256
246
257
- def raw_delete (self , path , data = None , ** kwargs ):
247
+ async def raw_delete (self , path , data = None , ** kwargs ):
258
248
"""Submit delete request to the path.
259
249
260
250
:param path: Path for request.
@@ -268,13 +258,13 @@ def raw_delete(self, path, data=None, **kwargs):
268
258
:raises KeycloakConnectionError: HttpError Can't connect to server.
269
259
"""
270
260
try :
271
- return self ._s .delete (
261
+ return await self ._s .request (
262
+ "DELETE" ,
272
263
urljoin (self .base_url , path ),
273
264
params = kwargs ,
274
265
data = data or dict (),
275
266
headers = self .headers ,
276
267
timeout = self .timeout ,
277
- verify = self .verify ,
278
268
)
279
269
except Exception as e :
280
270
raise KeycloakConnectionError ("Can't connect to server (%s)" % e )
0 commit comments