1515
1616"""Provides factories for different flavors of httpx.AsyncHTTPTransport."""
1717
18- from hishel import AsyncCacheTransport , AsyncInMemoryStorage
19- from httpx import AsyncHTTPTransport , Limits
18+ from hishel import AsyncCacheTransport , AsyncInMemoryStorage , Controller
19+ from httpx import AsyncBaseTransport , AsyncHTTPTransport , Limits
2020
2121from .config import CompositeCacheConfig , CompositeConfig
2222from .ratelimiting import AsyncRateLimitingTransport
@@ -28,11 +28,21 @@ class CompositeTransportFactory:
2828
2929 @classmethod
3030 def _create_common_transport_layers (
31- cls , config : CompositeConfig , limits : Limits | None = None
31+ cls ,
32+ config : CompositeConfig ,
33+ base_transport : AsyncBaseTransport | None = None ,
34+ limits : Limits | None = None ,
3235 ):
33- """Creates wrapped transports reused between different factory methods."""
36+ """Creates wrapped transports reused between different factory methods.
37+
38+ If provided, limits are applied to the AsyncHTTPTransport instance this method creates.
39+ If provided, a custom base_transport class is used and any limits are ignored.
40+ Those have to be provided directly to the custom base_transport passed into this method.
41+ """
3442 base_transport = (
35- AsyncHTTPTransport (limits = limits ) if limits else AsyncHTTPTransport ()
43+ base_transport or AsyncHTTPTransport (limits = limits )
44+ if limits
45+ else AsyncHTTPTransport ()
3646 )
3747 ratelimiting_transport = AsyncRateLimitingTransport (
3848 config = config , transport = base_transport
@@ -44,16 +54,44 @@ def _create_common_transport_layers(
4454
4555 @classmethod
4656 def create_ratelimiting_retry_transport (
47- cls , config : CompositeConfig , limits : Limits | None = None
57+ cls ,
58+ config : CompositeConfig ,
59+ base_transport : AsyncBaseTransport | None = None ,
60+ limits : Limits | None = None ,
4861 ) -> AsyncRetryTransport :
49- """Creates a retry transport, wrapping a rate limiting transport, wrapping an AsyncHTTPTransport."""
50- return cls ._create_common_transport_layers (config , limits = limits )
62+ """Creates a retry transport, wrapping, in sequence, a rate limiting transport and AsyncHTTPTransport.
63+
64+ If provided, limits are applied to the wrapped AsyncHTTPTransport instance.
65+ If provided, a custom base_transport class is used and any limits are ignored.
66+ Those have to be provided directly to the custom base_transport passed into this method.
67+ """
68+ return cls ._create_common_transport_layers (
69+ config , base_transport = base_transport , limits = limits
70+ )
5171
5272 @classmethod
5373 def create_cached_ratelimiting_retry_transport (
54- cls , config : CompositeCacheConfig , limits : Limits | None = None
74+ cls ,
75+ config : CompositeCacheConfig ,
76+ base_transport : AsyncBaseTransport | None = None ,
77+ limits : Limits | None = None ,
5578 ) -> AsyncCacheTransport :
56- """Creates a retry transport, wrapping a rate limiting transport, wrapping a cache transport, wrapping an AsyncHTTPTransport."""
57- storage = AsyncInMemoryStorage (ttl = config .cache_ttl )
58- retry_transport = cls ._create_common_transport_layers (config , limits = limits )
59- return AsyncCacheTransport (transport = retry_transport , storage = storage )
79+ """Creates a cache transport, wrapping, in sequence, a retry, rate limiting transport and AsyncHTTPTransport.
80+
81+ If provided, limits are applied to the wrapped AsyncHTTPTransport instance.
82+ If provided, a custom base_transport class is used and any limits are ignored.
83+ Those have to be provided directly to the custom base_transport passed into this method.
84+ """
85+ retry_transport = cls ._create_common_transport_layers (
86+ config , base_transport = base_transport , limits = limits
87+ )
88+ controller = Controller (
89+ cacheable_methods = config .client_cacheable_methods ,
90+ cacheable_status_codes = config .client_cacheable_status_codes ,
91+ )
92+ storage = AsyncInMemoryStorage (
93+ ttl = config .client_cache_ttl , capacity = config .client_cache_capacity
94+ )
95+ return AsyncCacheTransport (
96+ controller = controller , transport = retry_transport , storage = storage
97+ )
0 commit comments