Source code for human_requests.abstraction.http
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional
from urllib.parse import parse_qs, urlparse
[docs]
class HttpMethod(Enum):
"""Represents an HTTP method."""
"""Retrieves data from a server.
It only reads data and does not modify it."""
"""Submits data to a server to create a new resource.
It can also be used to update existing resources."""
"""Updates a existing resource on a server.
It can also be used to create a new resource."""
"""Updates a existing resource on a server.
It only updates the fields that are provided in the request body."""
"""Deletes a resource from a server."""
"""Retrieves metadata from a server.
It only reads the headers and does not return the response body."""
"""Provides information about the HTTP methods supported by a server.
It can be used for Cross-Origin Resource Sharing (CORS) request."""
@dataclass(frozen=True)
[docs]
class URL:
"""A dataclass containing the parsed URL components."""
"""The full URL."""
"""The base URL, without query parameters."""
"""Whether the URL is secure (https/wss)."""
"""The protocol of the URL."""
"""The path of the URL."""
[docs]
domain_with_port: str = ""
"""The domain of the URL with port."""
"""The domain of the URL."""
[docs]
port: Optional[int] = None
"""The port of the URL."""
[docs]
params: dict[str, list[str]] = field(default_factory=dict)
"""A dictionary of query parameters."""
[docs]
def __post_init__(self) -> None:
parsed_url = urlparse(self.full_url)
object.__setattr__(self, "base_url", parsed_url._replace(query="").geturl())
object.__setattr__(self, "secure", parsed_url.scheme in ["https", "wss"])
object.__setattr__(self, "protocol", parsed_url.scheme)
object.__setattr__(self, "path", parsed_url.path)
full_domen = parsed_url.netloc.split(":")
object.__setattr__(self, "domain_with_port", parsed_url.netloc)
object.__setattr__(self, "domain", full_domen[0])
if len(full_domen) > 1:
object.__setattr__(self, "port", int(full_domen[1]))
object.__setattr__(self, "params", parse_qs(parsed_url.query))