Browse Source

fix: prevent all httpx deprecation warnings (#666)

pull/671/head v5.8.1
Greg 2 months ago
committed by GitHub
parent
commit
43c7adf099
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 23
      src/keycloak/connection.py
  2. 2
      tests/test_keycloak_admin.py

23
src/keycloak/connection.py

@ -395,7 +395,7 @@ class ConnectionManager:
method="POST", method="POST",
url=urljoin(self.base_url, path), url=urljoin(self.base_url, path),
params=self._filter_query_params(kwargs), params=self._filter_query_params(kwargs),
data=data,
**self._prepare_httpx_request_content(data),
headers=self.headers, headers=self.headers,
timeout=self.timeout, timeout=self.timeout,
) )
@ -421,7 +421,7 @@ class ConnectionManager:
return await self.async_s.put( return await self.async_s.put(
urljoin(self.base_url, path), urljoin(self.base_url, path),
params=self._filter_query_params(kwargs), params=self._filter_query_params(kwargs),
data=data,
**self._prepare_httpx_request_content(data),
headers=self.headers, headers=self.headers,
timeout=self.timeout, timeout=self.timeout,
) )
@ -452,7 +452,7 @@ class ConnectionManager:
return await self.async_s.request( return await self.async_s.request(
method="DELETE", method="DELETE",
url=urljoin(self.base_url, path), url=urljoin(self.base_url, path),
data=data or {},
**self._prepare_httpx_request_content(data or {}),
params=self._filter_query_params(kwargs), params=self._filter_query_params(kwargs),
headers=self.headers, headers=self.headers,
timeout=self.timeout, timeout=self.timeout,
@ -461,6 +461,23 @@ class ConnectionManager:
msg = "Can't connect to server" msg = "Can't connect to server"
raise KeycloakConnectionError(msg) from e raise KeycloakConnectionError(msg) from e
@staticmethod
def _prepare_httpx_request_content(data: dict | str | None) -> dict:
"""
Create the correct request content kwarg to `httpx.AsyncClient.request()`.
See https://www.python-httpx.org/compatibility/#request-content
:param data: the request content
:type data: dict | str | None
:returns: A dict mapping the correct kwarg to the request content
:rtype: dict
"""
if isinstance(data, str):
# Note: this could also accept bytes, Iterable[bytes], or AsyncIterable[bytes]
return {"content": data}
return {"data": data}
@staticmethod @staticmethod
def _filter_query_params(query_params: dict) -> dict: def _filter_query_params(query_params: dict) -> dict:
""" """

2
tests/test_keycloak_admin.py

@ -6156,7 +6156,7 @@ async def test_a_email_query_param_handling(admin: KeycloakAdmin, user: str) ->
mock_put.assert_awaited_once_with( mock_put.assert_awaited_once_with(
ANY, ANY,
data='["UPDATE_PASSWORD"]',
content='["UPDATE_PASSWORD"]',
params={"client_id": "update-account-client-id", "redirect_uri": "https://example.com"}, params={"client_id": "update-account-client-id", "redirect_uri": "https://example.com"},
headers=ANY, headers=ANY,
timeout=60, timeout=60,

Loading…
Cancel
Save