Data structure for representing an HTTP URL. Used as a return value for :func:`parse_url`. Both the scheme and host are normalized as they are both case-insensitive according to RFC 3986.
| 78 | |
| 79 | |
| 80 | class Url( |
| 81 | typing.NamedTuple( |
| 82 | "Url", |
| 83 | [ |
| 84 | ("scheme", typing.Optional[str]), |
| 85 | ("auth", typing.Optional[str]), |
| 86 | ("host", typing.Optional[str]), |
| 87 | ("port", typing.Optional[int]), |
| 88 | ("path", typing.Optional[str]), |
| 89 | ("query", typing.Optional[str]), |
| 90 | ("fragment", typing.Optional[str]), |
| 91 | ], |
| 92 | ) |
| 93 | ): |
| 94 | """ |
| 95 | Data structure for representing an HTTP URL. Used as a return value for |
| 96 | :func:`parse_url`. Both the scheme and host are normalized as they are |
| 97 | both case-insensitive according to RFC 3986. |
| 98 | """ |
| 99 | |
| 100 | def __new__( # type: ignore[no-untyped-def] |
| 101 | cls, |
| 102 | scheme: str | None = None, |
| 103 | auth: str | None = None, |
| 104 | host: str | None = None, |
| 105 | port: int | None = None, |
| 106 | path: str | None = None, |
| 107 | query: str | None = None, |
| 108 | fragment: str | None = None, |
| 109 | ): |
| 110 | if path and not path.startswith("/"): |
| 111 | path = "/" + path |
| 112 | if scheme is not None: |
| 113 | scheme = scheme.lower() |
| 114 | return super().__new__(cls, scheme, auth, host, port, path, query, fragment) |
| 115 | |
| 116 | @property |
| 117 | def hostname(self) -> str | None: |
| 118 | """For backwards-compatibility with urlparse. We're nice like that.""" |
| 119 | return self.host |
| 120 | |
| 121 | @property |
| 122 | def request_uri(self) -> str: |
| 123 | """Absolute path including the query string.""" |
| 124 | uri = self.path or "/" |
| 125 | |
| 126 | if self.query is not None: |
| 127 | uri += "?" + self.query |
| 128 | |
| 129 | return uri |
| 130 | |
| 131 | @property |
| 132 | def authority(self) -> str | None: |
| 133 | """ |
| 134 | Authority component as defined in RFC 3986 3.2. |
| 135 | This includes userinfo (auth), host and port. |
| 136 | |
| 137 | i.e. |
no outgoing calls