-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocExamples.kt
More file actions
260 lines (225 loc) · 10.3 KB
/
DocExamples.kt
File metadata and controls
260 lines (225 loc) · 10.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package software.momento.example.doc_examples
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeoutOrNull
import software.momento.kotlin.sdk.CacheClient
import software.momento.kotlin.sdk.TopicClient
import software.momento.kotlin.sdk.auth.CredentialProvider
import software.momento.kotlin.sdk.config.Configurations
import software.momento.kotlin.sdk.config.TopicConfigurations
import software.momento.kotlin.sdk.responses.cache.DeleteResponse
import software.momento.kotlin.sdk.responses.cache.GetResponse
import software.momento.kotlin.sdk.responses.cache.SetResponse
import software.momento.kotlin.sdk.responses.cache.control.CacheCreateResponse
import software.momento.kotlin.sdk.responses.cache.control.CacheDeleteResponse
import software.momento.kotlin.sdk.responses.cache.control.CacheListResponse
import software.momento.kotlin.sdk.responses.topic.TopicMessage
import software.momento.kotlin.sdk.responses.topic.TopicPublishResponse
import software.momento.kotlin.sdk.responses.topic.TopicSubscribeResponse
import kotlin.time.Duration.Companion.seconds
const val FAKE_V1_API_KEY =
"eyJhcGlfa2V5IjogImV5SjBlWEFpT2lKS1YxUWlMQ0poYkdjaU9pSklVekkxTmlKOS5leUpwYzNNaU9pSlBibXhwYm1VZ1NsZFVJRUoxYVd4a1pY" +
"SWlMQ0pwWVhRaU9qRTJOemd6TURVNE1USXNJbVY0Y0NJNk5EZzJOVFV4TlRReE1pd2lZWFZrSWpvaUlpd2ljM1ZpSWpvaWFuSnZZMnRs" +
"ZEVCbGVHRnRjR3hsTG1OdmJTSjkuOEl5OHE4NExzci1EM1lDb19IUDRkLXhqSGRUOFVDSXV2QVljeGhGTXl6OCIsICJlbmRwb2ludCI6" +
"ICJ0ZXN0Lm1vbWVudG9ocS5jb20ifQo="
const val FAKE_V2_API_KEY = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJ0IjoiZyIsImp0aSI6InNvbWUtaWQifQ.GMr9nA6HE0ttB6llXct_2Sg5-fOKGFbJCdACZFgNbN1fhT6OPg_hVc8ThGzBrWC_RlsBpLA1nzqK3SOJDXYxAw"
suspend fun retrieveAuthTokenFromYourSecretsManager(): String {
return FAKE_V1_API_KEY
}
suspend fun retrieveApiKeyV2FromYourSecretsManager(): String {
return FAKE_V2_API_KEY
}
suspend fun example_API_CredentialProviderFromEnvVarV2() {
CredentialProvider.fromEnvVarV2("MOMENTO_API_KEY", "MOMENTO_ENDPOINT")
}
suspend fun example_API_CredentialProviderFromEnvVarV2Default() {
CredentialProvider.fromEnvVarV2()
}
suspend fun example_API_CredentialProviderFromApiKeyV2() {
val apiKey = retrieveApiKeyV2FromYourSecretsManager()
val endpoint = "cell-4-us-west-2-1.prod.a.momentohq.com"
CredentialProvider.fromApiKeyV2(apiKey, endpoint)
}
suspend fun example_API_CredentialProviderFromDisposableToken() {
val authToken = retrieveAuthTokenFromYourSecretsManager()
CredentialProvider.fromDisposableToken(authToken)
}
suspend fun example_API_CredentialProviderFromEnvVar() {
CredentialProvider.fromEnvVar("V1_API_KEY")
}
suspend fun example_API_CredentialProviderFromString() {
val authToken = retrieveAuthTokenFromYourSecretsManager()
CredentialProvider.fromString(authToken)
}
suspend fun example_API_ConfigurationLaptop() {
Configurations.Laptop.latest
}
suspend fun example_API_ConfigurationInRegionLatest() {
Configurations.InRegion.latest
}
suspend fun example_API_ConfigurationLowLatency() {
Configurations.InRegion.LowLatency.latest
}
suspend fun example_API_InstantiateCacheClient() {
CacheClient(
CredentialProvider.fromEnvVarV2(), Configurations.Laptop.latest, 60.seconds
).use { cacheClient ->
//...
}
}
suspend fun example_API_ErrorHandlingHitMiss(cacheClient: CacheClient) {
when (val response = cacheClient.get("test-cache", "test-key")) {
is GetResponse.Hit -> println("Retrieved value for key 'test-key': ${response.value}")
is GetResponse.Miss -> println("Key 'test-key' was not found in cache 'test-cache'")
is GetResponse.Error -> throw RuntimeException(
"An error occurred while attempting to get key 'test-key' from cache 'test-cache': ${response.errorCode}",
response
)
}
}
suspend fun example_API_ErrorHandlingSuccess(cacheClient: CacheClient) {
when (val response = cacheClient.set("test-cache", "test-key", "test-value")) {
is SetResponse.Success -> println("Key 'test-key' stored successfully")
is SetResponse.Error -> throw RuntimeException(
"An error occurred while attempting to store key 'test-key' in cache 'test-cache': ${response.errorCode}",
response
)
}
}
suspend fun example_API_CreateCache(cacheClient: CacheClient) {
when (val response = cacheClient.createCache("test-cache")) {
is CacheCreateResponse.Success -> println("Cache 'test-cache' created")
is CacheCreateResponse.AlreadyExists -> println("Cache 'test-cache' already exists")
is CacheCreateResponse.Error -> throw RuntimeException(
"An error occurred while attempting to create cache 'test-cache': ${response.errorCode}", response
)
}
}
suspend fun example_API_DeleteCache(cacheClient: CacheClient) {
when (val response = cacheClient.deleteCache("test-cache")) {
is CacheDeleteResponse.Success -> println("Cache 'test-cache' deleted")
is CacheDeleteResponse.Error -> throw RuntimeException(
"An error occurred while attempting to delete cache 'test-cache': ${response.errorCode}", response
)
}
}
suspend fun example_API_ListCaches(cacheClient: CacheClient) {
when (val response: CacheListResponse = cacheClient.listCaches()) {
is CacheListResponse.Success -> {
val caches: String = response.caches.joinToString("\n") { cacheInfo -> cacheInfo.name }
println("Caches:\n$caches")
}
is CacheListResponse.Error -> throw RuntimeException(
"An error occurred while attempting to list caches: ${response.errorCode}", response
)
}
}
suspend fun example_API_Set(cacheClient: CacheClient) {
when (val response = cacheClient.set("test-cache", "test-key", "test-value")) {
is SetResponse.Success -> println("Key 'test-key' stored successfully")
is SetResponse.Error -> throw RuntimeException(
"An error occurred while attempting to store key 'test-key' in cache 'test-cache': ${response.errorCode}",
response
)
}
}
suspend fun example_API_Get(cacheClient: CacheClient) {
when (val response = cacheClient.get("test-cache", "test-key")) {
is GetResponse.Hit -> println("Retrieved value for key 'test-key': ${response.value}")
is GetResponse.Miss -> println("Key 'test-key' was not found in cache 'test-cache'")
is GetResponse.Error -> throw RuntimeException(
"An error occurred while attempting to get key 'test-key' from cache 'test-cache': ${response.errorCode}",
response
)
}
}
suspend fun example_API_Delete(cacheClient: CacheClient) {
when (val response = cacheClient.delete("test-cache", "test-key")) {
is DeleteResponse.Success -> println("Key 'test-key' deleted successfully")
is DeleteResponse.Error -> throw RuntimeException(
"An error occurred while attempting to delete key 'test-key' from cache 'test-cache': ${response.errorCode}",
response
)
}
}
suspend fun example_API_InstantiateTopicClient() {
TopicClient(
CredentialProvider.fromEnvVarV2(), TopicConfigurations.Laptop.latest
).use { topicClient ->
//...
}
}
suspend fun example_API_TopicSubscribe(topicClient: TopicClient) {
when (val response = topicClient.subscribe("test-cache", "test-topic")) {
is TopicSubscribeResponse.Subscription -> coroutineScope {
launch {
withTimeoutOrNull(2000) {
response.collect { item ->
when (item) {
is TopicMessage.Text -> println("Received text message: ${item.value}")
is TopicMessage.Binary -> println("Received binary message: ${item.value}")
is TopicMessage.Error -> throw RuntimeException(
"An error occurred reading messages from topic 'test-topic': ${item.errorCode}", item
)
}
}
}
}
}
is TopicSubscribeResponse.Error -> throw RuntimeException(
"An error occurred while attempting to subscribe to topic 'test-topic': ${response.errorCode}", response
)
}
}
suspend fun example_API_TopicPublish(topicClient: TopicClient) {
when (val response = topicClient.publish("test-cache", "test-topic", "test-message")) {
is TopicPublishResponse.Success -> println("Message published successfully")
is TopicPublishResponse.Error -> throw RuntimeException(
"An error occurred while attempting to publish message to topic 'test-topic': ${response.errorCode}",
response
)
}
}
fun main() = runBlocking {
example_API_CredentialProviderFromEnvVar()
example_API_CredentialProviderFromString()
example_API_CredentialProviderFromEnvVarV2Default()
example_API_CredentialProviderFromEnvVarV2()
example_API_CredentialProviderFromApiKeyV2()
example_API_CredentialProviderFromDisposableToken()
example_API_ConfigurationLaptop()
example_API_ConfigurationInRegionLatest()
example_API_ConfigurationLowLatency()
example_API_InstantiateCacheClient()
CacheClient(
CredentialProvider.fromEnvVarV2(), Configurations.Laptop.latest, 60.seconds
).use { cacheClient ->
try {
example_API_ErrorHandlingHitMiss(cacheClient)
} catch (e: Exception) {
println("Hit/Miss error handling succeeded")
println(e.message)
}
try {
example_API_ErrorHandlingSuccess(cacheClient)
} catch (e: Exception) {
println("Success error handling succeeded")
println(e.message)
}
example_API_CreateCache(cacheClient)
example_API_DeleteCache(cacheClient)
example_API_CreateCache(cacheClient)
example_API_ListCaches(cacheClient)
example_API_Set(cacheClient)
example_API_Get(cacheClient)
example_API_Delete(cacheClient)
}
example_API_InstantiateTopicClient()
TopicClient(
CredentialProvider.fromEnvVarV2(), TopicConfigurations.Laptop.latest
).use { topicClient ->
example_API_TopicSubscribe(topicClient)
example_API_TopicPublish(topicClient)
}
}