Skip to content

Commit 3649999

Browse files
Copilotslister1001
andcommitted
Refactor: Extract validation logic into shared utility function
Created azure.core._version_validation module with a shared validate_version() function to eliminate code duplication across all SDK modules. Updated all user agent files to use this centralized validation. Co-authored-by: slister1001 <[email protected]>
1 parent 8c441c4 commit 3649999

File tree

9 files changed

+55
-16
lines changed

9 files changed

+55
-16
lines changed

sdk/appconfiguration/azure-appconfiguration-provider/azure/appconfiguration/provider/_user_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# -------------------------------------------------------------------------
66

77
from ._version import VERSION
8+
from azure.core._version_validation import validate_version
89

910
# Validate that VERSION is not empty or None to prevent unauthorized SDK usage
10-
if not VERSION or not isinstance(VERSION, str) or not VERSION.strip():
11-
raise ValueError("Invalid SDK version: version must be a non-empty string")
11+
validate_version(VERSION)
1212

1313
USER_AGENT = "python-appconfiguration-provider/{}".format(VERSION)

sdk/containerregistry/azure-containerregistry/azure/containerregistry/_user_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# ------------------------------------
66

77
from ._version import VERSION
8+
from azure.core._version_validation import validate_version
89

910
# Validate that VERSION is not empty or None to prevent unauthorized SDK usage
10-
if not VERSION or not isinstance(VERSION, str) or not VERSION.strip():
11-
raise ValueError("Invalid SDK version: version must be a non-empty string")
11+
validate_version(VERSION)
1212

1313
USER_AGENT = f"azure-containerregistry/{VERSION}"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# --------------------------------------------------------------------------
2+
#
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
#
5+
# The MIT License (MIT)
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the ""Software""), to
9+
# deal in the Software without restriction, including without limitation the
10+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11+
# sell copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
# IN THE SOFTWARE.
24+
#
25+
# --------------------------------------------------------------------------
26+
"""Utilities for validating SDK versions."""
27+
28+
29+
def validate_version(version):
30+
"""Validate that a version string is valid and not empty.
31+
32+
This prevents unauthorized SDK usage by ensuring the VERSION is properly set.
33+
34+
:param version: The version string to validate
35+
:type version: str
36+
:raises ValueError: If the version is None, empty, or contains only whitespace
37+
"""
38+
if not version or not isinstance(version, str) or not version.strip():
39+
raise ValueError("Invalid SDK version: version must be a non-empty string")

sdk/core/azure-core/azure/core/pipeline/policies/_universal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
from azure.core import __version__ as azcore_version
4242
from azure.core.exceptions import DecodeError
43+
from azure.core._version_validation import validate_version
4344

4445
from azure.core.pipeline import PipelineRequest, PipelineResponse
4546
from ._base import SansIOHTTPPolicy
@@ -209,8 +210,7 @@ def __init__(self, base_user_agent: Optional[str] = None, **kwargs: Any) -> None
209210
application_id: Optional[str] = kwargs.pop("user_agent", None)
210211

211212
# Validate that azcore_version is not empty or None to prevent unauthorized SDK usage
212-
if not azcore_version or not isinstance(azcore_version, str) or not azcore_version.strip():
213-
raise ValueError("Invalid SDK version: version must be a non-empty string")
213+
validate_version(azcore_version)
214214

215215
sdk_moniker: str = kwargs.pop("sdk_moniker", "core/{}".format(azcore_version))
216216

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/_user_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from typing import Iterator
66

77
from azure.ai.evaluation._version import VERSION
8+
from azure.core._version_validation import validate_version
89

910
# Validate that VERSION is not empty or None to prevent unauthorized SDK usage
10-
if not VERSION or not isinstance(VERSION, str) or not VERSION.strip():
11-
raise ValueError("Invalid SDK version: version must be a non-empty string")
11+
validate_version(VERSION)
1212

1313

1414
class UserAgentSingleton:

sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_user_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# ------------------------------------
55

66
from ._version import VERSION
7+
from azure.core._version_validation import validate_version
78

89
# Validate that VERSION is not empty or None to prevent unauthorized SDK usage
9-
if not VERSION or not isinstance(VERSION, str) or not VERSION.strip():
10-
raise ValueError("Invalid SDK version: version must be a non-empty string")
10+
validate_version(VERSION)
1111

1212
USER_AGENT = f"ai-formrecognizer/{VERSION}"

sdk/identity/azure-identity/azure/identity/_internal/user_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import platform
66

77
from .._version import VERSION
8+
from azure.core._version_validation import validate_version
89

910
# Validate that VERSION is not empty or None to prevent unauthorized SDK usage
10-
if not VERSION or not isinstance(VERSION, str) or not VERSION.strip():
11-
raise ValueError("Invalid SDK version: version must be a non-empty string")
11+
validate_version(VERSION)
1212

1313
USER_AGENT = "azsdk-python-identity/{} Python/{} ({})".format(VERSION, platform.python_version(), platform.platform())

sdk/ml/azure-ai-ml/azure/ai/ml/_user_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# Copyright (c) Microsoft Corporation. All rights reserved.
33
# ---------------------------------------------------------
44
from azure.ai.ml._version import VERSION
5+
from azure.core._version_validation import validate_version
56

67
# Validate that VERSION is not empty or None to prevent unauthorized SDK usage
7-
if not VERSION or not isinstance(VERSION, str) or not VERSION.strip():
8-
raise ValueError("Invalid SDK version: version must be a non-empty string")
8+
validate_version(VERSION)
99

1010
USER_AGENT = "{}/{}".format("azure-ai-ml", VERSION)

sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_user_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# ------------------------------------
55

66
from ._version import VERSION
7+
from azure.core._version_validation import validate_version
78

89
# Validate that VERSION is not empty or None to prevent unauthorized SDK usage
9-
if not VERSION or not isinstance(VERSION, str) or not VERSION.strip():
10-
raise ValueError("Invalid SDK version: version must be a non-empty string")
10+
validate_version(VERSION)
1111

1212
USER_AGENT = f"ai-textanalytics/{VERSION}"

0 commit comments

Comments
 (0)