Byparr/src/models.py

74 lines
1.9 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
from typing import Annotated, Any
2024-07-24 13:57:40 +00:00
from fastapi import Body
from pydantic import BaseModel
2024-07-24 13:57:40 +00:00
from src import consts
2024-07-24 13:57:40 +00:00
class LinkRequest(BaseModel):
cmd: Annotated[
str,
Body(
default="request.get",
description="Type of request, currently only supports GET requests. This string is purely for compatibility with FlareSolverr.",
),
]
url: Annotated[str, Body(pattern=r"^https?://", default="https://")]
max_timeout: Annotated[int, Body(default=60)]
2024-07-24 13:57:40 +00:00
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
start_timestamp: Annotated[int, Body(alias="startTimestamp")] = int(
time.time() * 1000
)
end_timestamp: Annotated[int, Body(alias="endTimestamp")] = int(time.time() * 1000)
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),
start_timestamp=int(time.time() * 1000),
end_timestamp=int(time.time() * 1000),
2024-07-24 13:57:40 +00:00
)