-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc_example_apis.dart
More file actions
208 lines (178 loc) · 6.76 KB
/
doc_example_apis.dart
File metadata and controls
208 lines (178 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import 'dart:async';
import 'dart:io';
import 'package:momento/momento.dart';
import 'package:uuid/uuid.dart';
String retrieveApiKeyFromYourSecretsManager() {
// this is not a valid API key but conforms to the syntax requirements.
return "eyJhcGlfa2V5IjogImV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwYzNNaU9pSlBibXhwYm1VZ1NsZFVJRUoxYVd4a1pYSWlMQ0pwWVhRaU9qRTJOemd6TURVNE1USXNJbVY0Y0NJNk5EZzJOVFV4TlRReE1pd2lZWFZrSWpvaUlpd2ljM1ZpSWpvaWFuSnZZMnRsZEVCbGVHRnRjR3hsTG1OdmJTSjkuOEl5OHE4NExzci1EM1lDb19IUDRkLXhqSGRUOFVDSXV2QVljeGhGTXl6OCIsICJlbmRwb2ludCI6ICJ0ZXN0Lm1vbWVudG9ocS5jb20ifQo=";
}
String retrieveApiKeyV2FromYourSecretsManager() {
// this is not a valid API key but conforms to the syntax requirements.
return "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJ0IjoiZyIsImp0aSI6InNvbWUtaWQifQ.GMr9nA6HE0ttB6llXct_2Sg5-fOKGFbJCdACZFgNbN1fhT6OPg_hVc8ThGzBrWC_RlsBpLA1nzqK3SOJDXYxAw";
}
void example_API_CredentialProviderFromApiKeyV2() {
final apiKey = retrieveApiKeyV2FromYourSecretsManager();
final endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com";
CredentialProvider.fromApiKeyV2(apiKey, endpoint);
}
void example_API_CredentialProviderFromDisposableToken() {
final authToken = retrieveApiKeyFromYourSecretsManager();
CredentialProvider.fromDisposableToken(authToken);
}
void example_API_CredentialProviderFromEnvVarV2() {
CredentialProvider.fromEnvironmentVariablesV2(
apiKeyEnvVar: "MOMENTO_API_KEY", endpointEnvVar: "MOMENTO_ENDPOINT");
}
void example_API_CredentialProviderFromEnvVarV2Default() {
CredentialProvider.fromEnvironmentVariablesV2();
}
Future<void> example_API_InstantiateCacheClient() async {
try {
final cacheClient = CacheClient(
CredentialProvider.fromEnvironmentVariablesV2(),
CacheClientConfigurations.latest(),
Duration(seconds: 30));
} catch (e) {
print("Unable to create cache client: $e");
exit(1);
}
}
Future<void> example_API_CreateCache(CacheClient cacheClient) async {
final result = await cacheClient.createCache("test-cache");
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) async {
final result = await cacheClient.deleteCache("test-cache");
switch (result) {
case DeleteCacheError():
print("Error deleting cache: $result");
exit(1);
case DeleteCacheSuccess():
print("Successfully deleted cache");
}
}
Future<void> example_API_Set(CacheClient cacheClient) async {
final result = await cacheClient.set("test-cache", "test-key", "test-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) async {
final result = await cacheClient.get("test-cache", "test-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) async {
final result = await cacheClient.delete("test-cache", "test-key");
switch (result) {
case DeleteError():
print("Error deleting key: $result");
exit(1);
case DeleteSuccess():
print("Successfully deleted key from cache");
}
}
Future<void> example_API_InstantiateTopicClient() async {
try {
final topicClient = TopicClient(
CredentialProvider.fromEnvironmentVariablesV2(),
TopicClientConfigurations.latest());
} catch (e) {
print("Unable to create topic 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 {
example_API_CredentialProviderFromApiKeyV2();
example_API_CredentialProviderFromDisposableToken();
example_API_CredentialProviderFromEnvVarV2();
example_API_CredentialProviderFromEnvVarV2Default();
final cacheClient = CacheClient(
CredentialProvider.fromEnvironmentVariablesV2(),
CacheClientConfigurations.latest(),
Duration(seconds: 30));
final topicClient = TopicClient(
CredentialProvider.fromEnvironmentVariablesV2(),
TopicClientConfigurations.latest());
final cacheName = "doc-example-apis-${Uuid().v4()}";
final key = "myKey";
final value = "myValue";
await example_API_InstantiateCacheClient();
await example_API_CreateCache(cacheClient);
await example_API_ListCaches(cacheClient);
await example_API_Set(cacheClient);
await example_API_Get(cacheClient);
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();
}