Source code for edri.dataclass.directive.http
from dataclasses import dataclass
from datetime import datetime
from typing import Optional
from edri.dataclass.directive import HTTPResponseDirective
__all__ = [
"CookieResponseDirective", "AccessDeniedResponseDirective",
"NotFoundResponseDirective", "ConflictResponseDirective",
"HeaderResponseDirective", "UnprocessableContentResponseDirective",
"BadRequestResponseDirective", "NotModifiedResponseDirective",
"ServiceUnavailableResponseDirective", "PartialContentResponseDirective",
"RangeNotSatisfiableResponseDirective", "MovedPermanentlyResponseDirective",
"FoundResponseDirective", "SeeOtherResponseDirective",
"TemporaryRedirectResponseDirective", "PermanentRedirectResponseDirective",
"CreatedResponseDirective", "AcceptedResponseDirective",
"NoContentResponseDirective", "GoneResponseDirective",
"TooManyRequestsResponseDirective", "NotImplementedResponseDirective",
"UnsupportedMediaTypeResponseDirective",
]
[docs]
@dataclass
class CookieResponseDirective(HTTPResponseDirective):
"""
Represents a directive for setting an HTTP cookie in a response.
Attributes:
name (str): The name of the cookie.
value (str): The value of the cookie.
expires (Optional[datetime]): The date and time when the cookie expires. If not set, the cookie will be a session cookie.
path (Optional[str]): The path within the domain where the cookie is valid. Defaults to the root path ('/').
comment (Optional[str]): A comment for the cookie, providing additional information to the client. This is rarely used.
domain (Optional[str]): The domain where the cookie is valid. If not set, defaults to the host of the request URL.
max_age (Optional[int]): The maximum age of the cookie in seconds. If set, it overrides the `expires` attribute.
secure (bool): Indicates if the cookie should only be transmitted over secure protocols like HTTPS.
version (Optional[int]): The version of the cookie. Version 0 is the default and refers to "old" cookies, while version 1 is the newer cookie format.
httponly (bool): Indicates if the cookie is accessible only through HTTP(S) and not available to JavaScript (helps mitigate cross-site scripting attacks).
samesite (Optional[str]): Controls whether a cookie is sent with cross-site requests. Possible values are 'Lax', 'Strict', or 'None'.
"""
name: str
value: str
expires: Optional[datetime] = None
path: Optional[str] = None
comment: Optional[str] = None
domain: Optional[str] = None
max_age: Optional[int] = None
secure: bool = False
version: Optional[int] = None
httponly: bool = False
samesite: Optional[str] = None
[docs]
@dataclass
class AccessDeniedResponseDirective(HTTPResponseDirective):
"""``403 Forbidden`` — access to the requested resource is denied."""
message: str | None = None
[docs]
@dataclass
class NotFoundResponseDirective(HTTPResponseDirective):
"""``404 Not Found`` — the requested resource could not be found."""
message: str | None = None
[docs]
@dataclass
class ConflictResponseDirective(HTTPResponseDirective):
"""``409 Conflict`` — the request conflicts with the current resource state."""
message: str | None = None
[docs]
@dataclass
class UnprocessableContentResponseDirective(HTTPResponseDirective):
"""``422 Unprocessable Content`` — the request is valid but cannot be processed."""
message: str | None = None
[docs]
@dataclass
class BadRequestResponseDirective(HTTPResponseDirective):
"""``400 Bad Request`` — the server cannot process the malformed request."""
message: str | None = None
[docs]
@dataclass
class NotModifiedResponseDirective(HTTPResponseDirective):
"""``304 Not Modified`` — the cached representation is still current."""
pass
[docs]
@dataclass
class ServiceUnavailableResponseDirective(HTTPResponseDirective):
"""``503 Service Unavailable`` — the server is temporarily unable to handle the request."""
message: str | None = None
[docs]
@dataclass
class PartialContentResponseDirective(HTTPResponseDirective):
"""``206 Partial Content`` — the response contains the requested byte range."""
pass
[docs]
@dataclass
class RangeNotSatisfiableResponseDirective(HTTPResponseDirective):
"""``416 Range Not Satisfiable`` — the requested byte range cannot be fulfilled."""
pass
[docs]
@dataclass
class MovedPermanentlyResponseDirective(HTTPResponseDirective):
"""``301 Moved Permanently`` — the resource now lives permanently at ``location``."""
location: str
[docs]
@dataclass
class FoundResponseDirective(HTTPResponseDirective):
"""``302 Found`` — temporary redirect to ``location`` (the common default redirect)."""
location: str
[docs]
@dataclass
class SeeOtherResponseDirective(HTTPResponseDirective):
"""``303 See Other`` — redirect to ``location`` with a GET (e.g. Post/Redirect/Get)."""
location: str
[docs]
@dataclass
class TemporaryRedirectResponseDirective(HTTPResponseDirective):
"""``307 Temporary Redirect`` — temporary redirect that preserves the HTTP method."""
location: str
[docs]
@dataclass
class PermanentRedirectResponseDirective(HTTPResponseDirective):
"""``308 Permanent Redirect`` — permanent redirect that preserves the HTTP method."""
location: str
[docs]
@dataclass
class CreatedResponseDirective(HTTPResponseDirective):
"""
Marks the response as ``201 Created``. If ``location`` is given, it is
written to the ``Location`` header pointing at the newly created resource.
"""
location: str | None = None
[docs]
@dataclass
class AcceptedResponseDirective(HTTPResponseDirective):
"""``202 Accepted`` — the request was accepted for (typically asynchronous) processing."""
pass
[docs]
@dataclass
class NoContentResponseDirective(HTTPResponseDirective):
"""``204 No Content`` — the request succeeded and there is no response body."""
pass
[docs]
@dataclass
class GoneResponseDirective(HTTPResponseDirective):
"""``410 Gone`` — the resource was permanently removed and will not return."""
message: str | None = None
[docs]
@dataclass
class TooManyRequestsResponseDirective(HTTPResponseDirective):
"""
``429 Too Many Requests`` — the client has sent too many requests in a given
amount of time. If ``retry_after`` (seconds) is set, it is written to the
``Retry-After`` header.
"""
message: str | None = None
retry_after: int | None = None
[docs]
@dataclass
class NotImplementedResponseDirective(HTTPResponseDirective):
"""``501 Not Implemented`` — the server does not support the requested functionality."""
message: str | None = None