```
* `def __init__([ssl], [timeout], [pool_limits], [max_redirects])`
-* `async def .request(method, url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`
-* `async def .get(url, [headers], [stream], [allow_redirects], [ssl], [timeout])`
-* `async def .options(url, [headers], [stream], [allow_redirects], [ssl], [timeout])`
-* `async def .head(url, [headers], [stream], [allow_redirects], [ssl], [timeout])`
-* `async def .post(url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`
-* `async def .put(url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`
-* `async def .patch(url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`
-* `async def .delete(url, [content], [headers], [stream], [allow_redirects], [ssl], [timeout])`
+* `async def .request(method, url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`
+* `async def .get(url, [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`
+* `async def .options(url, [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`
+* `async def .head(url, [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`
+* `async def .post(url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`
+* `async def .put(url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`
+* `async def .patch(url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`
+* `async def .delete(url, [content], [query_params], [headers], [stream], [allow_redirects], [ssl], [timeout])`
* `def .prepare_request(request)`
* `async def .send(request, [stream], [allow_redirects], [ssl], [timeout])`
* `async def .close()`
TimeoutConfig,
)
from .dispatch.connection_pool import ConnectionPool
-from .models import URL, ByteOrByteStream, HeaderTypes, Request, Response, URLTypes
+from .models import (
+ URL,
+ ByteOrByteStream,
+ HeaderTypes,
+ QueryParamTypes,
+ Request,
+ Response,
+ URLTypes,
+)
class Client:
url: URLTypes,
*,
content: ByteOrByteStream = b"",
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
allow_redirects: bool = True,
ssl: SSLConfig = None,
timeout: TimeoutConfig = None,
) -> Response:
- request = Request(method, url, headers=headers, content=content)
+ request = Request(
+ method, url, query_params=query_params, headers=headers, content=content
+ )
self.prepare_request(request)
response = await self.send(
request,
self,
url: URLTypes,
*,
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
allow_redirects: bool = True,
return await self.request(
"GET",
url,
+ query_params=query_params,
headers=headers,
stream=stream,
allow_redirects=allow_redirects,
self,
url: URLTypes,
*,
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
allow_redirects: bool = True,
return await self.request(
"OPTIONS",
url,
+ query_params=query_params,
headers=headers,
stream=stream,
allow_redirects=allow_redirects,
self,
url: URLTypes,
*,
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
allow_redirects: bool = False, # Note: Differs to usual default.
return await self.request(
"HEAD",
url,
+ query_params=query_params,
headers=headers,
stream=stream,
allow_redirects=allow_redirects,
url: URLTypes,
*,
content: ByteOrByteStream = b"",
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
allow_redirects: bool = True,
"POST",
url,
content=content,
+ query_params=query_params,
headers=headers,
stream=stream,
allow_redirects=allow_redirects,
url: URLTypes,
*,
content: ByteOrByteStream = b"",
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
allow_redirects: bool = True,
"PUT",
url,
content=content,
+ query_params=query_params,
headers=headers,
stream=stream,
allow_redirects=allow_redirects,
url: URLTypes,
*,
content: ByteOrByteStream = b"",
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
allow_redirects: bool = True,
"PATCH",
url,
content=content,
+ query_params=query_params,
headers=headers,
stream=stream,
allow_redirects=allow_redirects,
url: URLTypes,
*,
content: ByteOrByteStream = b"",
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
stream: bool = False,
allow_redirects: bool = True,
"DELETE",
url,
content=content,
+ query_params=query_params,
headers=headers,
stream=stream,
allow_redirects=allow_redirects,
class URL:
def __init__(
- self, url: URLTypes, allow_relative: bool = False, params: QueryParamTypes = None
+ self,
+ url: URLTypes,
+ allow_relative: bool = False,
+ query_params: QueryParamTypes = None,
) -> None:
if isinstance(url, rfc3986.uri.URIReference):
self.components = url
# Normalize scheme and domain name.
self.components = self.components.normalize()
+ # Add any query parameters.
+ if query_params:
+ query_string = str(QueryParams(query_params))
+ self.components = self.components.copy_with(query=query_string)
+
# Enforce absolute URLs by default.
if not allow_relative:
if not self.scheme:
method: str,
url: typing.Union[str, URL],
*,
+ query_params: QueryParamTypes = None,
headers: HeaderTypes = None,
content: ByteOrByteStream = b"",
):
self.method = method.upper()
- self.url = URL(url) if isinstance(url, str) else url
+ self.url = URL(url, query_params=query_params)
if isinstance(content, bytes):
self.is_streaming = False
self.content = content
new = url.copy_with(scheme="http")
assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")
assert new.scheme == "http"
+
+
+def test_url_query_params():
+ url = URL("https://example.org:123/path/to/somewhere", query_params={"a": "123"})
+ assert str(url) == "https://example.org:123/path/to/somewhere?a=123"
+
+ url = URL(
+ "https://example.org:123/path/to/somewhere?b=456", query_params={"a": "123"}
+ )
+ assert str(url) == "https://example.org:123/path/to/somewhere?a=123"