Skip to content

Commit abb8202

Browse files
authored
fix(python): expose user_agent on the config (#4141)
1 parent 2e894c9 commit abb8202

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

clients/algoliasearch-client-python/algoliasearch/http/base_config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Dict, Optional
33

44
from algoliasearch.http.hosts import HostsCollection
5+
from algoliasearch.http.user_agent import UserAgent
56

67

78
class BaseConfig:
@@ -27,9 +28,18 @@ def __init__(self, app_id: Optional[str] = None, api_key: Optional[str] = None):
2728
self.proxies: Optional[Dict[str, str]] = None
2829
self.hosts: Optional[HostsCollection] = None
2930

31+
self._user_agent: UserAgent = UserAgent()
32+
3033
def set_client_api_key(self, api_key: str) -> None:
3134
"""Sets a new API key to authenticate requests."""
3235
self.api_key = api_key
3336
if self.headers is None:
3437
self.headers = {}
3538
self.headers["x-algolia-api-key"] = api_key
39+
40+
def add_user_agent(self, segment: str, version: Optional[str] = None) -> None:
41+
"""adds a segment to the default user agent, and update the headers sent with each requests as well"""
42+
self._user_agent = self._user_agent.add(segment, version)
43+
44+
if self.headers is not None:
45+
self.headers["user-agent"] = self._user_agent.get()

clients/algoliasearch-client-python/algoliasearch/http/user_agent.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def __init__(self) -> None:
1919
def get(self) -> str:
2020
return self.value
2121

22-
def add(self, segment: str, version: Optional[str] = __version__) -> Self:
23-
self.value += "; {} ({})".format(segment, version)
22+
def add(self, segment: str, version: Optional[str] = None) -> Self:
23+
self.value += "; {}".format(segment)
24+
25+
if version is not None:
26+
self.value += " ({})".format(version)
27+
2428
return self

playground/python/app/search.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def main():
1313
client = SearchClientSync(
1414
environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY")
1515
)
16+
client.add_user_agent("playground")
17+
client.add_user_agent("bar", "baz")
18+
19+
print("user_agent", client._config._user_agent.get())
1620
print("client initialized", client)
1721

1822
try:

playground/python/poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/python/api.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}:
102102
"""Sets a new API key to authenticate requests."""
103103
self._transporter.config.set_client_api_key(api_key)
104104

105+
{{^isSyncClient}}async {{/isSyncClient}}def add_user_agent(self, segment: str, version: Optional[str] = None) -> None:
106+
"""adds a segment to the default user agent, and update the headers sent with each requests as well"""
107+
self._transporter.config.add_user_agent(segment, version)
108+
105109
{{#isSearchClient}}
106110
{{> search_helpers}}
107111
{{/isSearchClient}}

templates/python/config.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ from algoliasearch.http.hosts import (
88
)
99
from algoliasearch.http.user_agent import UserAgent
1010
from algoliasearch.http.base_config import BaseConfig
11+
from algoliasearch import __version__
1112

1213

1314
class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig):
1415
def __init__(self, app_id: Optional[str], api_key: Optional[str]{{#hasRegionalHost}}, region: {{#fallbackToAliasHost}}Optional[str] = None{{/fallbackToAliasHost}}{{^fallbackToAliasHost}}str = ""{{/fallbackToAliasHost}}{{/hasRegionalHost}}) -> None:
1516
super().__init__(app_id, api_key)
1617

17-
user_agent = UserAgent().add("{{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}")
18+
self._user_agent = UserAgent()
19+
self.add_user_agent("{{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}", __version__)
1820

1921
if app_id is None or not app_id:
2022
raise ValueError("`app_id` is missing.")
@@ -25,7 +27,7 @@ class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig):
2527
self.headers = {
2628
"x-algolia-application-id": app_id,
2729
"x-algolia-api-key": api_key,
28-
"user-agent": user_agent.get(),
30+
"user-agent": self._user_agent.get(),
2931
"content-type": "application/json",
3032
}
3133

0 commit comments

Comments
 (0)