Skip to content

Commit e053811

Browse files
authored
chore: add method documentation (#15)
add method documentation
1 parent 5d961b8 commit e053811

File tree

3 files changed

+137
-6
lines changed

3 files changed

+137
-6
lines changed

src/momento/cache.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
class Cache:
1616
def __init__(self, auth_token, cache_name, endpoint, default_ttlSeconds):
17+
"""Initializes Cache to perform gets and sets.
18+
19+
Args:
20+
auth_token: Momento JWT token.
21+
cache_name: Name of the cache
22+
end_point: String of endpoint to reach Momento Cache
23+
default_ttlSeconds: Time (in seconds) for which an item will be stored in the cache.
24+
"""
1725
self._validate_ttl(default_ttlSeconds)
1826
self._default_ttlSeconds = default_ttlSeconds
1927
self._secure_channel = grpc.secure_channel(
@@ -27,11 +35,15 @@ def __init__(self, auth_token, cache_name, endpoint, default_ttlSeconds):
2735
cache_interceptor)
2836
self._client = cache_client.ScsStub(intercept_channel)
2937

30-
# While the constructor opens the grpc channel. Connect allows the channel
31-
# to test the connection with provided cache name and auth token.
32-
# Separating the _connect from the constructor, allows better latency and
33-
# resource management for calls that need get or create functionality.
38+
3439
def _connect(self) :
40+
"""Connects the cache to backend.
41+
42+
While the constructor opens the grpc channel. Connect allows the channel
43+
to test the connection with provided cache name and auth token.
44+
Separating the _connect from the constructor, allows better latency and
45+
resource management for calls that need get or create functionality.
46+
"""
3547
try:
3648
_momento_logger.debug('Initializing connection with Cache Service')
3749
self.get(uuid.uuid1().bytes)
@@ -48,6 +60,22 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
4860
self._secure_channel.close()
4961

5062
def set(self, key, value, ttl_seconds=None):
63+
"""Stores an item in cache
64+
65+
Args:
66+
key (string or bytes): The key to be used to store item in the cache.
67+
value (string or bytes): The value to be used to store item in the cache.
68+
ttl_second (Optional): Time to live in cache in seconds. If not provided default TTL provided while creating the cache client instance is used.
69+
70+
Returns:
71+
CacheSetResponse
72+
73+
Raises:
74+
CacheValueError: If service validation fails for provided values.
75+
CacheNotFoundError: If an attempt is made to store an item in a cache that doesn't exist.
76+
PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation.
77+
InternalServerError: If server encountered an unknown error while trying to store the item.
78+
"""
5179
try:
5280
_momento_logger.debug(f'Issuing a set request with key {key}')
5381
item_ttl_seconds = self._default_ttlSeconds if ttl_seconds is None else ttl_seconds
@@ -67,6 +95,20 @@ def set(self, key, value, ttl_seconds=None):
6795
raise _cache_service_errors_converter.convert(e)
6896

6997
def get(self, key):
98+
"""Retrieve an item from the cache
99+
100+
Args:
101+
key (string or bytes): The key to be used to retrieve item from the cache.
102+
103+
Returns:
104+
CacheGetResponse
105+
106+
Raises:
107+
CacheValueError: If service validation fails for provided values.
108+
CacheNotFoundError: If an attempt is made to retrieve an item in a cache that doesn't exist.
109+
PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation.
110+
InternalServerError: If server encountered an unknown error while trying to retrieve the item.
111+
"""
70112
try:
71113
_momento_logger.debug(f'Issuing a get request with key {key}')
72114
get_request = cache_client_types.GetRequest()

src/momento/cache_operation_responses.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,40 @@ class CacheResult(Enum):
1111

1212
class CacheSetResponse:
1313
def __init__(self, grpc_set_response, value):
14+
"""Initializes CacheSetResponse to handle gRPC set response.
15+
16+
Args:
17+
grpc_set_response: Protobuf based response returned by Scs.
18+
value (string or bytes): The value to be used to store item in the cache
19+
20+
Raises:
21+
InternalServerError: If server encountered an unknown error while trying to store the item.
22+
"""
1423
self._value = value
1524
if (grpc_set_response.result != cache_client_types.Ok):
1625
_momento_logger.debug(f'Set received unsupported ECacheResult {grpc_set_response.result}')
1726
raise error_converter.convert_ecache_result(
1827
grpc_set_response.result, grpc_set_response.message, 'SET')
1928

2029
def str_utf8(self):
30+
"""Decodes string value set in cache to a utf-8 string."""
2131
return self._value.decode('utf-8')
2232

2333
def bytes(self):
34+
"""Returns byte value set in cache."""
2435
return self._value
2536

2637

2738
class CacheGetResponse:
2839
def __init__(self, grpc_get_response):
40+
"""Initializes CacheGetResponse to handle gRPC get response.
41+
42+
Args:
43+
grpc_get_response: Protobuf based response returned by Scs.
44+
45+
Raises:
46+
InternalServerError: If server encountered an unknown error while trying to retrieve the item.
47+
"""
2948
self._value = grpc_get_response.cache_body
3049

3150
if (grpc_get_response.result == cache_client_types.Hit):
@@ -39,16 +58,19 @@ def __init__(self, grpc_get_response):
3958

4059

4160
def str_utf8(self):
61+
"""Returns value stored in cache as utf-8 string if there was Hit. Returns None otherwise."""
4262
if (self._result == CacheResult.HIT):
4363
return self._value.decode('utf-8')
4464
return None
4565

4666
def bytes(self):
67+
"""Returns value stored in cache as bytes if there was Hit. Returns None otherwise."""
4768
if (self._result == CacheResult.HIT):
4869
return self._value
4970
return None
5071

5172
def result(self):
73+
"""Returns get operation result such as HIT or MISS."""
5274
return self._result
5375

5476

@@ -64,21 +86,34 @@ def __init__(self, grpc_delete_cache_response):
6486

6587
class ListCachesResponse:
6688
def __init__(self, grpc_list_cache_response):
89+
"""Initializes ListCacheResponse to handle list cache response.
90+
91+
Args:
92+
grpc_list_cache_response: Protobuf based response returned by Scs.
93+
"""
6794
self._next_token = grpc_list_cache_response.next_token if grpc_list_cache_response.next_token != '' else None
6895
self._caches = []
6996
for cache in grpc_list_cache_response.cache:
7097
self._caches.append(CacheInfo(cache))
7198

7299
def next_token(self):
100+
"""Returns next token."""
73101
return self._next_token
74102

75103
def caches(self):
104+
"""Returns all caches."""
76105
return self._caches
77106

78107

79108
class CacheInfo:
80-
def __init__(self, grpc_listed_caches):
81-
self._name = grpc_listed_caches.cache_name
109+
def __init__(self, grpc_listed_cache):
110+
"""Initializes CacheInfo to handle caches returned from list cache operation.
111+
112+
Args:
113+
grpc_listed_cache: Protobuf based response returned by Scs.
114+
"""
115+
self._name = grpc_listed_cache.cache_name
82116

83117
def name(self):
118+
"""Returns all cache's name."""
84119
return self._name

src/momento/momento.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
class Momento:
1818
def __init__(self, auth_token, endpoint_override=None):
19+
"""Initializes Momento to setup SCS client.
20+
21+
Args:
22+
auth_token: Momento JWT
23+
endpoint_override: String of optional endpoint override to be used when given an explicit endpoint by the Momneto team
24+
"""
1925
endpoints = _momento_endpoint_resolver.resolve(auth_token,
2026
endpoint_override)
2127
self._auth_token = auth_token
@@ -36,6 +42,17 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
3642
self._secure_channel.close()
3743

3844
def create_cache(self, cache_name):
45+
"""Creates a new cache in your Momento account.
46+
47+
Args:
48+
cache_name: String used to create cache.
49+
50+
Returns:
51+
CreateCacheResponse
52+
53+
Raises:
54+
ClientSdkError: If an attempt is made to pass anything other than string as cache_name.
55+
"""
3956
try:
4057
_momento_logger.debug(f'Creating cache with name: {cache_name}')
4158
request = CreateCacheRequest()
@@ -46,6 +63,18 @@ def create_cache(self, cache_name):
4663
raise _cache_service_errors_converter.convert(e) from None
4764

4865
def delete_cache(self, cache_name):
66+
"""Deletes a cache and all of the items within it.
67+
68+
Args:
69+
cache_name: String cache name to delete.
70+
71+
Returns:
72+
DeleteCacheResponse
73+
74+
Raises:
75+
CacheNotFoundError: If an attempt is made to delete a MomentoCache that doesn't exits.
76+
ClientSdkError: If an attempt is made to pass anything other than string as cache_name.
77+
"""
4978
try:
5079
_momento_logger.debug(f'Deleting cache with name: {cache_name}')
5180
request = DeleteCacheRequest()
@@ -56,6 +85,20 @@ def delete_cache(self, cache_name):
5685
raise _cache_service_errors_converter.convert(e) from None
5786

5887
def get_cache(self, cache_name, ttl_seconds, create_if_absent=False):
88+
"""Gets a MomentoCache to perform gets and sets on.
89+
90+
Args:
91+
cache_name: String cache name
92+
ttl_seconds: Time to live if object insdie of cache in seconds.
93+
create_if_absent: Boolean value to decide if cache should be created if it does not exist.
94+
95+
Returns:
96+
Cache
97+
98+
Raises:
99+
CacheNotFoundError: If an attempt is made to get a MomentoCache that doesn't exits.
100+
ClientSdkError: If an attempt is made to pass anything other than string as cache_name.
101+
"""
59102
cache = Cache(self._auth_token, cache_name, self._cache_endpoint,
60103
ttl_seconds)
61104
try:
@@ -69,6 +112,17 @@ def get_cache(self, cache_name, ttl_seconds, create_if_absent=False):
69112
return cache._connect()
70113

71114
def list_caches(self, next_token=None):
115+
"""Lists all caches.
116+
117+
Args:
118+
next_token: Token to continue paginating through the list. It's used to handle large paginated lists.
119+
120+
Returns:
121+
ListCachesResponse
122+
123+
Raises:
124+
Exception to notify either sdk, grpc, or operation error.
125+
"""
72126
try:
73127
list_caches_request = ListCachesRequest()
74128
list_caches_request.next_token = next_token if next_token is not None else ''

0 commit comments

Comments
 (0)