From ac573e176a6b5a46171c3d432f76e3847acba2bb Mon Sep 17 00:00:00 2001 From: shortcuts Date: Thu, 21 Nov 2024 15:44:35 +0100 Subject: [PATCH 1/7] fix(python): expose `user_agent` on the config --- playground/python/app/search.py | 2 ++ playground/python/poetry.lock | 4 ++-- templates/python/config.mustache | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/playground/python/app/search.py b/playground/python/app/search.py index dcea84b8756..63bc7b32beb 100644 --- a/playground/python/app/search.py +++ b/playground/python/app/search.py @@ -13,6 +13,8 @@ def main(): client = SearchClientSync( environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY") ) + client._config.user_agent.add("playground") + 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/config.mustache b/templates/python/config.mustache index 1395487752e..100d75c7a48 100644 --- a/templates/python/config.mustache +++ b/templates/python/config.mustache @@ -14,7 +14,7 @@ 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().add("{{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}") if app_id is None or not app_id: raise ValueError("`app_id` is missing.") @@ -25,7 +25,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", } From 095a10ec354e3b31c160cf5bd201b13fe133f37e Mon Sep 17 00:00:00 2001 From: shortcuts Date: Thu, 21 Nov 2024 16:04:08 +0100 Subject: [PATCH 2/7] fix: type on baseconfig --- .../algoliasearch/http/base_config.py | 3 +++ playground/python/app/search.py | 1 + 2 files changed, 4 insertions(+) diff --git a/clients/algoliasearch-client-python/algoliasearch/http/base_config.py b/clients/algoliasearch-client-python/algoliasearch/http/base_config.py index 4191a0bd55b..73453b888c8 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,6 +28,8 @@ 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 diff --git a/playground/python/app/search.py b/playground/python/app/search.py index 63bc7b32beb..e0d59398f3a 100644 --- a/playground/python/app/search.py +++ b/playground/python/app/search.py @@ -15,6 +15,7 @@ def main(): ) client._config.user_agent.add("playground") + print("user_agent", client._config.user_agent.get()) print("client initialized", client) try: From ed4bd3e4c9b43754b587cd526ed2f51c4af4bdb9 Mon Sep 17 00:00:00 2001 From: shortcuts Date: Thu, 21 Nov 2024 16:16:34 +0100 Subject: [PATCH 3/7] fix: like set api key --- .../algoliasearch/http/base_config.py | 7 +++++++ playground/python/app/search.py | 2 +- templates/python/api.mustache | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-python/algoliasearch/http/base_config.py b/clients/algoliasearch-client-python/algoliasearch/http/base_config.py index 73453b888c8..958dfaad200 100644 --- a/clients/algoliasearch-client-python/algoliasearch/http/base_config.py +++ b/clients/algoliasearch-client-python/algoliasearch/http/base_config.py @@ -36,3 +36,10 @@ def set_client_api_key(self, api_key: str) -> None: 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/playground/python/app/search.py b/playground/python/app/search.py index e0d59398f3a..3d018dbcdfd 100644 --- a/playground/python/app/search.py +++ b/playground/python/app/search.py @@ -13,7 +13,7 @@ def main(): client = SearchClientSync( environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY") ) - client._config.user_agent.add("playground") + client.add_user_agent("playground") print("user_agent", client._config.user_agent.get()) print("client initialized", client) 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}} From cdad0fa112dd6bcfdbb270fbc62891bfa258baa8 Mon Sep 17 00:00:00 2001 From: shortcuts Date: Thu, 21 Nov 2024 16:49:13 +0100 Subject: [PATCH 4/7] fix: default to __version__ --- .../algoliasearch/http/base_config.py | 6 +++--- .../algoliasearch/http/user_agent.py | 2 +- playground/python/app/search.py | 2 +- templates/python/config.mustache | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clients/algoliasearch-client-python/algoliasearch/http/base_config.py b/clients/algoliasearch-client-python/algoliasearch/http/base_config.py index 958dfaad200..82eb4bc09c6 100644 --- a/clients/algoliasearch-client-python/algoliasearch/http/base_config.py +++ b/clients/algoliasearch-client-python/algoliasearch/http/base_config.py @@ -28,7 +28,7 @@ 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() + self._user_agent: UserAgent = UserAgent() def set_client_api_key(self, api_key: str) -> None: """Sets a new API key to authenticate requests.""" @@ -39,7 +39,7 @@ def set_client_api_key(self, api_key: str) -> None: 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) + self._user_agent = self._user_agent.add(segment, version) if self.headers is not None: - self.headers["user-agent"] = self.user_agent.get() + 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..5fd0609b678 100644 --- a/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py +++ b/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py @@ -20,5 +20,5 @@ def get(self) -> str: return self.value def add(self, segment: str, version: Optional[str] = __version__) -> Self: - self.value += "; {} ({})".format(segment, version) + self.value += "; {} ({})".format(segment, __version__ if version is None else version) return self diff --git a/playground/python/app/search.py b/playground/python/app/search.py index 3d018dbcdfd..f86d4a977b9 100644 --- a/playground/python/app/search.py +++ b/playground/python/app/search.py @@ -15,7 +15,7 @@ def main(): ) client.add_user_agent("playground") - print("user_agent", client._config.user_agent.get()) + print("user_agent", client._config._user_agent.get()) print("client initialized", client) try: diff --git a/templates/python/config.mustache b/templates/python/config.mustache index 100d75c7a48..46943656a84 100644 --- a/templates/python/config.mustache +++ b/templates/python/config.mustache @@ -14,7 +14,7 @@ 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) - self.user_agent = UserAgent().add("{{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}") + self._user_agent = UserAgent().add("{{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}") if app_id is None or not app_id: raise ValueError("`app_id` is missing.") @@ -25,7 +25,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": self.user_agent.get(), + "user-agent": self._user_agent.get(), "content-type": "application/json", } From 58f85044df755b31bd8c7c40f4990cde2991d74e Mon Sep 17 00:00:00 2001 From: shortcuts Date: Thu, 21 Nov 2024 17:18:01 +0100 Subject: [PATCH 5/7] fix: no need async --- .../algoliasearch/http/user_agent.py | 4 +++- templates/python/api.mustache | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py b/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py index 5fd0609b678..d2c7133091c 100644 --- a/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py +++ b/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py @@ -20,5 +20,7 @@ def get(self) -> str: return self.value def add(self, segment: str, version: Optional[str] = __version__) -> Self: - self.value += "; {} ({})".format(segment, __version__ if version is None else version) + self.value += "; {} ({})".format( + segment, __version__ if version is None else version + ) return self diff --git a/templates/python/api.mustache b/templates/python/api.mustache index f483a8e4d5c..530784d3042 100644 --- a/templates/python/api.mustache +++ b/templates/python/api.mustache @@ -98,11 +98,11 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}: return self._transporter.close() {{/isSyncClient}} - {{^isSyncClient}}async {{/isSyncClient}}def set_client_api_key(self, api_key: str) -> None: + def set_client_api_key(self, api_key: str) -> None: """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: + 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) From 13edf22fa2ef174850a673ae866b515f6f358a06 Mon Sep 17 00:00:00 2001 From: shortcuts Date: Thu, 21 Nov 2024 17:28:02 +0100 Subject: [PATCH 6/7] Revert "fix: no need async" This reverts commit 58f85044df755b31bd8c7c40f4990cde2991d74e. --- .../algoliasearch/http/user_agent.py | 4 +--- templates/python/api.mustache | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py b/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py index d2c7133091c..5fd0609b678 100644 --- a/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py +++ b/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py @@ -20,7 +20,5 @@ def get(self) -> str: return self.value def add(self, segment: str, version: Optional[str] = __version__) -> Self: - self.value += "; {} ({})".format( - segment, __version__ if version is None else version - ) + self.value += "; {} ({})".format(segment, __version__ if version is None else version) return self diff --git a/templates/python/api.mustache b/templates/python/api.mustache index 530784d3042..f483a8e4d5c 100644 --- a/templates/python/api.mustache +++ b/templates/python/api.mustache @@ -98,11 +98,11 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}: return self._transporter.close() {{/isSyncClient}} - def set_client_api_key(self, api_key: str) -> None: + {{^isSyncClient}}async {{/isSyncClient}}def set_client_api_key(self, api_key: str) -> None: """Sets a new API key to authenticate requests.""" self._transporter.config.set_client_api_key(api_key) - def add_user_agent(self, segment: str, version: Optional[str] = None) -> None: + {{^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) From 4d7811a78b00cb0ce4970a98545f3d5c17709862 Mon Sep 17 00:00:00 2001 From: shortcuts Date: Fri, 22 Nov 2024 11:05:05 +0100 Subject: [PATCH 7/7] fix: do not default version --- .../algoliasearch/http/user_agent.py | 8 ++++++-- playground/python/app/search.py | 1 + templates/python/config.mustache | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py b/clients/algoliasearch-client-python/algoliasearch/http/user_agent.py index 5fd0609b678..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__ if version is None else 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 f86d4a977b9..8513f58c06e 100644 --- a/playground/python/app/search.py +++ b/playground/python/app/search.py @@ -14,6 +14,7 @@ def main(): 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) diff --git a/templates/python/config.mustache b/templates/python/config.mustache index 46943656a84..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) - self._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.")