Skip to content
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 back in URL.raw with NamedTuple #2481

Merged
merged 4 commits into from Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions httpx/_types.py
Expand Up @@ -16,6 +16,7 @@
Iterator,
List,
Mapping,
NamedTuple,
Optional,
Sequence,
Tuple,
Expand All @@ -31,6 +32,16 @@

PrimitiveData = Optional[Union[str, int, float, bool]]

RawURL = NamedTuple(
"RawURL",
[
("raw_scheme", bytes),
("raw_host", bytes),
("port", Optional[int]),
("raw_path", bytes),
],
)

URLTypes = Union["URL", str]

QueryParamTypes = Union[
Expand Down
17 changes: 16 additions & 1 deletion httpx/_urls.py
Expand Up @@ -6,7 +6,7 @@
import rfc3986.exceptions

from ._exceptions import InvalidURL
from ._types import PrimitiveData, QueryParamTypes, URLTypes
from ._types import PrimitiveData, QueryParamTypes, RawURL, URLTypes
from ._utils import primitive_value_to_str


Expand Down Expand Up @@ -311,6 +311,21 @@ def fragment(self) -> str:
"""
return unquote(self._uri_reference.fragment or "")

@property
def raw(self) -> RawURL:
"""
Provides the (scheme, host, port, target) for the outgoing request.

In older versions of `httpx` this was used in the low-level transport API.
We no longer use `RawURL`, and this property will be deprecated in a future release.
"""
return RawURL(
self.raw_scheme,
self.raw_host,
self.port,
self.raw_path,
)

@property
def is_absolute_url(self) -> bool:
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/models/test_url.py
Expand Up @@ -424,3 +424,13 @@ def test_ipv6_url_from_raw_url(host):
assert url.host == "::ffff:192.168.0.1"
assert url.netloc == b"[::ffff:192.168.0.1]"
assert str(url) == "https://[::ffff:192.168.0.1]/"


def test_url_raw_compatibility():
url = httpx.URL("https://www.example.com/path")
scheme, host, port, raw_path = url.raw

assert scheme == b"https"
assert host == b"www.example.com"
assert port is None
assert raw_path == b"/path"