A URL path string that may also hold an associated protocol and/or host. Used by the routing to return `url_path_for` matches.
| 173 | |
| 174 | |
| 175 | class URLPath(str): |
| 176 | """ |
| 177 | A URL path string that may also hold an associated protocol and/or host. |
| 178 | Used by the routing to return `url_path_for` matches. |
| 179 | """ |
| 180 | |
| 181 | def __new__(cls, path: str, protocol: Literal["http", "websocket", ""] = "", host: str = "") -> URLPath: |
| 182 | assert protocol in ("http", "websocket", "") |
| 183 | return str.__new__(cls, path) |
| 184 | |
| 185 | def __init__(self, path: str, protocol: Literal["http", "websocket", ""] = "", host: str = "") -> None: |
| 186 | self.protocol = protocol |
| 187 | self.host = host |
| 188 | |
| 189 | def make_absolute_url(self, base_url: str | URL) -> URL: |
| 190 | if isinstance(base_url, str): |
| 191 | base_url = URL(base_url) |
| 192 | if self.protocol: |
| 193 | scheme = { |
| 194 | "http": {True: "https", False: "http"}, |
| 195 | "websocket": {True: "wss", False: "ws"}, |
| 196 | }[self.protocol][base_url.is_secure] |
| 197 | else: |
| 198 | scheme = base_url.scheme |
| 199 | |
| 200 | netloc = self.host or base_url.netloc |
| 201 | path = base_url.path.rstrip("/") + str(self) |
| 202 | return URL(scheme=scheme, netloc=netloc, path=path) |
| 203 | |
| 204 | |
| 205 | class Secret: |
no outgoing calls
no test coverage detected