Support Client(base_url=...)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by Tom Christie · parent 2af17c8 · Browse files · 3 files·+30 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−7 removed
- tests:+9 added−0 removed
- other:+21 added−7 removed
Provenance
- Commit
9d5aaff- Tree
a08e17d- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
2af17c8
3 files changed+30 added−7 removed
modified:http3/client.py+13 added-3 removed
49 unmodified lines50 cert: CertTypes = None,51 timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,52 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,53 max_redirects: int = DEFAULT_MAX_REDIRECTS,54+ base_url: URLTypes = None,55 dispatch: typing.Union[AsyncDispatcher, Dispatcher] = None,56 app: typing.Callable = None,57 backend: ConcurrencyBackend = None,58 ):20 unmodified lines79 async_dispatch = ThreadedDispatcher(dispatch, backend)80 else:81 async_dispatch = dispatch82 83+ if base_url is None:84+ self.base_url = URL('', allow_relative=True)85+ else:86+ self.base_url = URL(base_url)87+88 self.auth = auth89 self.cookies = Cookies(cookies)90 self.max_redirects = max_redirects91 self.dispatch = async_dispatch151 unmodified lines243 244 # Facilitate relative 'Location' headers, as allowed by RFC 7231.245 # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')246 if url.is_relative_url:241- url = url.resolve_with(request.url)247+ url = request.url.join(url)248 249 # Attach previous fragment if needed (RFC 7231 7.1.2)250 if request.url.fragment and not url.fragment:251 url = url.copy_with(fragment=request.url.fragment)259 unmodified lines511 cert: CertTypes = None,512 verify: VerifyTypes = None,513 timeout: TimeoutTypes = None,514 ) -> AsyncResponse:515+ url = self.base_url.join(url)516+ cookies = self.merge_cookies(cookies)517 request = AsyncRequest(518 method,519 url,520 data=data,521 files=files,522 json=json,523 params=params,524 headers=headers,517- cookies=self.merge_cookies(cookies),525+ cookies=cookies,526 )527 response = await self.send(528 request,529 stream=stream,62 unmodified lines592 cert: CertTypes = None,593 verify: VerifyTypes = None,594 timeout: TimeoutTypes = None,595 ) -> Response:596+ url = self.base_url.join(url)597+ cookies = self.merge_cookies(cookies)598 request = AsyncRequest(599 method,600 url,601 data=self._async_request_data(data),602 files=files,603 json=json,604 params=params,605 headers=headers,596- cookies=self.merge_cookies(cookies),606+ cookies=cookies,607 )608 concurrency_backend = self.concurrency_backend609 610 coroutine = self.send271 unmodified linesmodified:http3/models.py+8 added-4 removed
181 unmodified lines182 183 def copy_with(self, **kwargs: typing.Any) -> "URL":184 return URL(self.components.copy_with(**kwargs))185 186- def resolve_with(self, base_url: URLTypes) -> "URL":186+ def join(self, relative_url: URLTypes) -> "URL":187 """188- Return an absolute URL, using base_url as the base.188+ Return an absolute URL, using given this URL as the base.189 """190+ if self.is_relative_url:191+ return URL(relative_url)192+193 # We drop any fragment portion, because RFC 3986 strictly194 # treats URLs with a fragment portion as not being absolute URLs.192- base_url = URL(base_url).copy_with(fragment=None)193- return URL(self.components.resolve_with(base_url.components))195+ base_components = self.components.copy_with(fragment=None)196+ relative_url = URL(relative_url, allow_relative=True)197+ return URL(relative_url.components.resolve_with(base_components))198 199 def __hash__(self) -> int:200 return hash(str(self))201 948 unmodified linesmodified:tests/client/test_client.py+9 added
139 unmodified lines140 with http3.Client() as http:141 response = http.delete("http://127.0.0.1:8000/")142 assert response.status_code == 200143 assert response.reason_phrase == "OK"144+145+146+@threadpool147+def test_base_url(server):148+ base_url = "http://127.0.0.1:8000/"149+ with http3.Client(base_url=base_url) as http:150+ response = http.get('/')151+ assert response.status_code == 200152+ assert str(response.url) == base_url