From 258bad19ee7235bfdd6f94229eb4cb5fb3df0b15 Mon Sep 17 00:00:00 2001 From: Thephaseless Date: Sat, 30 Nov 2024 12:42:51 +0000 Subject: [PATCH] add version control and more verbose error return --- Dockerfile | 3 +++ src/models/requests.py | 25 ++++++++++++++++++------- src/utils/consts.py | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index d1ad6d0..ba5db89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ diff --git a/src/models/requests.py b/src/models/requests.py index e4f6ef4..8466669 100644 --- a/src/models/requests.py +++ b/src/models/requests.py @@ -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.""" diff --git a/src/utils/consts.py b/src/utils/consts.py index 5834160..8092395 100644 --- a/src/utils/consts.py +++ b/src/utils/consts.py @@ -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...",