Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Jun 27, 2024
1 parent 3e57b5b commit 0c81a7b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
15 changes: 11 additions & 4 deletions databricks/sdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ def __init__(self,
self._fix_host_if_needed()
self._validate()
self.init_auth()
self._product = product
self._product_version = product_version
self._init_product(product, product_version)
except ValueError as e:
message = self.wrap_debug_info(str(e))
raise ValueError(message) from e
Expand Down Expand Up @@ -236,8 +235,8 @@ def user_agent(self):
""" Returns User-Agent header used by this SDK """

# global user agent includes SDK version, product name & version, platform info,
# and global extra info
ua = [useragent.to_string((self._product, self._product_version))]
# and global extra info. However,
ua = [useragent.to_string(self._product_info)]
# Python SDK supports local extra info, which needs to be added in addition to global info
if len(self._user_agent_other_info) > 0:
ua.append(' '.join(self._user_agent_other_info))
Expand Down Expand Up @@ -446,6 +445,14 @@ def init_auth(self):
except ValueError as e:
raise ValueError(f'{self._credentials_strategy.auth_type()} auth: {e}') from e

def _init_product(self, product, product_version):
if product is not None or product_version is not None:
default_product, default_version = useragent.product()
self._product_info = (product or default_product, product_version or default_version)
else:
self._product_info = None


def __repr__(self):
return f'<{self.debug_string()}>'

Expand Down
18 changes: 11 additions & 7 deletions databricks/sdk/useragent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
CICD_KEY = 'cicd'
AUTH_KEY = 'auth'

product_name = "unknown"
product_version = "0.0.0"
_product_name = "unknown"
_product_version = "0.0.0"

logger = logging.getLogger("databricks.sdk.useragent")

Expand All @@ -38,14 +38,18 @@ def _match_alphanum_or_semver(value):
raise ValueError(f"Invalid value: {value}")


def product():
return _product_name, _product_version


def with_product(name: str, version: str):
"""Change the product name and version used in the User-Agent header."""
global product_name, product_version
global _product_name, _product_version
match_alphanum(name)
match_semver(version)
logger.debug(f'Changing product from {product_name}/{product_version} to {name}/{version}')
product_name = name
product_version = version
logger.debug(f'Changing product from {_product_name}/{_product_version} to {name}/{version}')
_product_name = name
_product_version = version


def with_user_agent_extra(key: str, value: str):
Expand Down Expand Up @@ -89,7 +93,7 @@ def to_string(alternate_product_info: Optional[Tuple[str, str]] = None):
if alternate_product_info:
base.append(alternate_product_info)
else:
base.append((product_name, product_version))
base.append((_product_name, _product_version))
base.extend([
("databricks-sdk-py", __version__),
("python", platform.python_version()),
Expand Down
8 changes: 4 additions & 4 deletions tests/test_user_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
@pytest.fixture(scope="function")
def user_agent():
from databricks.sdk import useragent
orig_product_name = useragent.product_name
orig_product_version = useragent.product_version
orig_product_name = useragent._product_name
orig_product_version = useragent._product_version
orig_extra = useragent._extra

yield useragent

useragent.product_name = orig_product_name
useragent.product_version = orig_product_version
useragent._product_name = orig_product_name
useragent._product_version = orig_product_version
useragent._extra = orig_extra


Expand Down

0 comments on commit 0c81a7b

Please sign in to comment.