add version control and more verbose error return

This commit is contained in:
Thephaseless 2024-11-30 12:42:51 +00:00
parent 37f0cbdb00
commit 258bad19ee
3 changed files with 45 additions and 7 deletions

View File

@ -5,6 +5,9 @@ FROM python:3.12-slim-bullseye
ARG GITHUB_BUILD=false
ENV GITHUB_BUILD=${GITHUB_BUILD}
ARG VERSION
ENV VERSION=${VERSION}
ENV HOME=/root
ENV \
DEBIAN_FRONTEND=noninteractive \

View File

@ -6,6 +6,8 @@ from typing import Any
from pydantic import BaseModel, Field
from src.utils import consts
class LinkRequest(BaseModel):
cmd: str = "get"
@ -26,9 +28,14 @@ class Solution(BaseModel):
response: str
@classmethod
def empty(cls):
def invalid(cls, url: str):
"""
Return an empty Solution with default values.
Useful for returning an error response.
"""
return cls(
url="",
url=url,
status=HTTPStatus.INTERNAL_SERVER_ERROR,
cookies=[],
userAgent="",
@ -43,19 +50,23 @@ class LinkResponse(BaseModel):
solution: Solution
startTimestamp: int # noqa: N815 # Ignore to preserve compatibility
endTimestamp: int = int(time.time() * 1000) # noqa: N815 # Ignore to preserve compatibility
version: str = "3.3.21" # TODO: Implement versioning
version: str = consts.VERSION
@classmethod
def invalid(cls):
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.
"""
return cls(
status="error",
message="Invalid request",
solution=Solution.empty(),
solution=Solution.invalid(url),
startTimestamp=int(time.time() * 1000),
endTimestamp=int(time.time() * 1000),
version="3.3.21",
)
class NoChromeExtensionError(Exception):
"""No chrome extention found."""
"""No chrome extension found."""

View File

@ -1,9 +1,33 @@
import logging
import os
def get_version_from_env():
"""
Retrieve the version from the environment variable 'VERSION'.
This function checks the 'VERSION' environment variable for a value
that starts with 'v' and returns the version without the prefix.
Returns:
str | None: The version string without the 'v' prefix, or None if
the 'VERSION' environment variable is not set or does not start
with 'v'.
"""
version_env = os.getenv("VERSION")
if not version_env or not version_env.startswith("v"):
return None
return version_env.removeprefix("v")
LOG_LEVEL = os.getenv("LOG_LEVEL") or "INFO"
LOG_LEVEL = logging.getLevelNamesMapping()[LOG_LEVEL.upper()]
VERSION = get_version_from_env() or "unknown"
CHALLENGE_TITLES = [
# Cloudflare
"Just a moment...",