Skip to content

Commit 403d69f

Browse files
authored
[ES-1372353] make user_agent_header part of public API (#530)
* make user_agent_header part of public API * removed user_agent_entry from list of internal params * add backward compatibility
1 parent e716136 commit 403d69f

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

examples/set_user_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
66
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
77
access_token=os.getenv("DATABRICKS_TOKEN"),
8-
_user_agent_entry="ExamplePartnerTag",
8+
user_agent_entry="ExamplePartnerTag",
99
) as connection:
1010

1111
with connection.cursor() as cursor:

src/databricks/sql/client.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ def __init__(
122122
port of the oauth redirect uri (localhost). This is required when custom oauth client_id
123123
`oauth_client_id` is set
124124
125+
user_agent_entry: `str`, optional
126+
A custom tag to append to the User-Agent header. This is typically used by partners to identify their applications.. If not specified, it will use the default user agent PyDatabricksSqlConnector
127+
125128
experimental_oauth_persistence: configures preferred storage for persisting oauth tokens.
126129
This has to be a class implementing `OAuthPersistence`.
127130
When `auth_type` is set to `databricks-oauth` or `azure-oauth` without persisting the oauth token in a
@@ -176,8 +179,6 @@ def read(self) -> Optional[OAuthToken]:
176179
"""
177180

178181
# Internal arguments in **kwargs:
179-
# _user_agent_entry
180-
# Tag to add to User-Agent header. For use by partners.
181182
# _use_cert_as_auth
182183
# Use a TLS cert instead of a token
183184
# _enable_ssl
@@ -227,12 +228,21 @@ def read(self) -> Optional[OAuthToken]:
227228
server_hostname, **kwargs
228229
)
229230

230-
if not kwargs.get("_user_agent_entry"):
231-
useragent_header = "{}/{}".format(USER_AGENT_NAME, __version__)
232-
else:
231+
user_agent_entry = kwargs.get("user_agent_entry")
232+
if user_agent_entry is None:
233+
user_agent_entry = kwargs.get("_user_agent_entry")
234+
if user_agent_entry is not None:
235+
logger.warning(
236+
"[WARN] Parameter '_user_agent_entry' is deprecated; use 'user_agent_entry' instead. "
237+
"This parameter will be removed in the upcoming releases."
238+
)
239+
240+
if user_agent_entry:
233241
useragent_header = "{}/{} ({})".format(
234-
USER_AGENT_NAME, __version__, kwargs.get("_user_agent_entry")
242+
USER_AGENT_NAME, __version__, user_agent_entry
235243
)
244+
else:
245+
useragent_header = "{}/{}".format(USER_AGENT_NAME, __version__)
236246

237247
base_headers = [("User-Agent", useragent_header)]
238248

src/databricks/sql/thrift_backend.py

-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ def __init__(
9595
**kwargs,
9696
):
9797
# Internal arguments in **kwargs:
98-
# _user_agent_entry
99-
# Tag to add to User-Agent header. For use by partners.
10098
# _username, _password
10199
# Username and password Basic authentication (no official support)
102100
# _connection_uri

tests/unit/test_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_useragent_header(self, mock_client_class):
157157
)
158158
self.assertIn(user_agent_header, http_headers)
159159

160-
databricks.sql.connect(**self.DUMMY_CONNECTION_ARGS, _user_agent_entry="foobar")
160+
databricks.sql.connect(**self.DUMMY_CONNECTION_ARGS, user_agent_entry="foobar")
161161
user_agent_header_with_entry = (
162162
"User-Agent",
163163
"{}/{} ({})".format(

0 commit comments

Comments
 (0)