Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

Commit 1eb4876

Browse files
committed
Refactor system validation
1 parent cb6dee6 commit 1eb4876

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

pydepsdev/api.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
DEFAULT_MAX_BACKOFF,
2828
DEFAULT_MAX_RETRIES,
2929
DEFAULT_TIMEOUT_DURATION,
30+
SUPPORTED_SYSTEMS,
31+
SUPPORTED_SYSTEMS_REQUIREMENTS,
32+
SUPPORTED_SYSTEMS_DEPENDENCIES,
33+
SUPPORTED_SYSTEMS_DEPENDENTS,
34+
SUPPORTED_SYSTEMS_CAPABILITIES,
35+
SUPPORTED_SYSTEMS_QUERY,
3036
)
3137
from .exceptions import APIError
3238
from .utils import encode_url_param, validate_hash, validate_system
@@ -362,8 +368,7 @@ async def get_requirements(
362368
ValueError: If system_name is not "NUGET".
363369
APIError: On request failure.
364370
"""
365-
if system_name.upper() != "NUGET":
366-
raise ValueError("get_requirements is only for NuGet.")
371+
validate_system(system_name, SUPPORTED_SYSTEMS_REQUIREMENTS)
367372
name_enc = encode_url_param(package_name)
368373
ver_enc = encode_url_param(version)
369374
return await self._get(
@@ -394,7 +399,7 @@ async def get_dependencies(
394399
ValueError: If system_name is invalid.
395400
APIError: On request failure.
396401
"""
397-
validate_system(system_name)
402+
validate_system(system_name, SUPPORTED_SYSTEMS_DEPENDENCIES)
398403
name_enc = encode_url_param(package_name)
399404
ver_enc = encode_url_param(version)
400405
return await self._get(
@@ -429,7 +434,7 @@ async def get_dependents(
429434
ValueError: If system_name is invalid.
430435
APIError: On request failure.
431436
"""
432-
validate_system(system_name)
437+
validate_system(system_name, SUPPORTED_SYSTEMS_DEPENDENTS)
433438
name_enc = encode_url_param(package_name)
434439
ver_enc = encode_url_param(version)
435440
return await self._get(
@@ -470,8 +475,7 @@ async def get_capabilities(
470475
ValueError: If system_name is not "GO".
471476
APIError: On request failure.
472477
"""
473-
if system_name.upper() != "GO":
474-
raise ValueError("get_capabilities is only for GO.")
478+
validate_system(system_name, SUPPORTED_SYSTEMS_CAPABILITIES)
475479
name_enc = encode_url_param(package_name)
476480
ver_enc = encode_url_param(version)
477481
return await self._get(
@@ -653,7 +657,7 @@ async def query_package_versions(
653657
if hash_type and hash_value:
654658
validate_hash(hash_type)
655659
if version_system:
656-
validate_system(version_system)
660+
validate_system(version_system, SUPPORTED_SYSTEMS_QUERY)
657661

658662
params: Dict[str, str] = {}
659663
if hash_type and hash_value:

pydepsdev/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
BASE_URL = "https://api.deps.dev/v3alpha"
1919
SUPPORTED_SYSTEMS = ["GO", "RUBYGEMS", "NPM", "CARGO", "MAVEN", "PYPI", "NUGET"]
20+
SUPPORTED_SYSTEMS_REQUIREMENTS = ["RUBYGEMS", "NPM", "MAVEN", "NUGET"]
21+
SUPPORTED_SYSTEMS_DEPENDENCIES = ["NPM", "CARGO", "MAVEN", "PYPI"]
22+
SUPPORTED_SYSTEMS_DEPENDENTS = ["NPM", "CARGO", "MAVEN", "PYPI"]
23+
SUPPORTED_SYSTEMS_CAPABILITIES = ["GO"]
24+
SUPPORTED_SYSTEMS_QUERY = ["RUBYGEMS", "NPM", "CARGO", "MAVEN", "PYPI", "NUGET"]
2025
SUPPORTED_HASHES = ["MD5", "SHA1", "SHA256", "SHA512"]
2126

2227
DEFAULT_MAX_RETRIES = 3

pydepsdev/utils.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616
#
1717

1818
import urllib.parse
19-
from typing import NoReturn
20-
from .constants import SUPPORTED_SYSTEMS, SUPPORTED_HASHES
19+
from typing import Optional, Sequence
20+
from .constants import (
21+
SUPPORTED_SYSTEMS,
22+
SUPPORTED_SYSTEMS_REQUIREMENTS,
23+
SUPPORTED_SYSTEMS_DEPENDENCIES,
24+
SUPPORTED_SYSTEMS_DEPENDENTS,
25+
SUPPORTED_SYSTEMS_CAPABILITIES,
26+
SUPPORTED_SYSTEMS_QUERY,
27+
)
2128

2229

2330
def encode_url_param(param: str) -> str:
@@ -33,22 +40,26 @@ def encode_url_param(param: str) -> str:
3340
return urllib.parse.quote_plus(param)
3441

3542

36-
def validate_system(system: str) -> None:
43+
def validate_system(
44+
system: str,
45+
allowed_systems: Optional[Sequence[str]] = None,
46+
) -> None:
3747
"""
3848
Validate that the given system identifier is supported.
49+
If allowed_systems is None, we fall back to SUPPORTED_SYSTEMS.
3950
4051
Args:
41-
system (str): The package system name (e.g. "npm", "pypi") to validate.
52+
system (str): e.g. "npm", "PYPI", etc.
53+
allowed_systems (Optional[Sequence[str]]):
54+
A specific SUPPORTED_* constant, or None for all.
4255
4356
Raises:
44-
ValueError: If `system` (case-insensitive) is not in SUPPORTED_SYSTEMS.
57+
ValueError: if system.upper() not in allowed_systems.
4558
"""
4659
normalized = system.upper()
47-
if normalized not in SUPPORTED_SYSTEMS:
48-
raise ValueError(
49-
"This operation is currently only available for "
50-
f"{', '.join(SUPPORTED_SYSTEMS)}."
51-
)
60+
allowed = allowed_systems or SUPPORTED_SYSTEMS
61+
if normalized not in allowed:
62+
raise ValueError(f"This operation is only available for: {', '.join(allowed)}")
5263

5364

5465
def validate_hash(hash_type: str) -> None:

0 commit comments

Comments
 (0)