From 669237cbd941907c0c3b3edc2cc6517977939f18 Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Tue, 18 Jun 2024 21:03:43 -0300 Subject: [PATCH] refactor: Set user agent for API calls in base settings The code changes in `base.py` add a `user_agent` attribute to the `Settings` class and a corresponding class method `set_user_agent` to set the user agent for API calls. If the `user_agent` is not provided, it defaults to "langflow". The method also updates the `USER_AGENT` environment variable and logs the new value. This refactor improves the flexibility and maintainability of the code. The commit message follows the established convention of using a prefix to indicate the type of change. --- .../base/langflow/services/settings/base.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/backend/base/langflow/services/settings/base.py b/src/backend/base/langflow/services/settings/base.py index e8c0cbf90..3df9360f9 100644 --- a/src/backend/base/langflow/services/settings/base.py +++ b/src/backend/base/langflow/services/settings/base.py @@ -119,8 +119,22 @@ class Settings(BaseSettings): """Timeout for the API calls in seconds.""" frontend_timeout: int = 0 """Timeout for the frontend API calls in seconds.""" + user_agent: str = "langflow" + """User agent for the API calls.""" + + @field_validator("user_agent", mode="after") + @classmethod + def set_user_agent(cls, value): + if not value: + value = "langflow" + import os + + os.environ["USER_AGENT"] = value + logger.debug(f"Setting user agent to {value}") + return value @field_validator("config_dir", mode="before") + @classmethod def set_langflow_dir(cls, value): if not value: from platformdirs import user_cache_dir @@ -144,6 +158,7 @@ class Settings(BaseSettings): return str(value) @field_validator("database_url", mode="before") + @classmethod def set_database_url(cls, value, info): if not value: logger.debug("No database_url provided, trying LANGFLOW_DATABASE_URL env variable")