Add query_params support
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 211bef1 · Browse files · 4 files·+56 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+56 added−12 removed
- tests:+10 added−0 removed
- other:+46 added−12 removed
Provenance
- Commit
faededa- Tree
3c3ca88- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
211bef1
4 files changed+56 added−12 removed
modified:httpcore/client.py+27 added-2 removed
13 unmodified lines14 SSLConfig,15 TimeoutConfig,16 )17 from .dispatch.connection_pool import ConnectionPool18-from .models import URL, ByteOrByteStream, HeaderTypes, Request, Response, URLTypes18+from .models import (19+ URL,20+ ByteOrByteStream,21+ HeaderTypes,22+ QueryParamTypes,23+ Request,24+ Response,25+ URLTypes,26+)27 28 29 class Client:30 def __init__(18 unmodified lines49 method: str,50 url: URLTypes,51 *,52 content: ByteOrByteStream = b"",53+ query_params: QueryParamTypes = None,54 headers: HeaderTypes = None,55 stream: bool = False,56 allow_redirects: bool = True,57 ssl: SSLConfig = None,58 timeout: TimeoutConfig = None,59 ) -> Response:51- request = Request(method, url, headers=headers, content=content)60+ request = Request(61+ method, url, query_params=query_params, headers=headers, content=content62+ )63 self.prepare_request(request)64 response = await self.send(65 request,66 stream=stream,6 unmodified lines73 async def get(74 self,75 url: URLTypes,76 *,77+ query_params: QueryParamTypes = None,78 headers: HeaderTypes = None,79 stream: bool = False,80 allow_redirects: bool = True,81 ssl: SSLConfig = None,1 unmodified line83 ) -> Response:84 return await self.request(85 "GET",86 url,87+ query_params=query_params,88 headers=headers,89 stream=stream,90 allow_redirects=allow_redirects,91 ssl=ssl,3 unmodified lines95 async def options(96 self,97 url: URLTypes,98 *,99+ query_params: QueryParamTypes = None,100 headers: HeaderTypes = None,101 stream: bool = False,102 allow_redirects: bool = True,103 ssl: SSLConfig = None,1 unmodified line105 ) -> Response:106 return await self.request(107 "OPTIONS",108 url,109+ query_params=query_params,110 headers=headers,111 stream=stream,112 allow_redirects=allow_redirects,113 ssl=ssl,3 unmodified lines117 async def head(118 self,119 url: URLTypes,120 *,121+ query_params: QueryParamTypes = None,122 headers: HeaderTypes = None,123 stream: bool = False,124 allow_redirects: bool = False, # Note: Differs to usual default.125 ssl: SSLConfig = None,1 unmodified line127 ) -> Response:128 return await self.request(129 "HEAD",130 url,131+ query_params=query_params,132 headers=headers,133 stream=stream,134 allow_redirects=allow_redirects,135 ssl=ssl,4 unmodified lines140 self,141 url: URLTypes,142 *,143 content: ByteOrByteStream = b"",144+ query_params: QueryParamTypes = None,145 headers: HeaderTypes = None,146 stream: bool = False,147 allow_redirects: bool = True,148 ssl: SSLConfig = None,2 unmodified lines151 return await self.request(152 "POST",153 url,154 content=content,155+ query_params=query_params,156 headers=headers,157 stream=stream,158 allow_redirects=allow_redirects,159 ssl=ssl,4 unmodified lines164 self,165 url: URLTypes,166 *,167 content: ByteOrByteStream = b"",168+ query_params: QueryParamTypes = None,169 headers: HeaderTypes = None,170 stream: bool = False,171 allow_redirects: bool = True,172 ssl: SSLConfig = None,2 unmodified lines175 return await self.request(176 "PUT",177 url,178 content=content,179+ query_params=query_params,180 headers=headers,181 stream=stream,182 allow_redirects=allow_redirects,183 ssl=ssl,4 unmodified lines188 self,189 url: URLTypes,190 *,191 content: ByteOrByteStream = b"",192+ query_params: QueryParamTypes = None,193 headers: HeaderTypes = None,194 stream: bool = False,195 allow_redirects: bool = True,196 ssl: SSLConfig = None,2 unmodified lines199 return await self.request(200 "PATCH",201 url,202 content=content,203+ query_params=query_params,204 headers=headers,205 stream=stream,206 allow_redirects=allow_redirects,207 ssl=ssl,4 unmodified lines212 self,213 url: URLTypes,214 *,215 content: ByteOrByteStream = b"",216+ query_params: QueryParamTypes = None,217 headers: HeaderTypes = None,218 stream: bool = False,219 allow_redirects: bool = True,220 ssl: SSLConfig = None,2 unmodified lines223 return await self.request(224 "DELETE",225 url,226 content=content,227+ query_params=query_params,228 headers=headers,229 stream=stream,230 allow_redirects=allow_redirects,231 ssl=ssl,40 unmodified linesmodified:httpcore/models.py+11 added-2 removed
47 unmodified lines48 49 50 class URL:51 def __init__(52- self, url: URLTypes, allow_relative: bool = False, params: QueryParamTypes = None52+ self,53+ url: URLTypes,54+ allow_relative: bool = False,55+ query_params: QueryParamTypes = None,56 ) -> None:57 if isinstance(url, rfc3986.uri.URIReference):58 self.components = url59 elif isinstance(url, str):9 unmodified lines69 70 # Normalize scheme and domain name.71 self.components = self.components.normalize()72 73+ # Add any query parameters.74+ if query_params:75+ query_string = str(QueryParams(query_params))76+ self.components = self.components.copy_with(query=query_string)77+78 # Enforce absolute URLs by default.79 if not allow_relative:80 if not self.scheme:81 raise InvalidURL("No scheme included in URL.")369 unmodified lines451 self,452 method: str,453 url: typing.Union[str, URL],454 *,455+ query_params: QueryParamTypes = None,456 headers: HeaderTypes = None,457 content: ByteOrByteStream = b"",458 ):459 self.method = method.upper()451- self.url = URL(url) if isinstance(url, str) else url460+ self.url = URL(url, query_params=query_params)461 if isinstance(content, bytes):462 self.is_streaming = False463 self.content = content464 else:278 unmodified linesmodified:tests/models/test_url.py+10 added
21 unmodified lines22 23 new = url.copy_with(scheme="http")24 assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")25 assert new.scheme == "http"26+27+28+def test_url_query_params():29+ url = URL("https://example.org:123/path/to/somewhere", query_params={"a": "123"})30+ assert str(url) == "https://example.org:123/path/to/somewhere?a=123"31+32+ url = URL(33+ "https://example.org:123/path/to/somewhere?b=456", query_params={"a": "123"}34+ )35+ assert str(url) == "https://example.org:123/path/to/somewhere?a=123"modified:README.md+8 added-8 removed
85 unmodified lines86 >>> response = await client.get('https://example.org')87 ```88 89 * `def __init__([ssl], [timeout], [pool_limits], [max_redirects])`90-* `async def .request(method, url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`91-* `async def .get(url, [headers], [stream], [allow_redirects], [ssl], [timeout])`92-* `async def .options(url, [headers], [stream], [allow_redirects], [ssl], [timeout])`93-* `async def .head(url, [headers], [stream], [allow_redirects], [ssl], [timeout])`94-* `async def .post(url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`95-* `async def .put(url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`96-* `async def .patch(url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`97-* `async def .delete(url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`90+* `async def .request(method, url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`91+* `async def .get(url, [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`92+* `async def .options(url, [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`93+* `async def .head(url, [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`94+* `async def .post(url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`95+* `async def .put(url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`96+* `async def .patch(url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`97+* `async def .delete(url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`98 * `def .prepare_request(request)`99 * `async def .send(request, [stream], [allow_redirects], [ssl], [timeout])`100 * `async def .close()`101 123 unmodified lines