-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: cache client control plane operations #44
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7369aef
feat: cache client control plane operations
anitarua 40c8b6f
pass auth token to ci
anitarua 3e737ea
remove unnecessary commented out tests
anitarua 020b3af
empty commit to nudge ci
anitarua 797888f
shorten response name to AlreadyExists
anitarua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,56 @@ | ||
import 'package:client_sdk_dart/client_sdk_dart.dart'; | ||
import 'package:client_sdk_dart/src/config/cache_configuration.dart'; | ||
import 'package:client_sdk_dart/src/internal/control_client.dart'; | ||
import 'package:client_sdk_dart/src/internal/data_client.dart'; | ||
import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/get_response.dart'; | ||
import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/set_response.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
import 'auth/credential_provider.dart'; | ||
import 'config/logger.dart'; | ||
import 'messages/values.dart'; | ||
// import 'config/logger.dart'; | ||
|
||
abstract class ICacheClient { | ||
Future<CreateCacheResponse> createCache(String cacheName); | ||
|
||
Future<DeleteCacheResponse> deleteCache(String cacheName); | ||
|
||
Future<ListCachesResponse> listCaches(); | ||
|
||
Future<GetResponse> get(String cacheName, Value value); | ||
|
||
Future<SetResponse> set(String cacheName, Value key, Value value, | ||
{Duration? ttl}); | ||
} | ||
|
||
class CacheClient implements ICacheClient { | ||
final DataClient _dataClient; | ||
late final DataClient _dataClient; | ||
late final ControlClient _controlClient; | ||
final Logger _logger = Logger('MomentoCacheClient'); | ||
|
||
CacheClient(CredentialProvider credentialProvider, | ||
CacheConfiguration configuration, Duration defaultTtl) | ||
: _dataClient = | ||
DataClient(credentialProvider, configuration, defaultTtl) { | ||
_logger.level = determineLoggerLevel(configuration.logLevel); | ||
CacheConfiguration configuration, Duration defaultTtl) { | ||
_dataClient = DataClient(credentialProvider, configuration, defaultTtl); | ||
_controlClient = ControlClient(credentialProvider, configuration); | ||
// _logger.level = determineLoggerLevel(configuration.logLevel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue captured in #36 |
||
_logger.finest("initializing cache client"); | ||
} | ||
|
||
@override | ||
Future<CreateCacheResponse> createCache(String cacheName) { | ||
// TODO: add validators | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will be added in #43 which will also round out all the basic cache operations |
||
return _controlClient.createCache(cacheName); | ||
} | ||
|
||
@override | ||
Future<DeleteCacheResponse> deleteCache(String cacheName) { | ||
// TODO: add validators | ||
return _controlClient.deleteCache(cacheName); | ||
} | ||
|
||
@override | ||
Future<ListCachesResponse> listCaches() { | ||
// TODO: add validators | ||
return _controlClient.listCaches(); | ||
} | ||
|
||
@override | ||
Future<GetResponse> get(String cacheName, Value value) { | ||
return _dataClient.get(cacheName, value); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import 'package:client_sdk_dart/client_sdk_dart.dart'; | ||
import 'package:client_sdk_dart/generated/controlclient.pbgrpc.dart'; | ||
import 'package:client_sdk_dart/src/config/cache_configuration.dart'; | ||
import 'package:client_sdk_dart/src/errors/errors.dart'; | ||
import 'package:grpc/grpc.dart'; | ||
|
||
abstract class AbstractControlClient { | ||
Future<CreateCacheResponse> createCache(String cacheName); | ||
|
||
Future<DeleteCacheResponse> deleteCache(String cacheName); | ||
|
||
Future<ListCachesResponse> listCaches(); | ||
} | ||
|
||
class ControlClient implements AbstractControlClient { | ||
late ClientChannel _channel; | ||
late ScsControlClient _client; | ||
final CacheConfiguration _configuration; | ||
|
||
ControlClient(CredentialProvider credentialProvider, this._configuration) { | ||
_channel = ClientChannel(credentialProvider.controlEndpoint); | ||
_client = ScsControlClient(_channel, | ||
options: CallOptions(metadata: { | ||
'authorization': credentialProvider.apiKey, | ||
'agent': 'dart:0.1.0', | ||
}, timeout: _configuration.transportStrategy.grpcConfig.deadline)); | ||
} | ||
|
||
@override | ||
Future<CreateCacheResponse> createCache(String cacheName) async { | ||
var request = CreateCacheRequest_(); | ||
request.cacheName = cacheName; | ||
try { | ||
await _client.createCache(request, | ||
options: CallOptions(metadata: { | ||
'cache': cacheName, | ||
})); | ||
return CreateCacheSuccess(); | ||
} catch (e) { | ||
if (e is GrpcError && e.code == StatusCode.alreadyExists) { | ||
return AlreadyExists(); | ||
} else if (e is SdkException) { | ||
return CreateCacheError(e); | ||
} else { | ||
return CreateCacheError( | ||
UnknownException("Unexpected error: $e", null, null)); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Future<DeleteCacheResponse> deleteCache(String cacheName) async { | ||
var request = DeleteCacheRequest_(); | ||
request.cacheName = cacheName; | ||
try { | ||
await _client.deleteCache(request, | ||
options: CallOptions(metadata: { | ||
'cache': cacheName, | ||
})); | ||
return DeleteCacheSuccess(); | ||
} catch (e) { | ||
if (e is SdkException) { | ||
return DeleteCacheError(e); | ||
} else { | ||
return DeleteCacheError( | ||
UnknownException("Unexpected error: $e", null, null)); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Future<ListCachesResponse> listCaches() async { | ||
var request = ListCachesRequest_(); | ||
try { | ||
final resp = await _client.listCaches(request); | ||
return ListCachesSuccess(resp.cache); | ||
} catch (e) { | ||
if (e is SdkException) { | ||
return ListCachesError(e); | ||
} else { | ||
return ListCachesError( | ||
UnknownException("Unexpected error: $e", null, null)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lib/src/messages/responses/cache/control/create_cache_response.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'package:client_sdk_dart/src/messages/responses/responses_base.dart'; | ||
|
||
/// Sealed class for a create cache response. | ||
/// | ||
/// Pattern matching can be used to operate on the appropriate subtype. | ||
/// ``` | ||
/// switch (response) { | ||
/// case CreateCacheSuccess(): | ||
/// // handle success | ||
/// case AlreadyExists(): | ||
/// // handle already exists | ||
/// case CreateCacheError(): | ||
/// // handle error | ||
/// } | ||
/// ``` | ||
sealed class CreateCacheResponse {} | ||
|
||
/// Indicates a successful create cache request. | ||
class CreateCacheSuccess implements CreateCacheResponse {} | ||
|
||
/// Indicates that the cache already exists, so there was nothing to do. | ||
class AlreadyExists implements CreateCacheResponse {} | ||
|
||
/// Indicates that an error occurred during the create cache request. | ||
/// | ||
/// The response object includes the following fields you can use to determine how you want to handle the error: | ||
/// - `errorCode`: a unique Momento error code indicating the type of error that occurred | ||
/// - `message`: a human-readable description of the error | ||
/// - `innerException`: the original error that caused the failure; can be re-thrown | ||
class CreateCacheError extends ErrorResponseBase | ||
implements CreateCacheResponse { | ||
CreateCacheError(super.exception); | ||
} |
28 changes: 28 additions & 0 deletions
28
lib/src/messages/responses/cache/control/delete_cache_response.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:client_sdk_dart/src/messages/responses/responses_base.dart'; | ||
|
||
/// Sealed class for a delete cache response. | ||
/// | ||
/// Pattern matching can be used to operate on the appropriate subtype. | ||
/// ``` | ||
/// switch (response) { | ||
/// case DeleteCacheSuccess(): | ||
/// // handle success | ||
/// case DeleteCacheError(): | ||
/// // handle error | ||
/// } | ||
/// ``` | ||
sealed class DeleteCacheResponse {} | ||
|
||
/// Indicates a successful delete cache request. | ||
class DeleteCacheSuccess implements DeleteCacheResponse {} | ||
|
||
/// Indicates that an error occurred during the delete cache request. | ||
/// | ||
/// The response object includes the following fields you can use to determine how you want to handle the error: | ||
/// - `errorCode`: a unique Momento error code indicating the type of error that occurred | ||
/// - `message`: a human-readable description of the error | ||
/// - `innerException`: the original error that caused the failure; can be re-thrown | ||
class DeleteCacheError extends ErrorResponseBase | ||
implements DeleteCacheResponse { | ||
DeleteCacheError(super.exception); | ||
} |
46 changes: 46 additions & 0 deletions
46
lib/src/messages/responses/cache/control/list_caches_response.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'package:client_sdk_dart/generated/controlclient.pb.dart'; | ||
import 'package:client_sdk_dart/src/messages/responses/responses_base.dart'; | ||
|
||
/// Represents information about a listed cache, such as its name. | ||
/// May include additional information in the future. | ||
class CacheInfo { | ||
final String name; | ||
|
||
CacheInfo(this.name); | ||
} | ||
|
||
/// Sealed class for a list caches response. | ||
/// | ||
/// Pattern matching can be used to operate on the appropriate subtype. | ||
/// ``` | ||
/// switch (response) { | ||
/// case ListCachesSuccess(): | ||
/// // handle success | ||
/// case ListCachesError(): | ||
/// // handle error | ||
/// } | ||
/// ``` | ||
sealed class ListCachesResponse {} | ||
|
||
/// Indicates a successful list caches request. | ||
class ListCachesSuccess implements ListCachesResponse { | ||
late final List<CacheInfo> caches; | ||
|
||
ListCachesSuccess(List<Cache_> grpcCaches) { | ||
caches = grpcCaches.map((cache) => CacheInfo(cache.cacheName)).toList(); | ||
} | ||
|
||
String description() { | ||
return "[ListCachesSuccess] length of caches list: ${caches.length}"; | ||
} | ||
} | ||
|
||
/// Indicates that an error occurred during the list caches request. | ||
/// | ||
/// The response object includes the following fields you can use to determine how you want to handle the error: | ||
/// - `errorCode`: a unique Momento error code indicating the type of error that occurred | ||
/// - `message`: a human-readable description of the error | ||
/// - `innerException`: the original error that caused the failure; can be re-thrown | ||
class ListCachesError extends ErrorResponseBase implements ListCachesResponse { | ||
ListCachesError(super.exception); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
names clashed when both topic and cache configs were exported from the top-level
lib/client_sdk_dart.dart
file