diff --git a/clients/algoliasearch-client-python/algoliasearch/http/base_config.py b/clients/algoliasearch-client-python/algoliasearch/http/base_config.py index 4191a0bd55b..82eb4bc09c6 100644 --- a/clients/algoliasearch-client-python/algoliasearch/http/base_config.py +++ b/clients/algoliasearch-client-python/algoliasearch/http/base_config.py @@ -2,6 +2,7 @@ from typing import Dict, Optional from algoliasearch.http.hosts import HostsCollection +from algoliasearch.http.user_agent import UserAgent class BaseConfig: @@ -27,9 +28,18 @@ def __init__(self, app_id: Optional[str] = None, api_key: Optional[str] = None): self.proxies: Optional[Dict[str, str]] = None self.hosts: Optional[HostsCollection] = None + self._user_agent: UserAgent = UserAgent() + def set_client_api_key(self, api_key: str) -> None: """Sets a new API key to authenticate requests.""" self.api_key = api_key if self.headers is None: self.headers = {} self.headers["x-algolia-api-key"] = api_key + + def add_user_agent(self, segment: str, version: Optional[str] = None) -> None: + """adds a segment to the default user agent, and update the headers sent with each requests as well""" + self._user_agent = self._user_agent.add(segment, version) + + if self.headers is not None: + self.headers["user-agent"] = self._user_agent.get() diff --git a/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py b/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py index edaf03f0348..613f8030bb3 100644 --- a/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py +++ b/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py @@ -19,6 +19,10 @@ def __init__(self) -> None: def get(self) -> str: return self.value - def add(self, segment: str, version: Optional[str] = __version__) -> Self: - self.value += "; {} ({})".format(segment, version) + def add(self, segment: str, version: Optional[str] = None) -> Self: + self.value += "; {}".format(segment) + + if version is not None: + self.value += " ({})".format(version) + return self diff --git a/playground/python/app/search.py b/playground/python/app/search.py index dcea84b8756..8513f58c06e 100644 --- a/playground/python/app/search.py +++ b/playground/python/app/search.py @@ -13,6 +13,10 @@ def main(): client = SearchClientSync( environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY") ) + client.add_user_agent("playground") + client.add_user_agent("bar", "baz") + + print("user_agent", client._config._user_agent.get()) print("client initialized", client) try: diff --git a/playground/python/poetry.lock b/playground/python/poetry.lock index ad644bba730..0b68826e589 100644 --- a/playground/python/poetry.lock +++ b/playground/python/poetry.lock @@ -139,7 +139,7 @@ frozenlist = ">=1.1.0" [[package]] name = "algoliasearch" -version = "4.9.1" +version = "4.9.2" description = "A fully-featured and blazing-fast Python API client to interact with Algolia." optional = false python-versions = ">= 3.8.1" @@ -147,7 +147,7 @@ files = [] develop = true [package.dependencies] -aiohttp = ">= 3.9.2" +aiohttp = ">= 3.10.11" async-timeout = ">= 4.0.3" pydantic = ">= 2" python-dateutil = ">= 2.8.2" diff --git a/templates/python/api.mustache b/templates/python/api.mustache index 521e87ade8a..f483a8e4d5c 100644 --- a/templates/python/api.mustache +++ b/templates/python/api.mustache @@ -102,6 +102,10 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}: """Sets a new API key to authenticate requests.""" self._transporter.config.set_client_api_key(api_key) + {{^isSyncClient}}async {{/isSyncClient}}def add_user_agent(self, segment: str, version: Optional[str] = None) -> None: + """adds a segment to the default user agent, and update the headers sent with each requests as well""" + self._transporter.config.add_user_agent(segment, version) + {{#isSearchClient}} {{> search_helpers}} {{/isSearchClient}} diff --git a/templates/python/config.mustache b/templates/python/config.mustache index 1395487752e..5d4815c6a9c 100644 --- a/templates/python/config.mustache +++ b/templates/python/config.mustache @@ -8,13 +8,15 @@ from algoliasearch.http.hosts import ( ) from algoliasearch.http.user_agent import UserAgent from algoliasearch.http.base_config import BaseConfig +from algoliasearch import __version__ class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig): def __init__(self, app_id: Optional[str], api_key: Optional[str]{{#hasRegionalHost}}, region: {{#fallbackToAliasHost}}Optional[str] = None{{/fallbackToAliasHost}}{{^fallbackToAliasHost}}str = ""{{/fallbackToAliasHost}}{{/hasRegionalHost}}) -> None: super().__init__(app_id, api_key) - user_agent = UserAgent().add("{{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}") + self._user_agent = UserAgent() + self.add_user_agent("{{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}", __version__) if app_id is None or not app_id: raise ValueError("`app_id` is missing.") @@ -25,7 +27,7 @@ class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig): self.headers = { "x-algolia-application-id": app_id, "x-algolia-api-key": api_key, - "user-agent": user_agent.get(), + "user-agent": self._user_agent.get(), "content-type": "application/json", }