-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache client control plane operations (#44)
* feat: cache client control plane operations * pass auth token to ci * remove unnecessary commented out tests * empty commit to nudge ci * shorten response name to AlreadyExists
- Loading branch information
Showing
14 changed files
with
356 additions
and
18 deletions.
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
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.