Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not use test util in package #464

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/momento/auth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from momento.config.auth_configuration import AuthConfiguration
from momento.internal.synchronous._scs_token_client import _ScsTokenClient
from momento.responses.auth.generate_disposable_token import GenerateDisposableTokenResponse
from momento.utilities.expiration import ExpiresIn
from momento.utilities import ExpiresIn


class AuthClient:
Expand Down
2 changes: 1 addition & 1 deletion src/momento/auth_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from momento.config.auth_configuration import AuthConfiguration
from momento.internal.aio._scs_token_client import _ScsTokenClient
from momento.responses.auth.generate_disposable_token import GenerateDisposableTokenResponse
from momento.utilities.expiration import ExpiresIn
from momento.utilities import ExpiresIn


class AuthClientAsync:
Expand Down
4 changes: 2 additions & 2 deletions 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 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 Expand Up @@ -206,7 +206,7 @@ def create(
eager_connection_timeout = timedelta(seconds=30)
client = CacheClient.create(configuration, credential_provider, ttl_seconds, eager_connection_timeout)
"""
validate_eager_connection_timeout(eager_connection_timeout)
_validate_eager_connection_timeout(eager_connection_timeout)
# an explicit 0 means that the client disabled eager connections
if eager_connection_timeout.total_seconds() != 0:
client = CacheClient(configuration, credential_provider, default_ttl)
Expand Down
4 changes: 2 additions & 2 deletions 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 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 Expand Up @@ -207,7 +207,7 @@ async def create(
eager_connection_timeout = timedelta(seconds=30)
client = CacheClientAsync.create(configuration, credential_provider, ttl_seconds, eager_connection_timeout)
"""
validate_eager_connection_timeout(eager_connection_timeout)
_validate_eager_connection_timeout(eager_connection_timeout)
# an explicit 0 means that the client disabled eager connections
if eager_connection_timeout.total_seconds() != 0:
client = CacheClientAsync(configuration, credential_provider, default_ttl)
Expand Down
2 changes: 2 additions & 0 deletions src/momento/internal/_utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
_gen_set_input_as_bytes,
_validate_cache_name,
_validate_dictionary_name,
_validate_disposable_token_expiry,
_validate_eager_connection_timeout,
_validate_list_name,
_validate_request_timeout,
_validate_set_name,
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 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/_utilities/_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
TopicPermission,
TopicRole,
)
from tests.utils import str_to_bytes
from momento.utilities.shared_sync_asyncio import str_to_bytes


class SuperuserPermissions(PredefinedScope):
Expand Down
6 changes: 3 additions & 3 deletions 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
from momento.utilities import ExpiresIn


class _ScsTokenClient:
Expand All @@ -38,7 +38,7 @@ async def generate_disposable_token(
disposable_token_props: Optional[DisposableTokenProps] = None,
) -> GenerateDisposableTokenResponse:
try:
validate_disposable_token_expiry(expires_in)
_validate_disposable_token_expiry(expires_in)
self._logger.info("Creating disposable token")

token_id = disposable_token_props.token_id if disposable_token_props else None
Expand Down
6 changes: 3 additions & 3 deletions 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
from momento.utilities import ExpiresIn


class _ScsTokenClient:
Expand All @@ -38,7 +38,7 @@ def generate_disposable_token(
disposable_token_props: Optional[DisposableTokenProps] = None,
) -> GenerateDisposableTokenResponse:
try:
validate_disposable_token_expiry(expires_in)
_validate_disposable_token_expiry(expires_in)
self._logger.info("Creating disposable token")

token_id = disposable_token_props.token_id if disposable_token_props else None
Expand Down
2 changes: 1 addition & 1 deletion src/momento/responses/auth/generate_disposable_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from momento_wire_types import token_pb2 as token_pb

from momento.responses.response import AuthResponse
from momento.utilities.expiration import ExpiresAt
from momento.utilities import ExpiresAt

from ..mixins import ErrorResponseMixin

Expand Down
10 changes: 10 additions & 0 deletions src/momento/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .expiration import Expiration, ExpiresAt, ExpiresIn
from .shared_sync_asyncio import DEFAULT_EAGER_CONNECTION_TIMEOUT_SECONDS, str_to_bytes
anitarua marked this conversation as resolved.
Show resolved Hide resolved

__all__ = [
"Expiration",
"ExpiresAt",
"ExpiresIn",
"DEFAULT_EAGER_CONNECTION_TIMEOUT_SECONDS",
"str_to_bytes",
]
24 changes: 8 additions & 16 deletions src/momento/utilities/shared_sync_asyncio.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
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 str_to_bytes(string: str) -> bytes:
"""Convert a string to bytes.

Args:
string (str): The string to convert.

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")
Returns:
bytes: A UTF-8 byte representation of the string.
"""
return string.encode("utf-8")
2 changes: 1 addition & 1 deletion tests/momento/auth_client/test_auth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from momento.responses.pubsub.publish import TopicPublish
from momento.responses.pubsub.subscribe import TopicSubscribe
from momento.topic_client import TopicClient
from momento.utilities.expiration import ExpiresIn
from momento.utilities import ExpiresIn

from tests.utils import uuid_str

Expand Down
2 changes: 1 addition & 1 deletion tests/momento/auth_client/test_auth_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from momento.responses.pubsub.publish import TopicPublish
from momento.responses.pubsub.subscribe import TopicSubscribe
from momento.topic_client_async import TopicClientAsync
from momento.utilities.expiration import ExpiresIn
from momento.utilities import ExpiresIn

from tests.utils import uuid_str

Expand Down
Loading