|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import os |
6 | | -from typing import Any, Mapping |
| 6 | +from typing import TYPE_CHECKING, Any, Mapping |
7 | 7 | from typing_extensions import Self, override |
8 | 8 |
|
9 | 9 | import httpx |
|
20 | 20 | not_given, |
21 | 21 | ) |
22 | 22 | from ._utils import is_given, get_async_library |
| 23 | +from ._compat import cached_property |
23 | 24 | from ._version import __version__ |
24 | 25 | from ._streaming import Stream as Stream, AsyncStream as AsyncStream |
25 | 26 | from ._exceptions import WarpAPIError, APIStatusError |
|
28 | 29 | SyncAPIClient, |
29 | 30 | AsyncAPIClient, |
30 | 31 | ) |
31 | | -from .resources.agent import agent |
| 32 | + |
| 33 | +if TYPE_CHECKING: |
| 34 | + from .resources import agent |
| 35 | + from .resources.agent.agent import AgentResource, AsyncAgentResource |
32 | 36 |
|
33 | 37 | __all__ = ["Timeout", "Transport", "ProxiesTypes", "RequestOptions", "WarpAPI", "AsyncWarpAPI", "Client", "AsyncClient"] |
34 | 38 |
|
35 | 39 |
|
36 | 40 | class WarpAPI(SyncAPIClient): |
37 | | - agent: agent.AgentResource |
38 | | - with_raw_response: WarpAPIWithRawResponse |
39 | | - with_streaming_response: WarpAPIWithStreamedResponse |
40 | | - |
41 | 41 | # client options |
42 | 42 | api_key: str |
43 | 43 |
|
@@ -92,9 +92,19 @@ def __init__( |
92 | 92 | _strict_response_validation=_strict_response_validation, |
93 | 93 | ) |
94 | 94 |
|
95 | | - self.agent = agent.AgentResource(self) |
96 | | - self.with_raw_response = WarpAPIWithRawResponse(self) |
97 | | - self.with_streaming_response = WarpAPIWithStreamedResponse(self) |
| 95 | + @cached_property |
| 96 | + def agent(self) -> AgentResource: |
| 97 | + from .resources.agent import AgentResource |
| 98 | + |
| 99 | + return AgentResource(self) |
| 100 | + |
| 101 | + @cached_property |
| 102 | + def with_raw_response(self) -> WarpAPIWithRawResponse: |
| 103 | + return WarpAPIWithRawResponse(self) |
| 104 | + |
| 105 | + @cached_property |
| 106 | + def with_streaming_response(self) -> WarpAPIWithStreamedResponse: |
| 107 | + return WarpAPIWithStreamedResponse(self) |
98 | 108 |
|
99 | 109 | @property |
100 | 110 | @override |
@@ -202,10 +212,6 @@ def _make_status_error( |
202 | 212 |
|
203 | 213 |
|
204 | 214 | class AsyncWarpAPI(AsyncAPIClient): |
205 | | - agent: agent.AsyncAgentResource |
206 | | - with_raw_response: AsyncWarpAPIWithRawResponse |
207 | | - with_streaming_response: AsyncWarpAPIWithStreamedResponse |
208 | | - |
209 | 215 | # client options |
210 | 216 | api_key: str |
211 | 217 |
|
@@ -260,9 +266,19 @@ def __init__( |
260 | 266 | _strict_response_validation=_strict_response_validation, |
261 | 267 | ) |
262 | 268 |
|
263 | | - self.agent = agent.AsyncAgentResource(self) |
264 | | - self.with_raw_response = AsyncWarpAPIWithRawResponse(self) |
265 | | - self.with_streaming_response = AsyncWarpAPIWithStreamedResponse(self) |
| 269 | + @cached_property |
| 270 | + def agent(self) -> AsyncAgentResource: |
| 271 | + from .resources.agent import AsyncAgentResource |
| 272 | + |
| 273 | + return AsyncAgentResource(self) |
| 274 | + |
| 275 | + @cached_property |
| 276 | + def with_raw_response(self) -> AsyncWarpAPIWithRawResponse: |
| 277 | + return AsyncWarpAPIWithRawResponse(self) |
| 278 | + |
| 279 | + @cached_property |
| 280 | + def with_streaming_response(self) -> AsyncWarpAPIWithStreamedResponse: |
| 281 | + return AsyncWarpAPIWithStreamedResponse(self) |
266 | 282 |
|
267 | 283 | @property |
268 | 284 | @override |
@@ -370,23 +386,55 @@ def _make_status_error( |
370 | 386 |
|
371 | 387 |
|
372 | 388 | class WarpAPIWithRawResponse: |
| 389 | + _client: WarpAPI |
| 390 | + |
373 | 391 | def __init__(self, client: WarpAPI) -> None: |
374 | | - self.agent = agent.AgentResourceWithRawResponse(client.agent) |
| 392 | + self._client = client |
| 393 | + |
| 394 | + @cached_property |
| 395 | + def agent(self) -> agent.AgentResourceWithRawResponse: |
| 396 | + from .resources.agent import AgentResourceWithRawResponse |
| 397 | + |
| 398 | + return AgentResourceWithRawResponse(self._client.agent) |
375 | 399 |
|
376 | 400 |
|
377 | 401 | class AsyncWarpAPIWithRawResponse: |
| 402 | + _client: AsyncWarpAPI |
| 403 | + |
378 | 404 | def __init__(self, client: AsyncWarpAPI) -> None: |
379 | | - self.agent = agent.AsyncAgentResourceWithRawResponse(client.agent) |
| 405 | + self._client = client |
| 406 | + |
| 407 | + @cached_property |
| 408 | + def agent(self) -> agent.AsyncAgentResourceWithRawResponse: |
| 409 | + from .resources.agent import AsyncAgentResourceWithRawResponse |
| 410 | + |
| 411 | + return AsyncAgentResourceWithRawResponse(self._client.agent) |
380 | 412 |
|
381 | 413 |
|
382 | 414 | class WarpAPIWithStreamedResponse: |
| 415 | + _client: WarpAPI |
| 416 | + |
383 | 417 | def __init__(self, client: WarpAPI) -> None: |
384 | | - self.agent = agent.AgentResourceWithStreamingResponse(client.agent) |
| 418 | + self._client = client |
| 419 | + |
| 420 | + @cached_property |
| 421 | + def agent(self) -> agent.AgentResourceWithStreamingResponse: |
| 422 | + from .resources.agent import AgentResourceWithStreamingResponse |
| 423 | + |
| 424 | + return AgentResourceWithStreamingResponse(self._client.agent) |
385 | 425 |
|
386 | 426 |
|
387 | 427 | class AsyncWarpAPIWithStreamedResponse: |
| 428 | + _client: AsyncWarpAPI |
| 429 | + |
388 | 430 | def __init__(self, client: AsyncWarpAPI) -> None: |
389 | | - self.agent = agent.AsyncAgentResourceWithStreamingResponse(client.agent) |
| 431 | + self._client = client |
| 432 | + |
| 433 | + @cached_property |
| 434 | + def agent(self) -> agent.AsyncAgentResourceWithStreamingResponse: |
| 435 | + from .resources.agent import AsyncAgentResourceWithStreamingResponse |
| 436 | + |
| 437 | + return AsyncAgentResourceWithStreamingResponse(self._client.agent) |
390 | 438 |
|
391 | 439 |
|
392 | 440 | Client = WarpAPI |
|
0 commit comments