]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add back in URL.raw with NamedTuple (#2481)
authorDemetri <developerDemetri@users.noreply.github.com>
Fri, 2 Dec 2022 16:20:05 +0000 (08:20 -0800)
committerGitHub <noreply@github.com>
Fri, 2 Dec 2022 16:20:05 +0000 (16:20 +0000)
* add back in URL.raw with NamedTuple

* Update _urls.py

* Update _urls.py

Co-authored-by: Tom Christie <tom@tomchristie.com>
httpx/_types.py
httpx/_urls.py
tests/models/test_url.py

index f3c1f6ef4e94ab828954fc4315f1e0a749bb7ac4..6b610e14084b8a94f2fbad725a8885c2cf62fad8 100644 (file)
@@ -16,6 +16,7 @@ from typing import (
     Iterator,
     List,
     Mapping,
+    NamedTuple,
     Optional,
     Sequence,
     Tuple,
@@ -31,6 +32,16 @@ if TYPE_CHECKING:  # pragma: no cover
 
 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[
index ab39c2ec01842aa19b6fbf686a5aea8bff16cbe8..f26b2eb2dc635be8192e870549a31e54373e5bee 100644 (file)
@@ -6,7 +6,7 @@ import rfc3986
 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
 
 
@@ -311,6 +311,21 @@ class URL:
         """
         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:
         """
index 321cffb3c9aca31dc0985f1d4ecdb412f034cd2a..959681be9f53bc4a2bda8c470877779858431dce 100644 (file)
@@ -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"