Skip to content

Commit a0429e9

Browse files
chore: speedup initial import
1 parent 011c5b3 commit a0429e9

File tree

1 file changed

+179
-44
lines changed

1 file changed

+179
-44
lines changed

src/unlayer/_client.py

Lines changed: 179 additions & 44 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
@@ -20,8 +20,8 @@
2020
not_given,
2121
)
2222
from ._utils import is_given, get_async_library
23+
from ._compat import cached_property
2324
from ._version import __version__
24-
from .resources import pages, emails, project, documents
2525
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
2626
from ._exceptions import UnlayerError, APIStatusError
2727
from ._base_client import (
@@ -30,6 +30,13 @@
3030
AsyncAPIClient,
3131
)
3232

33+
if TYPE_CHECKING:
34+
from .resources import pages, emails, project, documents
35+
from .resources.pages import PagesResource, AsyncPagesResource
36+
from .resources.emails import EmailsResource, AsyncEmailsResource
37+
from .resources.project import ProjectResource, AsyncProjectResource
38+
from .resources.documents import DocumentsResource, AsyncDocumentsResource
39+
3340
__all__ = [
3441
"ENVIRONMENTS",
3542
"Timeout",
@@ -50,13 +57,6 @@
5057

5158

5259
class Unlayer(SyncAPIClient):
53-
project: project.ProjectResource
54-
emails: emails.EmailsResource
55-
pages: pages.PagesResource
56-
documents: documents.DocumentsResource
57-
with_raw_response: UnlayerWithRawResponse
58-
with_streaming_response: UnlayerWithStreamedResponse
59-
6060
# client options
6161
api_key: str
6262

@@ -135,12 +135,37 @@ def __init__(
135135
_strict_response_validation=_strict_response_validation,
136136
)
137137

138-
self.project = project.ProjectResource(self)
139-
self.emails = emails.EmailsResource(self)
140-
self.pages = pages.PagesResource(self)
141-
self.documents = documents.DocumentsResource(self)
142-
self.with_raw_response = UnlayerWithRawResponse(self)
143-
self.with_streaming_response = UnlayerWithStreamedResponse(self)
138+
@cached_property
139+
def project(self) -> ProjectResource:
140+
from .resources.project import ProjectResource
141+
142+
return ProjectResource(self)
143+
144+
@cached_property
145+
def emails(self) -> EmailsResource:
146+
from .resources.emails import EmailsResource
147+
148+
return EmailsResource(self)
149+
150+
@cached_property
151+
def pages(self) -> PagesResource:
152+
from .resources.pages import PagesResource
153+
154+
return PagesResource(self)
155+
156+
@cached_property
157+
def documents(self) -> DocumentsResource:
158+
from .resources.documents import DocumentsResource
159+
160+
return DocumentsResource(self)
161+
162+
@cached_property
163+
def with_raw_response(self) -> UnlayerWithRawResponse:
164+
return UnlayerWithRawResponse(self)
165+
166+
@cached_property
167+
def with_streaming_response(self) -> UnlayerWithStreamedResponse:
168+
return UnlayerWithStreamedResponse(self)
144169

145170
@property
146171
@override
@@ -250,13 +275,6 @@ def _make_status_error(
250275

251276

252277
class AsyncUnlayer(AsyncAPIClient):
253-
project: project.AsyncProjectResource
254-
emails: emails.AsyncEmailsResource
255-
pages: pages.AsyncPagesResource
256-
documents: documents.AsyncDocumentsResource
257-
with_raw_response: AsyncUnlayerWithRawResponse
258-
with_streaming_response: AsyncUnlayerWithStreamedResponse
259-
260278
# client options
261279
api_key: str
262280

@@ -335,12 +353,37 @@ def __init__(
335353
_strict_response_validation=_strict_response_validation,
336354
)
337355

338-
self.project = project.AsyncProjectResource(self)
339-
self.emails = emails.AsyncEmailsResource(self)
340-
self.pages = pages.AsyncPagesResource(self)
341-
self.documents = documents.AsyncDocumentsResource(self)
342-
self.with_raw_response = AsyncUnlayerWithRawResponse(self)
343-
self.with_streaming_response = AsyncUnlayerWithStreamedResponse(self)
356+
@cached_property
357+
def project(self) -> AsyncProjectResource:
358+
from .resources.project import AsyncProjectResource
359+
360+
return AsyncProjectResource(self)
361+
362+
@cached_property
363+
def emails(self) -> AsyncEmailsResource:
364+
from .resources.emails import AsyncEmailsResource
365+
366+
return AsyncEmailsResource(self)
367+
368+
@cached_property
369+
def pages(self) -> AsyncPagesResource:
370+
from .resources.pages import AsyncPagesResource
371+
372+
return AsyncPagesResource(self)
373+
374+
@cached_property
375+
def documents(self) -> AsyncDocumentsResource:
376+
from .resources.documents import AsyncDocumentsResource
377+
378+
return AsyncDocumentsResource(self)
379+
380+
@cached_property
381+
def with_raw_response(self) -> AsyncUnlayerWithRawResponse:
382+
return AsyncUnlayerWithRawResponse(self)
383+
384+
@cached_property
385+
def with_streaming_response(self) -> AsyncUnlayerWithStreamedResponse:
386+
return AsyncUnlayerWithStreamedResponse(self)
344387

345388
@property
346389
@override
@@ -450,35 +493,127 @@ def _make_status_error(
450493

451494

452495
class UnlayerWithRawResponse:
496+
_client: Unlayer
497+
453498
def __init__(self, client: Unlayer) -> None:
454-
self.project = project.ProjectResourceWithRawResponse(client.project)
455-
self.emails = emails.EmailsResourceWithRawResponse(client.emails)
456-
self.pages = pages.PagesResourceWithRawResponse(client.pages)
457-
self.documents = documents.DocumentsResourceWithRawResponse(client.documents)
499+
self._client = client
500+
501+
@cached_property
502+
def project(self) -> project.ProjectResourceWithRawResponse:
503+
from .resources.project import ProjectResourceWithRawResponse
504+
505+
return ProjectResourceWithRawResponse(self._client.project)
506+
507+
@cached_property
508+
def emails(self) -> emails.EmailsResourceWithRawResponse:
509+
from .resources.emails import EmailsResourceWithRawResponse
510+
511+
return EmailsResourceWithRawResponse(self._client.emails)
512+
513+
@cached_property
514+
def pages(self) -> pages.PagesResourceWithRawResponse:
515+
from .resources.pages import PagesResourceWithRawResponse
516+
517+
return PagesResourceWithRawResponse(self._client.pages)
518+
519+
@cached_property
520+
def documents(self) -> documents.DocumentsResourceWithRawResponse:
521+
from .resources.documents import DocumentsResourceWithRawResponse
522+
523+
return DocumentsResourceWithRawResponse(self._client.documents)
458524

459525

460526
class AsyncUnlayerWithRawResponse:
527+
_client: AsyncUnlayer
528+
461529
def __init__(self, client: AsyncUnlayer) -> None:
462-
self.project = project.AsyncProjectResourceWithRawResponse(client.project)
463-
self.emails = emails.AsyncEmailsResourceWithRawResponse(client.emails)
464-
self.pages = pages.AsyncPagesResourceWithRawResponse(client.pages)
465-
self.documents = documents.AsyncDocumentsResourceWithRawResponse(client.documents)
530+
self._client = client
531+
532+
@cached_property
533+
def project(self) -> project.AsyncProjectResourceWithRawResponse:
534+
from .resources.project import AsyncProjectResourceWithRawResponse
535+
536+
return AsyncProjectResourceWithRawResponse(self._client.project)
537+
538+
@cached_property
539+
def emails(self) -> emails.AsyncEmailsResourceWithRawResponse:
540+
from .resources.emails import AsyncEmailsResourceWithRawResponse
541+
542+
return AsyncEmailsResourceWithRawResponse(self._client.emails)
543+
544+
@cached_property
545+
def pages(self) -> pages.AsyncPagesResourceWithRawResponse:
546+
from .resources.pages import AsyncPagesResourceWithRawResponse
547+
548+
return AsyncPagesResourceWithRawResponse(self._client.pages)
549+
550+
@cached_property
551+
def documents(self) -> documents.AsyncDocumentsResourceWithRawResponse:
552+
from .resources.documents import AsyncDocumentsResourceWithRawResponse
553+
554+
return AsyncDocumentsResourceWithRawResponse(self._client.documents)
466555

467556

468557
class UnlayerWithStreamedResponse:
558+
_client: Unlayer
559+
469560
def __init__(self, client: Unlayer) -> None:
470-
self.project = project.ProjectResourceWithStreamingResponse(client.project)
471-
self.emails = emails.EmailsResourceWithStreamingResponse(client.emails)
472-
self.pages = pages.PagesResourceWithStreamingResponse(client.pages)
473-
self.documents = documents.DocumentsResourceWithStreamingResponse(client.documents)
561+
self._client = client
562+
563+
@cached_property
564+
def project(self) -> project.ProjectResourceWithStreamingResponse:
565+
from .resources.project import ProjectResourceWithStreamingResponse
566+
567+
return ProjectResourceWithStreamingResponse(self._client.project)
568+
569+
@cached_property
570+
def emails(self) -> emails.EmailsResourceWithStreamingResponse:
571+
from .resources.emails import EmailsResourceWithStreamingResponse
572+
573+
return EmailsResourceWithStreamingResponse(self._client.emails)
574+
575+
@cached_property
576+
def pages(self) -> pages.PagesResourceWithStreamingResponse:
577+
from .resources.pages import PagesResourceWithStreamingResponse
578+
579+
return PagesResourceWithStreamingResponse(self._client.pages)
580+
581+
@cached_property
582+
def documents(self) -> documents.DocumentsResourceWithStreamingResponse:
583+
from .resources.documents import DocumentsResourceWithStreamingResponse
584+
585+
return DocumentsResourceWithStreamingResponse(self._client.documents)
474586

475587

476588
class AsyncUnlayerWithStreamedResponse:
589+
_client: AsyncUnlayer
590+
477591
def __init__(self, client: AsyncUnlayer) -> None:
478-
self.project = project.AsyncProjectResourceWithStreamingResponse(client.project)
479-
self.emails = emails.AsyncEmailsResourceWithStreamingResponse(client.emails)
480-
self.pages = pages.AsyncPagesResourceWithStreamingResponse(client.pages)
481-
self.documents = documents.AsyncDocumentsResourceWithStreamingResponse(client.documents)
592+
self._client = client
593+
594+
@cached_property
595+
def project(self) -> project.AsyncProjectResourceWithStreamingResponse:
596+
from .resources.project import AsyncProjectResourceWithStreamingResponse
597+
598+
return AsyncProjectResourceWithStreamingResponse(self._client.project)
599+
600+
@cached_property
601+
def emails(self) -> emails.AsyncEmailsResourceWithStreamingResponse:
602+
from .resources.emails import AsyncEmailsResourceWithStreamingResponse
603+
604+
return AsyncEmailsResourceWithStreamingResponse(self._client.emails)
605+
606+
@cached_property
607+
def pages(self) -> pages.AsyncPagesResourceWithStreamingResponse:
608+
from .resources.pages import AsyncPagesResourceWithStreamingResponse
609+
610+
return AsyncPagesResourceWithStreamingResponse(self._client.pages)
611+
612+
@cached_property
613+
def documents(self) -> documents.AsyncDocumentsResourceWithStreamingResponse:
614+
from .resources.documents import AsyncDocumentsResourceWithStreamingResponse
615+
616+
return AsyncDocumentsResourceWithStreamingResponse(self._client.documents)
482617

483618

484619
Client = Unlayer

0 commit comments

Comments
 (0)