Skip to content

Commit

Permalink
move validators to internal utils
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Sep 23, 2024
1 parent b170604 commit 0ac70a4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/momento/cache_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/momento/cache_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions src/momento/internal/_utilities/_data_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]: "
Expand Down Expand Up @@ -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")
2 changes: 1 addition & 1 deletion src/momento/internal/aio/_scs_token_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/momento/internal/synchronous/_scs_token_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 0 additions & 18 deletions src/momento/utilities/shared_sync_asyncio.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 0ac70a4

Please sign in to comment.