From 0fc744a3eb0ac89c382b37525c8f5e79d04689d3 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 29 May 2024 14:33:35 -0500 Subject: [PATCH] chore: add async client to connection --- .gitignore | 3 ++ pyproject.toml | 2 + src/keycloak/connection.py | 104 ++++++++++++++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a9acd74..b7ce21c 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,6 @@ main2.py s3air-authz-config.json .vscode _build + + +test.py \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 783d404..ce387a8 100644 --- a/pyproject.toml +++ b/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" diff --git a/src/keycloak/connection.py b/src/keycloak/connection.py index 0dfeaff..9f1f010 100644 --- a/src/keycloak/connection.py +++ b/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)