Skip to content

Commit

Permalink
chore: add TopicClient examples for docs (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgautier404 committed Jan 12, 2024
1 parent 07b3c1f commit e882684
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions example/doc_example_apis/doc_example_apis.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'package:momento/momento.dart';
import 'package:uuid/uuid.dart';
Expand Down Expand Up @@ -81,12 +82,66 @@ Future<void> example_API_Delete(CacheClient cacheClient) async {
}
}

Future<void> example_API_InstantiateTopicClient() async {
try {
final topicClient = TopicClient(
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"),
TopicClientConfigurations.latest());
} catch (e) {
print("Unable to create cache client: $e");
exit(1);
}
}

Future<void> example_API_TopicSubscribe(TopicClient topicClient) async {
final subscription = await topicClient.subscribe("test-cache", "test-topic");
final messageStream = switch (subscription) {
TopicSubscription() => subscription.stream,
TopicSubscribeError() => throw Exception(
"Subscribe error: ${subscription.errorCode} ${subscription.message}"),
};

// cancel subscription 5 seconds from now
Timer(const Duration(seconds: 5), () {
print("Cancelling subscription!");
subscription.unsubscribe();
});

try {
await for (final msg in messageStream) {
switch (msg) {
case TopicSubscriptionItemBinary():
print("Binary value: ${msg.value}");
case TopicSubscriptionItemText():
print("String value: ${msg.value}");
}
}
} catch (e) {
print("Runtime type: ${e.runtimeType}");
print("Error with await for loop: $e");
}
}

Future<void> example_API_TopicPublish(TopicClient topicClient) async {
final result = await topicClient.publish("cache", "topic", "hello message!");
switch (result) {
case TopicPublishSuccess():
print("Successful publish!");
case TopicPublishError():
print("Publish error: ${result.errorCode} ${result.message}");
}
}

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

final topicClient = TopicClient(
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"),
TopicClientConfigurations.latest());

final cacheName = "doc-example-apis-${Uuid().v4()}";
final key = "myKey";
final value = "myValue";
Expand All @@ -100,5 +155,19 @@ Future<void> main() async {
await example_API_Delete(cacheClient);

await example_API_DeleteCache(cacheClient);

await example_API_InstantiateTopicClient();

// the following topics tests need "test-cache" created
await example_API_CreateCache(cacheClient);

await example_API_TopicSubscribe(topicClient);
await example_API_TopicPublish(topicClient);

// clean up cache used for topic methods
await example_API_DeleteCache(cacheClient);

// close the clients to clean up their resources and allow the program to exit
await cacheClient.close();
topicClient.close();
}

0 comments on commit e882684

Please sign in to comment.