Browse Source

chore: add async client to connection

pull/566/head
David 11 months ago
parent
commit
0fc744a3eb
  1. 3
      .gitignore
  2. 2
      pyproject.toml
  3. 104
      src/keycloak/connection.py

3
.gitignore

@ -108,3 +108,6 @@ main2.py
s3air-authz-config.json
.vscode
_build
test.py

2
pyproject.toml

@ -34,6 +34,7 @@ requests = ">=2.20.0"
requests-toolbelt = ">=0.6.0"
deprecation = ">=2.1.0"
jwcrypto = "^1.5.4"
httpx = ">=0.23.2"
[tool.poetry.group.docs.dependencies]
alabaster = ">=0.7.12"
@ -49,6 +50,7 @@ sphinx-autoapi = ">=3.0.0"
tox = ">=4.0.0"
pytest = ">=7.1.2"
pytest-cov = ">=3.0.0"
pytest-asyncio = ">=0.23.7"
wheel = ">=0.38.4"
pre-commit = ">=2.19.0"
isort = ">=5.10.1"

104
src/keycloak/connection.py

@ -29,6 +29,7 @@ except ImportError: # pragma: no cover
from urlparse import urljoin
import requests
import httpx
from requests.adapters import HTTPAdapter
from .exceptions import KeycloakConnectionError
@ -86,6 +87,14 @@ class ConnectionManager(object):
if proxies:
self._s.proxies.update(proxies)
self.async_s = httpx.AsyncClient(verify=verify, proxies=proxies)
self.async_s.auth = None # don't let requests add auth headers
self.async_s.transport = httpx.AsyncHTTPTransport(retries=1)
async def aclose(self):
if hasattr(self, "_s"):
await self.async_s.aclose()
def __del__(self):
"""Del method."""
if hasattr(self, "_s"):
@ -271,7 +280,7 @@ class ConnectionManager(object):
:raises KeycloakConnectionError: HttpError Can't connect to server.
"""
try:
return self._s.delete(
r = self._s.delete(
urljoin(self.base_url, path),
params=kwargs,
data=data or dict(),
@ -279,5 +288,98 @@ class ConnectionManager(object):
timeout=self.timeout,
verify=self.verify,
)
return r
except Exception as e:
raise KeycloakConnectionError("Can't connect to server (%s)" % e)
async def a_raw_get(self, path, **kwargs):
"""Submit get request to the path.
:param path: Path for request.
:type path: str
:param kwargs: Additional arguments
:type kwargs: dict
:returns: Response the request.
:rtype: Response
:raises KeycloakConnectionError: HttpError Can't connect to server.
"""
try:
return await self.async_s.get(
urljoin(self.base_url, path),
params=kwargs,
headers=self.headers,
timeout=self.timeout
)
except Exception as e:
raise KeycloakConnectionError("Can't connect to server (%s)" % e)
async def a_raw_post(self, path, data, **kwargs):
"""Submit post request to the path.
:param path: Path for request.
:type path: str
:param data: Payload for request.
:type data: dict
:param kwargs: Additional arguments
:type kwargs: dict
:returns: Response the request.
:rtype: Response
:raises KeycloakConnectionError: HttpError Can't connect to server.
"""
try:
return await self.async_s.post(
urljoin(self.base_url, path),
params=kwargs,
data=data,
headers=self.headers,
timeout=self.timeout
)
except Exception as e:
raise KeycloakConnectionError("Can't connect to server (%s)" % e)
async def a_raw_put(self, path, data, **kwargs):
"""Submit put request to the path.
:param path: Path for request.
:type path: str
:param data: Payload for request.
:type data: dict
:param kwargs: Additional arguments
:type kwargs: dict
:returns: Response the request.
:rtype: Response
:raises KeycloakConnectionError: HttpError Can't connect to server.
"""
try:
return await self.async_s.put(
urljoin(self.base_url, path),
params=kwargs,
data=data,
headers=self.headers,
timeout=self.timeout,
)
except Exception as e:
raise KeycloakConnectionError("Can't connect to server (%s)" % e)
async def a_raw_delete(self, path, data=None, **kwargs):
"""Submit delete request to the path.
:param path: Path for request.
:type path: str
:param data: Payload for request.
:type data: dict | None
:param kwargs: Additional arguments
:type kwargs: dict
:returns: Response the request.
:rtype: Response
:raises KeycloakConnectionError: HttpError Can't connect to server.
"""
try:
return await self.async_s.delete(
urljoin(self.base_url, path),
params=kwargs,
headers=self.headers,
timeout=self.timeout,
)
except Exception as e:
raise KeycloakConnectionError("Can't connect to server (%s)" % e)
Loading…
Cancel
Save