-
-
Notifications
You must be signed in to change notification settings - Fork 857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Mount API #1362
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
69cbc1c
Add support for Mount API
tomchristie 492e742
Add test cases
tomchristie 618e56e
Add test case for all: mounted transport
tomchristie c827d02
Merge branch 'master' into mount-api
tomchristie 8c5eb57
Merge branch 'master' into mount-api
tomchristie b5dbfd8
Merge branch 'master' into mount-api
tomchristie 9dc09ea
Use 'transport' variable, in preference to 'proxy'
tomchristie cd60783
Add docs for mounted transports
tomchristie 5d0ac3b
Merge branch 'master' into mount-api
tomchristie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,7 @@ def __init__( | |
cookies: CookieTypes = None, | ||
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, | ||
max_redirects: int = DEFAULT_MAX_REDIRECTS, | ||
event_hooks: typing.Dict[str, typing.List[typing.Callable]] = None, | ||
event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None, | ||
base_url: URLTypes = "", | ||
trust_env: bool = True, | ||
): | ||
|
@@ -561,11 +561,12 @@ def __init__( | |
cert: CertTypes = None, | ||
http2: bool = False, | ||
proxies: ProxiesTypes = None, | ||
mounts: typing.Mapping[str, httpcore.SyncHTTPTransport] = None, | ||
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, | ||
limits: Limits = DEFAULT_LIMITS, | ||
pool_limits: Limits = None, | ||
max_redirects: int = DEFAULT_MAX_REDIRECTS, | ||
event_hooks: typing.Dict[str, typing.List[typing.Callable]] = None, | ||
event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None, | ||
base_url: URLTypes = "", | ||
transport: httpcore.SyncHTTPTransport = None, | ||
app: typing.Callable = None, | ||
|
@@ -611,7 +612,7 @@ def __init__( | |
app=app, | ||
trust_env=trust_env, | ||
) | ||
self._proxies: typing.Dict[ | ||
self._mounts: typing.Dict[ | ||
URLPattern, typing.Optional[httpcore.SyncHTTPTransport] | ||
] = { | ||
URLPattern(key): None | ||
|
@@ -626,7 +627,12 @@ def __init__( | |
) | ||
for key, proxy in proxy_map.items() | ||
} | ||
self._proxies = dict(sorted(self._proxies.items())) | ||
if mounts is not None: | ||
self._mounts.update( | ||
{URLPattern(key): transport for key, transport in mounts.items()} | ||
) | ||
|
||
self._mounts = dict(sorted(self._mounts.items())) | ||
|
||
def _init_transport( | ||
self, | ||
|
@@ -681,7 +687,7 @@ def _transport_for_url(self, url: URL) -> httpcore.SyncHTTPTransport: | |
Returns the transport instance that should be used for a given URL. | ||
This will either be the standard connection pool, or a proxy. | ||
""" | ||
for pattern, transport in self._proxies.items(): | ||
for pattern, transport in self._mounts.items(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So only the thing, why we introduce Then why it's a dictionary? May we have a list with tuples there? Update: the only benefit we get from |
||
if pattern.matches(url): | ||
return self._transport if transport is None else transport | ||
|
||
|
@@ -1109,17 +1115,17 @@ def close(self) -> None: | |
self._state = ClientState.CLOSED | ||
|
||
self._transport.close() | ||
for proxy in self._proxies.values(): | ||
if proxy is not None: | ||
proxy.close() | ||
for transport in self._mounts.values(): | ||
if transport is not None: | ||
transport.close() | ||
|
||
def __enter__(self: T) -> T: | ||
self._state = ClientState.OPENED | ||
|
||
self._transport.__enter__() | ||
for proxy in self._proxies.values(): | ||
if proxy is not None: | ||
proxy.__enter__() | ||
for transport in self._mounts.values(): | ||
if transport is not None: | ||
transport.__enter__() | ||
return self | ||
|
||
def __exit__( | ||
|
@@ -1131,9 +1137,9 @@ def __exit__( | |
self._state = ClientState.CLOSED | ||
|
||
self._transport.__exit__(exc_type, exc_value, traceback) | ||
for proxy in self._proxies.values(): | ||
if proxy is not None: | ||
proxy.__exit__(exc_type, exc_value, traceback) | ||
for transport in self._mounts.values(): | ||
if transport is not None: | ||
transport.__exit__(exc_type, exc_value, traceback) | ||
|
||
def __del__(self) -> None: | ||
self.close() | ||
|
@@ -1198,11 +1204,12 @@ def __init__( | |
cert: CertTypes = None, | ||
http2: bool = False, | ||
proxies: ProxiesTypes = None, | ||
mounts: typing.Mapping[str, httpcore.AsyncHTTPTransport] = None, | ||
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG, | ||
limits: Limits = DEFAULT_LIMITS, | ||
pool_limits: Limits = None, | ||
max_redirects: int = DEFAULT_MAX_REDIRECTS, | ||
event_hooks: typing.Dict[str, typing.List[typing.Callable]] = None, | ||
event_hooks: typing.Mapping[str, typing.List[typing.Callable]] = None, | ||
base_url: URLTypes = "", | ||
transport: httpcore.AsyncHTTPTransport = None, | ||
app: typing.Callable = None, | ||
|
@@ -1249,7 +1256,7 @@ def __init__( | |
trust_env=trust_env, | ||
) | ||
|
||
self._proxies: typing.Dict[ | ||
self._mounts: typing.Dict[ | ||
URLPattern, typing.Optional[httpcore.AsyncHTTPTransport] | ||
] = { | ||
URLPattern(key): None | ||
|
@@ -1264,7 +1271,11 @@ def __init__( | |
) | ||
for key, proxy in proxy_map.items() | ||
} | ||
self._proxies = dict(sorted(self._proxies.items())) | ||
if mounts is not None: | ||
self._mounts.update( | ||
{URLPattern(key): transport for key, transport in mounts.items()} | ||
) | ||
self._mounts = dict(sorted(self._mounts.items())) | ||
|
||
def _init_transport( | ||
self, | ||
|
@@ -1319,7 +1330,7 @@ def _transport_for_url(self, url: URL) -> httpcore.AsyncHTTPTransport: | |
Returns the transport instance that should be used for a given URL. | ||
This will either be the standard connection pool, or a proxy. | ||
""" | ||
for pattern, transport in self._proxies.items(): | ||
for pattern, transport in self._mounts.items(): | ||
if pattern.matches(url): | ||
return self._transport if transport is None else transport | ||
|
||
|
@@ -1499,7 +1510,7 @@ async def _send_single_request( | |
await timer.async_start() | ||
|
||
with map_exceptions(HTTPCORE_EXC_MAP, request=request): | ||
(status_code, headers, stream, ext,) = await transport.arequest( | ||
(status_code, headers, stream, ext) = await transport.arequest( | ||
request.method.encode(), | ||
request.url.raw, | ||
headers=request.headers.raw, | ||
|
@@ -1750,15 +1761,15 @@ async def aclose(self) -> None: | |
self._state = ClientState.CLOSED | ||
|
||
await self._transport.aclose() | ||
for proxy in self._proxies.values(): | ||
for proxy in self._mounts.values(): | ||
if proxy is not None: | ||
await proxy.aclose() | ||
|
||
async def __aenter__(self: U) -> U: | ||
self._state = ClientState.OPENED | ||
|
||
await self._transport.__aenter__() | ||
for proxy in self._proxies.values(): | ||
for proxy in self._mounts.values(): | ||
if proxy is not None: | ||
await proxy.__aenter__() | ||
return self | ||
|
@@ -1772,7 +1783,7 @@ async def __aexit__( | |
self._state = ClientState.CLOSED | ||
|
||
await self._transport.__aexit__(exc_type, exc_value, traceback) | ||
for proxy in self._proxies.values(): | ||
for proxy in self._mounts.values(): | ||
if proxy is not None: | ||
await proxy.__aexit__(exc_type, exc_value, traceback) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I understand correctly that the mounts overrides proxies? Like:
Do we need to outline it somewhere?