|
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 | | -from .resources import sessions |
25 | 25 | from ._streaming import Stream as Stream, AsyncStream as AsyncStream |
26 | 26 | from ._exceptions import APIStatusError, StagehandError |
27 | 27 | from ._base_client import ( |
|
30 | 30 | AsyncAPIClient, |
31 | 31 | ) |
32 | 32 |
|
| 33 | +if TYPE_CHECKING: |
| 34 | + from .resources import sessions |
| 35 | + from .resources.sessions import SessionsResource, AsyncSessionsResource |
| 36 | + |
33 | 37 | __all__ = [ |
34 | 38 | "Timeout", |
35 | 39 | "Transport", |
|
43 | 47 |
|
44 | 48 |
|
45 | 49 | class Stagehand(SyncAPIClient): |
46 | | - sessions: sessions.SessionsResource |
47 | | - with_raw_response: StagehandWithRawResponse |
48 | | - with_streaming_response: StagehandWithStreamedResponse |
49 | | - |
50 | 50 | # client options |
51 | 51 | browserbase_api_key: str |
52 | 52 | browserbase_project_id: str |
@@ -124,9 +124,19 @@ def __init__( |
124 | 124 | _strict_response_validation=_strict_response_validation, |
125 | 125 | ) |
126 | 126 |
|
127 | | - self.sessions = sessions.SessionsResource(self) |
128 | | - self.with_raw_response = StagehandWithRawResponse(self) |
129 | | - self.with_streaming_response = StagehandWithStreamedResponse(self) |
| 127 | + @cached_property |
| 128 | + def sessions(self) -> SessionsResource: |
| 129 | + from .resources.sessions import SessionsResource |
| 130 | + |
| 131 | + return SessionsResource(self) |
| 132 | + |
| 133 | + @cached_property |
| 134 | + def with_raw_response(self) -> StagehandWithRawResponse: |
| 135 | + return StagehandWithRawResponse(self) |
| 136 | + |
| 137 | + @cached_property |
| 138 | + def with_streaming_response(self) -> StagehandWithStreamedResponse: |
| 139 | + return StagehandWithStreamedResponse(self) |
130 | 140 |
|
131 | 141 | @property |
132 | 142 | @override |
@@ -252,10 +262,6 @@ def _make_status_error( |
252 | 262 |
|
253 | 263 |
|
254 | 264 | class AsyncStagehand(AsyncAPIClient): |
255 | | - sessions: sessions.AsyncSessionsResource |
256 | | - with_raw_response: AsyncStagehandWithRawResponse |
257 | | - with_streaming_response: AsyncStagehandWithStreamedResponse |
258 | | - |
259 | 265 | # client options |
260 | 266 | browserbase_api_key: str |
261 | 267 | browserbase_project_id: str |
@@ -333,9 +339,19 @@ def __init__( |
333 | 339 | _strict_response_validation=_strict_response_validation, |
334 | 340 | ) |
335 | 341 |
|
336 | | - self.sessions = sessions.AsyncSessionsResource(self) |
337 | | - self.with_raw_response = AsyncStagehandWithRawResponse(self) |
338 | | - self.with_streaming_response = AsyncStagehandWithStreamedResponse(self) |
| 342 | + @cached_property |
| 343 | + def sessions(self) -> AsyncSessionsResource: |
| 344 | + from .resources.sessions import AsyncSessionsResource |
| 345 | + |
| 346 | + return AsyncSessionsResource(self) |
| 347 | + |
| 348 | + @cached_property |
| 349 | + def with_raw_response(self) -> AsyncStagehandWithRawResponse: |
| 350 | + return AsyncStagehandWithRawResponse(self) |
| 351 | + |
| 352 | + @cached_property |
| 353 | + def with_streaming_response(self) -> AsyncStagehandWithStreamedResponse: |
| 354 | + return AsyncStagehandWithStreamedResponse(self) |
339 | 355 |
|
340 | 356 | @property |
341 | 357 | @override |
@@ -461,23 +477,55 @@ def _make_status_error( |
461 | 477 |
|
462 | 478 |
|
463 | 479 | class StagehandWithRawResponse: |
| 480 | + _client: Stagehand |
| 481 | + |
464 | 482 | def __init__(self, client: Stagehand) -> None: |
465 | | - self.sessions = sessions.SessionsResourceWithRawResponse(client.sessions) |
| 483 | + self._client = client |
| 484 | + |
| 485 | + @cached_property |
| 486 | + def sessions(self) -> sessions.SessionsResourceWithRawResponse: |
| 487 | + from .resources.sessions import SessionsResourceWithRawResponse |
| 488 | + |
| 489 | + return SessionsResourceWithRawResponse(self._client.sessions) |
466 | 490 |
|
467 | 491 |
|
468 | 492 | class AsyncStagehandWithRawResponse: |
| 493 | + _client: AsyncStagehand |
| 494 | + |
469 | 495 | def __init__(self, client: AsyncStagehand) -> None: |
470 | | - self.sessions = sessions.AsyncSessionsResourceWithRawResponse(client.sessions) |
| 496 | + self._client = client |
| 497 | + |
| 498 | + @cached_property |
| 499 | + def sessions(self) -> sessions.AsyncSessionsResourceWithRawResponse: |
| 500 | + from .resources.sessions import AsyncSessionsResourceWithRawResponse |
| 501 | + |
| 502 | + return AsyncSessionsResourceWithRawResponse(self._client.sessions) |
471 | 503 |
|
472 | 504 |
|
473 | 505 | class StagehandWithStreamedResponse: |
| 506 | + _client: Stagehand |
| 507 | + |
474 | 508 | def __init__(self, client: Stagehand) -> None: |
475 | | - self.sessions = sessions.SessionsResourceWithStreamingResponse(client.sessions) |
| 509 | + self._client = client |
| 510 | + |
| 511 | + @cached_property |
| 512 | + def sessions(self) -> sessions.SessionsResourceWithStreamingResponse: |
| 513 | + from .resources.sessions import SessionsResourceWithStreamingResponse |
| 514 | + |
| 515 | + return SessionsResourceWithStreamingResponse(self._client.sessions) |
476 | 516 |
|
477 | 517 |
|
478 | 518 | class AsyncStagehandWithStreamedResponse: |
| 519 | + _client: AsyncStagehand |
| 520 | + |
479 | 521 | def __init__(self, client: AsyncStagehand) -> None: |
480 | | - self.sessions = sessions.AsyncSessionsResourceWithStreamingResponse(client.sessions) |
| 522 | + self._client = client |
| 523 | + |
| 524 | + @cached_property |
| 525 | + def sessions(self) -> sessions.AsyncSessionsResourceWithStreamingResponse: |
| 526 | + from .resources.sessions import AsyncSessionsResourceWithStreamingResponse |
| 527 | + |
| 528 | + return AsyncSessionsResourceWithStreamingResponse(self._client.sessions) |
481 | 529 |
|
482 | 530 |
|
483 | 531 | Client = Stagehand |
|
0 commit comments