Byparr/src/models/requests.py

73 lines
1.7 KiB
Python
Raw Normal View History

2024-07-24 13:57:40 +00:00
from __future__ import annotations
import time
2024-11-24 23:04:19 +00:00
from http import HTTPStatus
2024-07-24 13:57:40 +00:00
from typing import Any
2024-11-25 10:19:09 +00:00
from pydantic import BaseModel, Field
2024-07-24 13:57:40 +00:00
from src.utils import consts
2024-07-24 13:57:40 +00:00
class LinkRequest(BaseModel):
2024-11-25 10:19:09 +00:00
cmd: str = "get"
2024-07-24 13:57:40 +00:00
url: str
2024-11-25 10:19:09 +00:00
max_timeout: int = Field(30, alias="maxTimeout")
2024-07-24 13:57:40 +00:00
class ProtectionTriggeredError(Exception):
pass
class Solution(BaseModel):
url: str
status: int
2024-07-24 15:53:14 +00:00
cookies: list
2024-07-24 13:57:40 +00:00
userAgent: str # noqa: N815 # Ignore to preserve compatibility
headers: dict[str, Any]
response: str
2024-11-24 23:04:19 +00:00
@classmethod
def invalid(cls, url: str):
"""
Return an empty Solution with default values.
Useful for returning an error response.
"""
2024-11-24 23:04:19 +00:00
return cls(
url=url,
2024-11-24 23:04:19 +00:00
status=HTTPStatus.INTERNAL_SERVER_ERROR,
cookies=[],
userAgent="",
headers={},
response="",
)
2024-07-24 13:57:40 +00:00
class LinkResponse(BaseModel):
status: str = "ok"
message: str
solution: Solution
startTimestamp: int # noqa: N815 # Ignore to preserve compatibility
endTimestamp: int = int(time.time() * 1000) # noqa: N815 # Ignore to preserve compatibility
version: str = consts.VERSION
2024-07-24 13:57:40 +00:00
@classmethod
def invalid(cls, url: str):
"""
Return an invalid LinkResponse with default error values.
This method is used to generate a response indicating an invalid request.
"""
2024-07-24 13:57:40 +00:00
return cls(
2024-11-24 23:04:19 +00:00
status="error",
message="Invalid request",
solution=Solution.invalid(url),
2024-11-24 23:04:19 +00:00
startTimestamp=int(time.time() * 1000),
endTimestamp=int(time.time() * 1000),
2024-07-24 13:57:40 +00:00
)
2024-07-24 15:16:46 +00:00
2024-11-24 12:29:06 +00:00
class NoChromeExtensionError(Exception):
"""No chrome extension found."""