Skip to content

Commit 4b38aa0

Browse files
authored
fix: deprecate public api_client property. warn on async api_client footgun on sync client (#2618)
1 parent 754fbc6 commit 4b38aa0

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

cognite/client/_cognite_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,21 @@ def __init__(self, config: ClientConfig | None = None) -> None:
171171
def api_client(self) -> APIClient:
172172
"""Returns the underlying API client used for HTTP requests.
173173
174+
.. deprecated:: 8.0
175+
Use :meth:`post`, :meth:`get`, or :meth:`put` instead.
176+
Will be removed in v9.
177+
174178
Returns:
175179
APIClient: The API client instance.
176180
"""
181+
import warnings
182+
183+
warnings.warn(
184+
"'api_client' is deprecated and will be removed in v9. "
185+
"Use client.post(), client.get(), or client.put() instead.",
186+
FutureWarning,
187+
stacklevel=2,
188+
)
177189
return self._api_client
178190

179191
def get_sync_client(self) -> CogniteClient:

cognite/client/_sync_cognite_client.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,33 @@ def config(self) -> ClientConfig:
142142
def api_client(self) -> APIClient:
143143
"""Returns the underlying API client used for HTTP requests.
144144
145+
.. deprecated:: 8.0
146+
Use :meth:`post`, :meth:`get`, or :meth:`put` instead.
147+
Will be removed in v9.
148+
149+
.. warning::
150+
This returns the **async** API client. Calling its methods directly
151+
requires ``await`` and will not work in a synchronous context.
152+
Use :meth:`post`, :meth:`get`, or :meth:`put` instead.
153+
145154
Returns:
146-
APIClient: The API client instance.
155+
APIClient: The async API client instance.
147156
"""
157+
import warnings
158+
159+
warnings.warn(
160+
"'api_client' is deprecated and will be removed in v9. "
161+
"Use client.post(), client.get(), or client.put() instead.",
162+
FutureWarning,
163+
stacklevel=2,
164+
)
165+
warnings.warn(
166+
"'api_client' returns the underlying async API client. Calling its methods directly requires "
167+
"'await' and will not work in a synchronous context. "
168+
"Use client.post(), client.get(), or client.put() for synchronous HTTP requests instead.",
169+
UserWarning,
170+
stacklevel=2,
171+
)
148172
return self.__async_client._api_client
149173

150174
def get_async_client(self) -> AsyncCogniteClient:

scripts/sync_client_codegen/sync_client_template.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,33 @@ class CogniteClient:
8686
def api_client(self) -> APIClient:
8787
"""Returns the underlying API client used for HTTP requests.
8888

89+
.. deprecated:: 8.0
90+
Use :meth:`post`, :meth:`get`, or :meth:`put` instead.
91+
Will be removed in v9.
92+
93+
.. warning::
94+
This returns the **async** API client. Calling its methods directly
95+
requires ``await`` and will not work in a synchronous context.
96+
Use :meth:`post`, :meth:`get`, or :meth:`put` instead.
97+
8998
Returns:
90-
APIClient: The API client instance.
99+
APIClient: The async API client instance.
91100
"""
101+
import warnings
102+
103+
warnings.warn(
104+
"'api_client' is deprecated and will be removed in v9. "
105+
"Use client.post(), client.get(), or client.put() instead.",
106+
FutureWarning,
107+
stacklevel=2,
108+
)
109+
warnings.warn(
110+
"'api_client' returns the underlying async API client. Calling its methods directly requires "
111+
"'await' and will not work in a synchronous context. "
112+
"Use client.post(), client.get(), or client.put() for synchronous HTTP requests instead.",
113+
UserWarning,
114+
stacklevel=2,
115+
)
92116
return self.__async_client._api_client
93117

94118
def get_async_client(self) -> AsyncCogniteClient:

0 commit comments

Comments
 (0)