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."""
[docs] GET = "GET"
"""Retrieves data from a server. It only reads data and does not modify it."""
[docs] POST = "POST"
"""Submits data to a server to create a new resource. It can also be used to update existing resources."""
[docs] PUT = "PUT"
"""Updates a existing resource on a server. It can also be used to create a new resource."""
[docs] PATCH = "PATCH"
"""Updates a existing resource on a server. It only updates the fields that are provided in the request body."""
[docs] DELETE = "DELETE"
"""Deletes a resource from a server."""
[docs] HEAD = "HEAD"
"""Retrieves metadata from a server. It only reads the headers and does not return the response body."""
[docs] OPTIONS = "OPTIONS"
"""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."""
[docs] full_url: str
"""The full URL."""
[docs] base_url: str = ""
"""The base URL, without query parameters."""
[docs] secure: bool = False
"""Whether the URL is secure (https/wss)."""
[docs] protocol: str = ""
"""The protocol of the URL."""
[docs] path: str = ""
"""The path of the URL."""
[docs] domain_with_port: str = ""
"""The domain of the URL with port."""
[docs] domain: str = ""
"""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))