Skip to content

Commit 43c7adf

Browse files
authored
fix: prevent all httpx deprecation warnings (#666)
1 parent 9d08530 commit 43c7adf

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/keycloak/connection.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ async def a_raw_post(self, path: str, data: dict, **kwargs: dict) -> AsyncRespon
395395
method="POST",
396396
url=urljoin(self.base_url, path),
397397
params=self._filter_query_params(kwargs),
398-
data=data,
398+
**self._prepare_httpx_request_content(data),
399399
headers=self.headers,
400400
timeout=self.timeout,
401401
)
@@ -421,7 +421,7 @@ async def a_raw_put(self, path: str, data: dict, **kwargs: dict) -> AsyncRespons
421421
return await self.async_s.put(
422422
urljoin(self.base_url, path),
423423
params=self._filter_query_params(kwargs),
424-
data=data,
424+
**self._prepare_httpx_request_content(data),
425425
headers=self.headers,
426426
timeout=self.timeout,
427427
)
@@ -452,7 +452,7 @@ async def a_raw_delete(
452452
return await self.async_s.request(
453453
method="DELETE",
454454
url=urljoin(self.base_url, path),
455-
data=data or {},
455+
**self._prepare_httpx_request_content(data or {}),
456456
params=self._filter_query_params(kwargs),
457457
headers=self.headers,
458458
timeout=self.timeout,
@@ -461,6 +461,23 @@ async def a_raw_delete(
461461
msg = "Can't connect to server"
462462
raise KeycloakConnectionError(msg) from e
463463

464+
@staticmethod
465+
def _prepare_httpx_request_content(data: dict | str | None) -> dict:
466+
"""
467+
Create the correct request content kwarg to `httpx.AsyncClient.request()`.
468+
469+
See https://www.python-httpx.org/compatibility/#request-content
470+
471+
:param data: the request content
472+
:type data: dict | str | None
473+
:returns: A dict mapping the correct kwarg to the request content
474+
:rtype: dict
475+
"""
476+
if isinstance(data, str):
477+
# Note: this could also accept bytes, Iterable[bytes], or AsyncIterable[bytes]
478+
return {"content": data}
479+
return {"data": data}
480+
464481
@staticmethod
465482
def _filter_query_params(query_params: dict) -> dict:
466483
"""

tests/test_keycloak_admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6156,7 +6156,7 @@ async def test_a_email_query_param_handling(admin: KeycloakAdmin, user: str) ->
61566156

61576157
mock_put.assert_awaited_once_with(
61586158
ANY,
6159-
data='["UPDATE_PASSWORD"]',
6159+
content='["UPDATE_PASSWORD"]',
61606160
params={"client_id": "update-account-client-id", "redirect_uri": "https://example.com"},
61616161
headers=ANY,
61626162
timeout=60,

0 commit comments

Comments
 (0)