From 0ac70a40c3aefe8c3189b7271774504c2df67152 Mon Sep 17 00:00:00 2001 From: anitarua Date: Mon, 23 Sep 2024 16:28:29 -0700 Subject: [PATCH] move validators to internal utils --- src/momento/cache_client.py | 2 +- src/momento/cache_client_async.py | 2 +- .../internal/_utilities/_data_validation.py | 15 +++++++++++++++ src/momento/internal/aio/_scs_token_client.py | 2 +- .../internal/synchronous/_scs_token_client.py | 2 +- src/momento/utilities/shared_sync_asyncio.py | 18 ------------------ 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/momento/cache_client.py b/src/momento/cache_client.py index f209e5e1..98a81c41 100644 --- a/src/momento/cache_client.py +++ b/src/momento/cache_client.py @@ -8,10 +8,10 @@ from momento.auth import CredentialProvider from momento.config import Configuration from momento.errors import UnknownException +from momento.internal._utilities._data_validation import validate_eager_connection_timeout from momento.requests import CollectionTtl, SortOrder from momento.utilities.shared_sync_asyncio import ( DEFAULT_EAGER_CONNECTION_TIMEOUT_SECONDS, - validate_eager_connection_timeout, ) try: diff --git a/src/momento/cache_client_async.py b/src/momento/cache_client_async.py index d2cf5fec..36f0860f 100644 --- a/src/momento/cache_client_async.py +++ b/src/momento/cache_client_async.py @@ -8,10 +8,10 @@ from momento.auth import CredentialProvider from momento.config import Configuration from momento.errors import UnknownException +from momento.internal._utilities._data_validation import validate_eager_connection_timeout from momento.requests import CollectionTtl, SortOrder from momento.utilities.shared_sync_asyncio import ( DEFAULT_EAGER_CONNECTION_TIMEOUT_SECONDS, - validate_eager_connection_timeout, ) try: diff --git a/src/momento/internal/_utilities/_data_validation.py b/src/momento/internal/_utilities/_data_validation.py index 8d70a4bb..2fcdedd3 100644 --- a/src/momento/internal/_utilities/_data_validation.py +++ b/src/momento/internal/_utilities/_data_validation.py @@ -15,6 +15,7 @@ TSortedSetElements, TSortedSetValues, ) +from momento.utilities.expiration import ExpiresIn DEFAULT_BYTES_CONVERSION_ERROR = "Could not convert the given type to bytes: " DEFAULT_LIST_CONVERSION_ERROR = "The given type is not list[str | bytes]: " @@ -137,3 +138,17 @@ def _validate_request_timeout(request_timeout: Optional[timedelta]) -> None: if request_timeout is None: return _validate_timedelta_ttl(ttl=request_timeout, field_name="Request timeout") + + +def validate_eager_connection_timeout(timeout: timedelta) -> None: + if timeout.total_seconds() < 0: + raise ValueError("The eager connection timeout must be greater than or equal to 0 seconds.") + + +def validate_disposable_token_expiry(expires_in: ExpiresIn) -> None: + if not expires_in.does_expire(): + raise ValueError("Disposable tokens must have an expiry") + if expires_in.valid_for_seconds() < 0: + raise ValueError("Disposable token expiry must be positive") + if expires_in.valid_for_seconds() > 60 * 60: + raise ValueError("Disposable tokens must expire within 1 hour") diff --git a/src/momento/internal/aio/_scs_token_client.py b/src/momento/internal/aio/_scs_token_client.py index 0abb9442..b1369442 100644 --- a/src/momento/internal/aio/_scs_token_client.py +++ b/src/momento/internal/aio/_scs_token_client.py @@ -8,12 +8,12 @@ from momento.auth.credential_provider import CredentialProvider from momento.config.auth_configuration import AuthConfiguration from momento.errors.error_converter import convert_error +from momento.internal._utilities._data_validation import validate_disposable_token_expiry from momento.internal._utilities._permissions import permissions_from_disposable_token_scope from momento.internal.aio._scs_grpc_manager import _TokenGrpcManager from momento.internal.services import Service from momento.responses.auth.generate_disposable_token import GenerateDisposableToken, GenerateDisposableTokenResponse from momento.utilities.expiration import ExpiresIn -from momento.utilities.shared_sync_asyncio import validate_disposable_token_expiry class _ScsTokenClient: diff --git a/src/momento/internal/synchronous/_scs_token_client.py b/src/momento/internal/synchronous/_scs_token_client.py index 61ed046a..8a05fc71 100644 --- a/src/momento/internal/synchronous/_scs_token_client.py +++ b/src/momento/internal/synchronous/_scs_token_client.py @@ -8,12 +8,12 @@ from momento.auth.credential_provider import CredentialProvider from momento.config.auth_configuration import AuthConfiguration from momento.errors.error_converter import convert_error +from momento.internal._utilities._data_validation import validate_disposable_token_expiry from momento.internal._utilities._permissions import permissions_from_disposable_token_scope from momento.internal.services import Service from momento.internal.synchronous._scs_grpc_manager import _TokenGrpcManager from momento.responses.auth.generate_disposable_token import GenerateDisposableToken, GenerateDisposableTokenResponse from momento.utilities.expiration import ExpiresIn -from momento.utilities.shared_sync_asyncio import validate_disposable_token_expiry class _ScsTokenClient: diff --git a/src/momento/utilities/shared_sync_asyncio.py b/src/momento/utilities/shared_sync_asyncio.py index d407ba50..4d8c7a86 100644 --- a/src/momento/utilities/shared_sync_asyncio.py +++ b/src/momento/utilities/shared_sync_asyncio.py @@ -1,26 +1,8 @@ from __future__ import annotations -from datetime import timedelta - -from momento.utilities.expiration import ExpiresIn - DEFAULT_EAGER_CONNECTION_TIMEOUT_SECONDS = 30 -def validate_eager_connection_timeout(timeout: timedelta) -> None: - if timeout.total_seconds() < 0: - raise ValueError("The eager connection timeout must be greater than or equal to 0 seconds.") - - -def validate_disposable_token_expiry(expires_in: ExpiresIn) -> None: - if not expires_in.does_expire(): - raise ValueError("Disposable tokens must have an expiry") - if expires_in.valid_for_seconds() < 0: - raise ValueError("Disposable token expiry must be positive") - if expires_in.valid_for_seconds() > 60 * 60: - raise ValueError("Disposable tokens must expire within 1 hour") - - def str_to_bytes(string: str) -> bytes: """Convert a string to bytes.