-
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: scalar delete #43
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bce3949
feat: add scalar delete
anitarua cce7bba
chore: add docstrings to scalar set and get response classes
anitarua a206ff0
add validators and export more things and dart format
anitarua 9f598d3
add validators to control plane operations
anitarua 87cfadd
fix: add missing grpc error to sdk exception function
anitarua 08ac7f4
fix: make sure to delete test caches
anitarua 6715881
chore: add tests for scalar get, set, delete
anitarua 0757273
Merge branch 'main' into scalar-delete-and-tests
anitarua 0a2b33d
fix imports and dart format
anitarua c27e6b5
remove unnecessary examples folder bc it was renamed
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
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,19 +1,17 @@ | ||
import 'package:momento/generated/cacheclient.pbgrpc.dart'; | ||
import 'package:momento/momento.dart'; | ||
import 'package:momento/src/config/cache_configuration.dart'; | ||
import 'package:momento/src/errors/errors.dart'; | ||
import 'package:momento/src/messages/responses/cache/data/scalar/get_response.dart'; | ||
import 'package:momento/src/messages/responses/cache/data/scalar/set_response.dart'; | ||
import 'package:fixnum/fixnum.dart'; | ||
import 'package:grpc/grpc.dart'; | ||
|
||
import '../auth/credential_provider.dart'; | ||
import '../messages/values.dart'; | ||
|
||
abstract class AbstractDataClient { | ||
Future<GetResponse> get(String cacheName, Value key); | ||
|
||
Future<SetResponse> set(String cacheName, Value key, Value value, | ||
{Duration? ttl}); | ||
|
||
Future<DeleteResponse> delete(String cacheName, Value key); | ||
} | ||
|
||
class DataClient implements AbstractDataClient { | ||
|
@@ -39,7 +37,7 @@ class DataClient implements AbstractDataClient { | |
try { | ||
var resp = await _client.get(request, | ||
options: CallOptions(metadata: { | ||
'cacheName': cacheName, | ||
'cache': cacheName, | ||
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. header was misnamed |
||
})); | ||
|
||
switch (resp.result) { | ||
|
@@ -52,8 +50,8 @@ class DataClient implements AbstractDataClient { | |
"unknown cache get error ${resp.result}", null, null)); | ||
} | ||
} catch (e) { | ||
if (e is SdkException) { | ||
return GetError(e); | ||
if (e is GrpcError) { | ||
return GetError(grpcStatusToSdkException(e)); | ||
} else { | ||
return GetError(UnknownException("Unexpected error: $e", null, null)); | ||
} | ||
|
@@ -66,21 +64,40 @@ class DataClient implements AbstractDataClient { | |
var request = SetRequest_(); | ||
request.cacheKey = key.toBinary(); | ||
request.cacheBody = value.toBinary(); | ||
request.ttlMilliseconds = (ttl != null | ||
? ttl.inMilliseconds | ||
: _defaultTtl.inMilliseconds) as Int64; | ||
request.ttlMilliseconds = | ||
Int64(ttl != null ? ttl.inMilliseconds : _defaultTtl.inMilliseconds); | ||
try { | ||
await _client.set(request, | ||
options: CallOptions(metadata: { | ||
'cacheName': cacheName, | ||
'cache': cacheName, | ||
})); | ||
return SetSuccess(); | ||
} catch (e) { | ||
if (e is SdkException) { | ||
return SetError(e); | ||
if (e is GrpcError) { | ||
return SetError(grpcStatusToSdkException(e)); | ||
} else { | ||
return SetError(UnknownException("Unexpected error: $e", null, null)); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Future<DeleteResponse> delete(String cacheName, Value key) async { | ||
var request = DeleteRequest_(); | ||
request.cacheKey = key.toBinary(); | ||
try { | ||
await _client.delete(request, | ||
options: CallOptions(metadata: { | ||
'cache': cacheName, | ||
})); | ||
return DeleteSuccess(); | ||
} catch (e) { | ||
if (e is GrpcError) { | ||
return DeleteError(grpcStatusToSdkException(e)); | ||
} else { | ||
return DeleteError( | ||
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
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,13 @@ | ||
import 'package:momento/src/errors/errors.dart'; | ||
|
||
void _validateString(String str, String errorMessage) { | ||
if (str.trim().isEmpty) { | ||
throw InvalidArgumentException(errorMessage, null, null); | ||
} | ||
} | ||
|
||
void validateCacheName(String cacheName) => | ||
_validateString(cacheName, "Invalid cache name"); | ||
|
||
void validateTopicName(String topicName) => | ||
_validateString(topicName, "Invalid topic name"); |
27 changes: 27 additions & 0 deletions
27
lib/src/messages/responses/cache/data/scalar/delete_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,27 @@ | ||
import 'package:momento/src/messages/responses/responses_base.dart'; | ||
|
||
/// Sealed class for a delete cache item request. | ||
/// | ||
/// Pattern matching can be used to operate on the appropriate subtype. | ||
/// ```dart | ||
/// switch (response) { | ||
/// case DeleteSuccess(): | ||
/// // handle success | ||
/// case DeleteError(): | ||
/// // handle error | ||
/// } | ||
/// ``` | ||
sealed class DeleteResponse {} | ||
|
||
/// Indicates a successful delete cache item request. | ||
class DeleteSuccess implements DeleteResponse {} | ||
|
||
/// Indicates that an error occurred during the delete cache item 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 DeleteError extends ErrorResponseBase implements DeleteResponse { | ||
DeleteError(super.exception); | ||
} |
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.
this is an issue captured in #36