Skip to content

Commit 9f17615

Browse files
chore: speedup initial import
1 parent c8986ce commit 9f17615

File tree

1 file changed

+179
-47
lines changed

1 file changed

+179
-47
lines changed

src/codex/_client.py

Lines changed: 179 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import os
6-
from typing import Any, Dict, Mapping, cast
6+
from typing import TYPE_CHECKING, Any, Dict, Mapping, cast
77
from typing_extensions import Self, Literal, override
88

99
import httpx
@@ -21,18 +21,22 @@
2121
not_given,
2222
)
2323
from ._utils import is_given, get_async_library
24+
from ._compat import cached_property
2425
from ._version import __version__
25-
from .resources import health
2626
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
2727
from ._exceptions import APIStatusError
2828
from ._base_client import (
2929
DEFAULT_MAX_RETRIES,
3030
SyncAPIClient,
3131
AsyncAPIClient,
3232
)
33-
from .resources.users import users
34-
from .resources.projects import projects
35-
from .resources.organizations import organizations
33+
34+
if TYPE_CHECKING:
35+
from .resources import users, health, projects, organizations
36+
from .resources.health import HealthResource, AsyncHealthResource
37+
from .resources.users.users import UsersResource, AsyncUsersResource
38+
from .resources.projects.projects import ProjectsResource, AsyncProjectsResource
39+
from .resources.organizations.organizations import OrganizationsResource, AsyncOrganizationsResource
3640

3741
__all__ = [
3842
"ENVIRONMENTS",
@@ -54,13 +58,6 @@
5458

5559

5660
class Codex(SyncAPIClient):
57-
health: health.HealthResource
58-
organizations: organizations.OrganizationsResource
59-
users: users.UsersResource
60-
projects: projects.ProjectsResource
61-
with_raw_response: CodexWithRawResponse
62-
with_streaming_response: CodexWithStreamedResponse
63-
6461
# client options
6562
auth_token: str | None
6663
api_key: str | None
@@ -138,12 +135,37 @@ def __init__(
138135
_strict_response_validation=_strict_response_validation,
139136
)
140137

141-
self.health = health.HealthResource(self)
142-
self.organizations = organizations.OrganizationsResource(self)
143-
self.users = users.UsersResource(self)
144-
self.projects = projects.ProjectsResource(self)
145-
self.with_raw_response = CodexWithRawResponse(self)
146-
self.with_streaming_response = CodexWithStreamedResponse(self)
138+
@cached_property
139+
def health(self) -> HealthResource:
140+
from .resources.health import HealthResource
141+
142+
return HealthResource(self)
143+
144+
@cached_property
145+
def organizations(self) -> OrganizationsResource:
146+
from .resources.organizations import OrganizationsResource
147+
148+
return OrganizationsResource(self)
149+
150+
@cached_property
151+
def users(self) -> UsersResource:
152+
from .resources.users import UsersResource
153+
154+
return UsersResource(self)
155+
156+
@cached_property
157+
def projects(self) -> ProjectsResource:
158+
from .resources.projects import ProjectsResource
159+
160+
return ProjectsResource(self)
161+
162+
@cached_property
163+
def with_raw_response(self) -> CodexWithRawResponse:
164+
return CodexWithRawResponse(self)
165+
166+
@cached_property
167+
def with_streaming_response(self) -> CodexWithStreamedResponse:
168+
return CodexWithStreamedResponse(self)
147169

148170
@property
149171
@override
@@ -298,13 +320,6 @@ def _make_status_error(
298320

299321

300322
class AsyncCodex(AsyncAPIClient):
301-
health: health.AsyncHealthResource
302-
organizations: organizations.AsyncOrganizationsResource
303-
users: users.AsyncUsersResource
304-
projects: projects.AsyncProjectsResource
305-
with_raw_response: AsyncCodexWithRawResponse
306-
with_streaming_response: AsyncCodexWithStreamedResponse
307-
308323
# client options
309324
auth_token: str | None
310325
api_key: str | None
@@ -382,12 +397,37 @@ def __init__(
382397
_strict_response_validation=_strict_response_validation,
383398
)
384399

385-
self.health = health.AsyncHealthResource(self)
386-
self.organizations = organizations.AsyncOrganizationsResource(self)
387-
self.users = users.AsyncUsersResource(self)
388-
self.projects = projects.AsyncProjectsResource(self)
389-
self.with_raw_response = AsyncCodexWithRawResponse(self)
390-
self.with_streaming_response = AsyncCodexWithStreamedResponse(self)
400+
@cached_property
401+
def health(self) -> AsyncHealthResource:
402+
from .resources.health import AsyncHealthResource
403+
404+
return AsyncHealthResource(self)
405+
406+
@cached_property
407+
def organizations(self) -> AsyncOrganizationsResource:
408+
from .resources.organizations import AsyncOrganizationsResource
409+
410+
return AsyncOrganizationsResource(self)
411+
412+
@cached_property
413+
def users(self) -> AsyncUsersResource:
414+
from .resources.users import AsyncUsersResource
415+
416+
return AsyncUsersResource(self)
417+
418+
@cached_property
419+
def projects(self) -> AsyncProjectsResource:
420+
from .resources.projects import AsyncProjectsResource
421+
422+
return AsyncProjectsResource(self)
423+
424+
@cached_property
425+
def with_raw_response(self) -> AsyncCodexWithRawResponse:
426+
return AsyncCodexWithRawResponse(self)
427+
428+
@cached_property
429+
def with_streaming_response(self) -> AsyncCodexWithStreamedResponse:
430+
return AsyncCodexWithStreamedResponse(self)
391431

392432
@property
393433
@override
@@ -542,35 +582,127 @@ def _make_status_error(
542582

543583

544584
class CodexWithRawResponse:
585+
_client: Codex
586+
545587
def __init__(self, client: Codex) -> None:
546-
self.health = health.HealthResourceWithRawResponse(client.health)
547-
self.organizations = organizations.OrganizationsResourceWithRawResponse(client.organizations)
548-
self.users = users.UsersResourceWithRawResponse(client.users)
549-
self.projects = projects.ProjectsResourceWithRawResponse(client.projects)
588+
self._client = client
589+
590+
@cached_property
591+
def health(self) -> health.HealthResourceWithRawResponse:
592+
from .resources.health import HealthResourceWithRawResponse
593+
594+
return HealthResourceWithRawResponse(self._client.health)
595+
596+
@cached_property
597+
def organizations(self) -> organizations.OrganizationsResourceWithRawResponse:
598+
from .resources.organizations import OrganizationsResourceWithRawResponse
599+
600+
return OrganizationsResourceWithRawResponse(self._client.organizations)
601+
602+
@cached_property
603+
def users(self) -> users.UsersResourceWithRawResponse:
604+
from .resources.users import UsersResourceWithRawResponse
605+
606+
return UsersResourceWithRawResponse(self._client.users)
607+
608+
@cached_property
609+
def projects(self) -> projects.ProjectsResourceWithRawResponse:
610+
from .resources.projects import ProjectsResourceWithRawResponse
611+
612+
return ProjectsResourceWithRawResponse(self._client.projects)
550613

551614

552615
class AsyncCodexWithRawResponse:
616+
_client: AsyncCodex
617+
553618
def __init__(self, client: AsyncCodex) -> None:
554-
self.health = health.AsyncHealthResourceWithRawResponse(client.health)
555-
self.organizations = organizations.AsyncOrganizationsResourceWithRawResponse(client.organizations)
556-
self.users = users.AsyncUsersResourceWithRawResponse(client.users)
557-
self.projects = projects.AsyncProjectsResourceWithRawResponse(client.projects)
619+
self._client = client
620+
621+
@cached_property
622+
def health(self) -> health.AsyncHealthResourceWithRawResponse:
623+
from .resources.health import AsyncHealthResourceWithRawResponse
624+
625+
return AsyncHealthResourceWithRawResponse(self._client.health)
626+
627+
@cached_property
628+
def organizations(self) -> organizations.AsyncOrganizationsResourceWithRawResponse:
629+
from .resources.organizations import AsyncOrganizationsResourceWithRawResponse
630+
631+
return AsyncOrganizationsResourceWithRawResponse(self._client.organizations)
632+
633+
@cached_property
634+
def users(self) -> users.AsyncUsersResourceWithRawResponse:
635+
from .resources.users import AsyncUsersResourceWithRawResponse
636+
637+
return AsyncUsersResourceWithRawResponse(self._client.users)
638+
639+
@cached_property
640+
def projects(self) -> projects.AsyncProjectsResourceWithRawResponse:
641+
from .resources.projects import AsyncProjectsResourceWithRawResponse
642+
643+
return AsyncProjectsResourceWithRawResponse(self._client.projects)
558644

559645

560646
class CodexWithStreamedResponse:
647+
_client: Codex
648+
561649
def __init__(self, client: Codex) -> None:
562-
self.health = health.HealthResourceWithStreamingResponse(client.health)
563-
self.organizations = organizations.OrganizationsResourceWithStreamingResponse(client.organizations)
564-
self.users = users.UsersResourceWithStreamingResponse(client.users)
565-
self.projects = projects.ProjectsResourceWithStreamingResponse(client.projects)
650+
self._client = client
651+
652+
@cached_property
653+
def health(self) -> health.HealthResourceWithStreamingResponse:
654+
from .resources.health import HealthResourceWithStreamingResponse
655+
656+
return HealthResourceWithStreamingResponse(self._client.health)
657+
658+
@cached_property
659+
def organizations(self) -> organizations.OrganizationsResourceWithStreamingResponse:
660+
from .resources.organizations import OrganizationsResourceWithStreamingResponse
661+
662+
return OrganizationsResourceWithStreamingResponse(self._client.organizations)
663+
664+
@cached_property
665+
def users(self) -> users.UsersResourceWithStreamingResponse:
666+
from .resources.users import UsersResourceWithStreamingResponse
667+
668+
return UsersResourceWithStreamingResponse(self._client.users)
669+
670+
@cached_property
671+
def projects(self) -> projects.ProjectsResourceWithStreamingResponse:
672+
from .resources.projects import ProjectsResourceWithStreamingResponse
673+
674+
return ProjectsResourceWithStreamingResponse(self._client.projects)
566675

567676

568677
class AsyncCodexWithStreamedResponse:
678+
_client: AsyncCodex
679+
569680
def __init__(self, client: AsyncCodex) -> None:
570-
self.health = health.AsyncHealthResourceWithStreamingResponse(client.health)
571-
self.organizations = organizations.AsyncOrganizationsResourceWithStreamingResponse(client.organizations)
572-
self.users = users.AsyncUsersResourceWithStreamingResponse(client.users)
573-
self.projects = projects.AsyncProjectsResourceWithStreamingResponse(client.projects)
681+
self._client = client
682+
683+
@cached_property
684+
def health(self) -> health.AsyncHealthResourceWithStreamingResponse:
685+
from .resources.health import AsyncHealthResourceWithStreamingResponse
686+
687+
return AsyncHealthResourceWithStreamingResponse(self._client.health)
688+
689+
@cached_property
690+
def organizations(self) -> organizations.AsyncOrganizationsResourceWithStreamingResponse:
691+
from .resources.organizations import AsyncOrganizationsResourceWithStreamingResponse
692+
693+
return AsyncOrganizationsResourceWithStreamingResponse(self._client.organizations)
694+
695+
@cached_property
696+
def users(self) -> users.AsyncUsersResourceWithStreamingResponse:
697+
from .resources.users import AsyncUsersResourceWithStreamingResponse
698+
699+
return AsyncUsersResourceWithStreamingResponse(self._client.users)
700+
701+
@cached_property
702+
def projects(self) -> projects.AsyncProjectsResourceWithStreamingResponse:
703+
from .resources.projects import AsyncProjectsResourceWithStreamingResponse
704+
705+
return AsyncProjectsResourceWithStreamingResponse(self._client.projects)
574706

575707

576708
Client = Codex

0 commit comments

Comments
 (0)