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

chore: add doc api examples #91

Merged
merged 6 commits into from
Jan 10, 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
12 changes: 10 additions & 2 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ jobs:
run: dart test
env:
TEST_API_KEY: ${{ secrets.ALPHA_TEST_AUTH_TOKEN }}


- name: Run examples
env:
MOMENTO_API_KEY: ${{ secrets.ALPHA_TEST_AUTH_TOKEN }}
run: |
pushd example/doc_example_apis
dart run doc_example_apis.dart
popd

readme:
runs-on: ubuntu-latest
steps:
Expand All @@ -50,4 +58,4 @@ jobs:
sdk_language: Dart
template_file: ./README.template.md
output_file: ./README.md
dev_docs_slug: dart
dev_docs_slug: dart
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ analyzer:
exclude:
- lib/generated/**
- example/flutter_chat_app/**
- example/doc_example_apis/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
Expand Down
109 changes: 109 additions & 0 deletions example/doc_example_apis/doc_example_apis.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'dart:io';
import 'package:momento/momento.dart';
import 'package:uuid/uuid.dart';

Future<void> example_API_InstantiateCacheClient() async {
try {
final cacheClient = CacheClient(
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"),
CacheClientConfigurations.latest(),
Duration(seconds: 30));
} catch (e) {
print("Unable to create cache client: $e");
exit(1);
}
}

Future<void> example_API_CreateCache(
CacheClient cacheClient, String cacheName) async {
final result = await cacheClient.createCache(cacheName);
switch (result) {
case CreateCacheAlreadyExists():
print("Cache already exists");
case CreateCacheError():
print("Error creating cache: $result");
case CreateCacheSuccess():
print("Successfully created the cache");
}
}

Future<void> example_API_ListCaches(CacheClient cacheClient) async {
final result = await cacheClient.listCaches();
switch (result) {
case ListCachesError():
print("Error listing caches: $result");
case ListCachesSuccess():
print("Successfully listed caches: ${result.cacheNames}");
}
}

Future<void> example_API_DeleteCache(
CacheClient cacheClient, String cacheName) async {
final result = await cacheClient.deleteCache(cacheName);
switch (result) {
case DeleteCacheError():
print("Error deleting cache: $result");
exit(1);
case DeleteCacheSuccess():
print("Successfully deleted cache");
}
}

Future<void> example_API_Set(
CacheClient cacheClient, String cacheName, Value key, Value value) async {
final result = await cacheClient.set(cacheName, key, value);
switch (result) {
case SetError():
print("Error setting key: $result");
exit(1);
case SetSuccess():
print("Successfully set item in the cache");
}
}

Future<void> example_API_Get(
CacheClient cacheClient, String cacheName, Value key) async {
final result = await cacheClient.get(cacheName, key);
switch (result) {
case GetMiss():
print("Key was not found in cache.");
case GetError():
print("Error getting key: $result");
case GetHit():
print("Successfully got item from cache: ${result.value}");
}
}

Future<void> example_API_Delete(
CacheClient cacheClient, String cacheName, Value key) async {
final result = await cacheClient.delete(cacheName, key);
switch (result) {
case DeleteError():
print("Error deleting key: $result");
exit(1);
case DeleteSuccess():
print("Successfully deleted key from cache");
}
}

Future<void> main() async {
final cacheClient = CacheClient(
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"),
CacheClientConfigurations.latest(),
Duration(seconds: 30));

final cacheName = "doc-example-apis-${Uuid().v4()}";
final key = StringValue("myKey");
final value = StringValue("myValue");

await example_API_InstantiateCacheClient();
await example_API_CreateCache(cacheClient, cacheName);
await example_API_ListCaches(cacheClient);

await example_API_Set(cacheClient, cacheName, key, value);
await example_API_Get(cacheClient, cacheName, key);
await example_API_Delete(cacheClient, cacheName, key);

await example_API_DeleteCache(cacheClient, cacheName);
await cacheClient.close();
}
2 changes: 1 addition & 1 deletion lib/src/internal/control_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ControlClient implements AbstractControlClient {
return CreateCacheSuccess();
} catch (e) {
if (e is GrpcError && e.code == StatusCode.alreadyExists) {
return AlreadyExists();
return CreateCacheAlreadyExists();
} else if (e is GrpcError) {
return CreateCacheError(grpcStatusToSdkException(e));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:momento/src/messages/responses/responses_base.dart';
/// switch (response) {
/// case CreateCacheSuccess():
/// // handle success
/// case AlreadyExists():
/// case CreateCacheAlreadyExists():
/// // handle already exists
/// case CreateCacheError():
/// // handle error
Expand All @@ -19,7 +19,7 @@ sealed class CreateCacheResponse {}
class CreateCacheSuccess implements CreateCacheResponse {}

/// Indicates that the cache already exists, so there was nothing to do.
class AlreadyExists implements CreateCacheResponse {}
class CreateCacheAlreadyExists implements CreateCacheResponse {}

/// Indicates that an error occurred during the create cache request.
///
Expand Down
4 changes: 2 additions & 2 deletions test/src/cache/cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
switch (createResp) {
case CreateCacheSuccess():
fail('Expected Error but got Success');
case AlreadyExists():
case CreateCacheAlreadyExists():
fail('Expected Error but got AlreadyExists');
case CreateCacheError():
expect(createResp.errorCode, MomentoErrorCode.invalidArgumentError,
Expand All @@ -51,7 +51,7 @@ void main() {
case CreateCacheSuccess():
expect(createResp.runtimeType, CreateCacheSuccess,
reason: "create cache should succeed");
case AlreadyExists():
case CreateCacheAlreadyExists():
fail('Expected Success but got AlreadyExists');
case CreateCacheError():
fail(
Expand Down