Skip to content

Commit 32fb2cc

Browse files
chore: speedup initial import
1 parent 6dbed91 commit 32fb2cc

1 file changed

Lines changed: 151 additions & 55 deletions

File tree

src/postgrid/_client.py

Lines changed: 151 additions & 55 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, Mapping
6+
from typing import TYPE_CHECKING, Any, Mapping
77
from typing_extensions import Self, override
88

99
import httpx
@@ -21,16 +21,24 @@
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 address_verification, intl_address_verification
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.print_mail import print_mail
33+
34+
if TYPE_CHECKING:
35+
from .resources import print_mail, address_verification, intl_address_verification
36+
from .resources.address_verification import AddressVerificationResource, AsyncAddressVerificationResource
37+
from .resources.print_mail.print_mail import PrintMailResource, AsyncPrintMailResource
38+
from .resources.intl_address_verification import (
39+
IntlAddressVerificationResource,
40+
AsyncIntlAddressVerificationResource,
41+
)
3442

3543
__all__ = [
3644
"Timeout",
@@ -45,12 +53,6 @@
4553

4654

4755
class PostGrid(SyncAPIClient):
48-
address_verification: address_verification.AddressVerificationResource
49-
intl_address_verification: intl_address_verification.IntlAddressVerificationResource
50-
print_mail: print_mail.PrintMailResource
51-
with_raw_response: PostGridWithRawResponse
52-
with_streaming_response: PostGridWithStreamedResponse
53-
5456
# client options
5557
address_verification_api_key: str | None
5658
print_mail_api_key: str | None
@@ -109,11 +111,31 @@ def __init__(
109111
_strict_response_validation=_strict_response_validation,
110112
)
111113

112-
self.address_verification = address_verification.AddressVerificationResource(self)
113-
self.intl_address_verification = intl_address_verification.IntlAddressVerificationResource(self)
114-
self.print_mail = print_mail.PrintMailResource(self)
115-
self.with_raw_response = PostGridWithRawResponse(self)
116-
self.with_streaming_response = PostGridWithStreamedResponse(self)
114+
@cached_property
115+
def address_verification(self) -> AddressVerificationResource:
116+
from .resources.address_verification import AddressVerificationResource
117+
118+
return AddressVerificationResource(self)
119+
120+
@cached_property
121+
def intl_address_verification(self) -> IntlAddressVerificationResource:
122+
from .resources.intl_address_verification import IntlAddressVerificationResource
123+
124+
return IntlAddressVerificationResource(self)
125+
126+
@cached_property
127+
def print_mail(self) -> PrintMailResource:
128+
from .resources.print_mail import PrintMailResource
129+
130+
return PrintMailResource(self)
131+
132+
@cached_property
133+
def with_raw_response(self) -> PostGridWithRawResponse:
134+
return PostGridWithRawResponse(self)
135+
136+
@cached_property
137+
def with_streaming_response(self) -> PostGridWithStreamedResponse:
138+
return PostGridWithStreamedResponse(self)
117139

118140
@override
119141
def _prepare_request(
@@ -266,12 +288,6 @@ def _make_status_error(
266288

267289

268290
class AsyncPostGrid(AsyncAPIClient):
269-
address_verification: address_verification.AsyncAddressVerificationResource
270-
intl_address_verification: intl_address_verification.AsyncIntlAddressVerificationResource
271-
print_mail: print_mail.AsyncPrintMailResource
272-
with_raw_response: AsyncPostGridWithRawResponse
273-
with_streaming_response: AsyncPostGridWithStreamedResponse
274-
275291
# client options
276292
address_verification_api_key: str | None
277293
print_mail_api_key: str | None
@@ -330,11 +346,31 @@ def __init__(
330346
_strict_response_validation=_strict_response_validation,
331347
)
332348

333-
self.address_verification = address_verification.AsyncAddressVerificationResource(self)
334-
self.intl_address_verification = intl_address_verification.AsyncIntlAddressVerificationResource(self)
335-
self.print_mail = print_mail.AsyncPrintMailResource(self)
336-
self.with_raw_response = AsyncPostGridWithRawResponse(self)
337-
self.with_streaming_response = AsyncPostGridWithStreamedResponse(self)
349+
@cached_property
350+
def address_verification(self) -> AsyncAddressVerificationResource:
351+
from .resources.address_verification import AsyncAddressVerificationResource
352+
353+
return AsyncAddressVerificationResource(self)
354+
355+
@cached_property
356+
def intl_address_verification(self) -> AsyncIntlAddressVerificationResource:
357+
from .resources.intl_address_verification import AsyncIntlAddressVerificationResource
358+
359+
return AsyncIntlAddressVerificationResource(self)
360+
361+
@cached_property
362+
def print_mail(self) -> AsyncPrintMailResource:
363+
from .resources.print_mail import AsyncPrintMailResource
364+
365+
return AsyncPrintMailResource(self)
366+
367+
@cached_property
368+
def with_raw_response(self) -> AsyncPostGridWithRawResponse:
369+
return AsyncPostGridWithRawResponse(self)
370+
371+
@cached_property
372+
def with_streaming_response(self) -> AsyncPostGridWithStreamedResponse:
373+
return AsyncPostGridWithStreamedResponse(self)
338374

339375
@override
340376
async def _prepare_request(
@@ -487,49 +523,109 @@ def _make_status_error(
487523

488524

489525
class PostGridWithRawResponse:
526+
_client: PostGrid
527+
490528
def __init__(self, client: PostGrid) -> None:
491-
self.address_verification = address_verification.AddressVerificationResourceWithRawResponse(
492-
client.address_verification
493-
)
494-
self.intl_address_verification = intl_address_verification.IntlAddressVerificationResourceWithRawResponse(
495-
client.intl_address_verification
496-
)
497-
self.print_mail = print_mail.PrintMailResourceWithRawResponse(client.print_mail)
529+
self._client = client
530+
531+
@cached_property
532+
def address_verification(self) -> address_verification.AddressVerificationResourceWithRawResponse:
533+
from .resources.address_verification import AddressVerificationResourceWithRawResponse
534+
535+
return AddressVerificationResourceWithRawResponse(self._client.address_verification)
536+
537+
@cached_property
538+
def intl_address_verification(self) -> intl_address_verification.IntlAddressVerificationResourceWithRawResponse:
539+
from .resources.intl_address_verification import IntlAddressVerificationResourceWithRawResponse
540+
541+
return IntlAddressVerificationResourceWithRawResponse(self._client.intl_address_verification)
542+
543+
@cached_property
544+
def print_mail(self) -> print_mail.PrintMailResourceWithRawResponse:
545+
from .resources.print_mail import PrintMailResourceWithRawResponse
546+
547+
return PrintMailResourceWithRawResponse(self._client.print_mail)
498548

499549

500550
class AsyncPostGridWithRawResponse:
551+
_client: AsyncPostGrid
552+
501553
def __init__(self, client: AsyncPostGrid) -> None:
502-
self.address_verification = address_verification.AsyncAddressVerificationResourceWithRawResponse(
503-
client.address_verification
504-
)
505-
self.intl_address_verification = intl_address_verification.AsyncIntlAddressVerificationResourceWithRawResponse(
506-
client.intl_address_verification
507-
)
508-
self.print_mail = print_mail.AsyncPrintMailResourceWithRawResponse(client.print_mail)
554+
self._client = client
555+
556+
@cached_property
557+
def address_verification(self) -> address_verification.AsyncAddressVerificationResourceWithRawResponse:
558+
from .resources.address_verification import AsyncAddressVerificationResourceWithRawResponse
559+
560+
return AsyncAddressVerificationResourceWithRawResponse(self._client.address_verification)
561+
562+
@cached_property
563+
def intl_address_verification(
564+
self,
565+
) -> intl_address_verification.AsyncIntlAddressVerificationResourceWithRawResponse:
566+
from .resources.intl_address_verification import AsyncIntlAddressVerificationResourceWithRawResponse
567+
568+
return AsyncIntlAddressVerificationResourceWithRawResponse(self._client.intl_address_verification)
569+
570+
@cached_property
571+
def print_mail(self) -> print_mail.AsyncPrintMailResourceWithRawResponse:
572+
from .resources.print_mail import AsyncPrintMailResourceWithRawResponse
573+
574+
return AsyncPrintMailResourceWithRawResponse(self._client.print_mail)
509575

510576

511577
class PostGridWithStreamedResponse:
578+
_client: PostGrid
579+
512580
def __init__(self, client: PostGrid) -> None:
513-
self.address_verification = address_verification.AddressVerificationResourceWithStreamingResponse(
514-
client.address_verification
515-
)
516-
self.intl_address_verification = intl_address_verification.IntlAddressVerificationResourceWithStreamingResponse(
517-
client.intl_address_verification
518-
)
519-
self.print_mail = print_mail.PrintMailResourceWithStreamingResponse(client.print_mail)
581+
self._client = client
582+
583+
@cached_property
584+
def address_verification(self) -> address_verification.AddressVerificationResourceWithStreamingResponse:
585+
from .resources.address_verification import AddressVerificationResourceWithStreamingResponse
586+
587+
return AddressVerificationResourceWithStreamingResponse(self._client.address_verification)
588+
589+
@cached_property
590+
def intl_address_verification(
591+
self,
592+
) -> intl_address_verification.IntlAddressVerificationResourceWithStreamingResponse:
593+
from .resources.intl_address_verification import IntlAddressVerificationResourceWithStreamingResponse
594+
595+
return IntlAddressVerificationResourceWithStreamingResponse(self._client.intl_address_verification)
596+
597+
@cached_property
598+
def print_mail(self) -> print_mail.PrintMailResourceWithStreamingResponse:
599+
from .resources.print_mail import PrintMailResourceWithStreamingResponse
600+
601+
return PrintMailResourceWithStreamingResponse(self._client.print_mail)
520602

521603

522604
class AsyncPostGridWithStreamedResponse:
605+
_client: AsyncPostGrid
606+
523607
def __init__(self, client: AsyncPostGrid) -> None:
524-
self.address_verification = address_verification.AsyncAddressVerificationResourceWithStreamingResponse(
525-
client.address_verification
526-
)
527-
self.intl_address_verification = (
528-
intl_address_verification.AsyncIntlAddressVerificationResourceWithStreamingResponse(
529-
client.intl_address_verification
530-
)
531-
)
532-
self.print_mail = print_mail.AsyncPrintMailResourceWithStreamingResponse(client.print_mail)
608+
self._client = client
609+
610+
@cached_property
611+
def address_verification(self) -> address_verification.AsyncAddressVerificationResourceWithStreamingResponse:
612+
from .resources.address_verification import AsyncAddressVerificationResourceWithStreamingResponse
613+
614+
return AsyncAddressVerificationResourceWithStreamingResponse(self._client.address_verification)
615+
616+
@cached_property
617+
def intl_address_verification(
618+
self,
619+
) -> intl_address_verification.AsyncIntlAddressVerificationResourceWithStreamingResponse:
620+
from .resources.intl_address_verification import AsyncIntlAddressVerificationResourceWithStreamingResponse
621+
622+
return AsyncIntlAddressVerificationResourceWithStreamingResponse(self._client.intl_address_verification)
623+
624+
@cached_property
625+
def print_mail(self) -> print_mail.AsyncPrintMailResourceWithStreamingResponse:
626+
from .resources.print_mail import AsyncPrintMailResourceWithStreamingResponse
627+
628+
return AsyncPrintMailResourceWithStreamingResponse(self._client.print_mail)
533629

534630

535631
Client = PostGrid

0 commit comments

Comments
 (0)