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
- self ._s .proxies . update ( proxies )
77
+ self ._s .proxies = 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,18 @@ 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 ,
217
+ files = kwargs .get ('files' ),
225
218
headers = self .headers ,
226
219
timeout = self .timeout ,
227
- verify = self .verify ,
228
220
)
229
221
except Exception as e :
230
222
raise KeycloakConnectionError ("Can't connect to server (%s)" % e )
231
223
232
- def raw_put (self , path , data , ** kwargs ):
224
+ async def raw_put (self , path , data , ** kwargs ):
233
225
"""Submit put request to the path.
234
226
235
227
:param path: Path for request.
@@ -243,18 +235,17 @@ def raw_put(self, path, data, **kwargs):
243
235
:raises KeycloakConnectionError: HttpError Can't connect to server.
244
236
"""
245
237
try :
246
- return self ._s .put (
238
+ return await self ._s .put (
247
239
urljoin (self .base_url , path ),
248
240
params = kwargs ,
249
241
data = data ,
250
242
headers = self .headers ,
251
243
timeout = self .timeout ,
252
- verify = self .verify ,
253
244
)
254
245
except Exception as e :
255
246
raise KeycloakConnectionError ("Can't connect to server (%s)" % e )
256
247
257
- def raw_delete (self , path , data = None , ** kwargs ):
248
+ async def raw_delete (self , path , data = None , ** kwargs ):
258
249
"""Submit delete request to the path.
259
250
260
251
:param path: Path for request.
@@ -268,13 +259,13 @@ def raw_delete(self, path, data=None, **kwargs):
268
259
:raises KeycloakConnectionError: HttpError Can't connect to server.
269
260
"""
270
261
try :
271
- return self ._s .delete (
262
+ return await self ._s .request (
263
+ "DELETE" ,
272
264
urljoin (self .base_url , path ),
273
265
params = kwargs ,
274
266
data = data or dict (),
275
267
headers = self .headers ,
276
268
timeout = self .timeout ,
277
- verify = self .verify ,
278
269
)
279
270
except Exception as e :
280
271
raise KeycloakConnectionError ("Can't connect to server (%s)" % e )
0 commit comments