-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a cheat sheet example and readme.
- Loading branch information
Showing
5 changed files
with
119 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,19 +93,17 @@ jobs: | |
java-version: 17 | ||
distribution: 'corretto' | ||
|
||
- name: Build examples | ||
- name: Set up gradle | ||
uses: gradle/[email protected] | ||
continue-on-error: true | ||
with: | ||
build-root-directory: ./examples | ||
arguments: clean build | ||
|
||
- name: Run doc examples | ||
uses: gradle/[email protected] | ||
id: validation | ||
run: ./gradlew docExamples readmeExample cheatSheetExample basic | ||
with: | ||
build-root-directory: ./examples | ||
arguments: docExamples readmeExample | ||
|
||
- name: Send CI failure mail | ||
if: ${{ steps.validation.outcome == 'failure' }} | ||
|
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,15 @@ | ||
# Kotlin Client SDK | ||
|
||
<br> | ||
|
||
## Running the Examples | ||
|
||
- You do not need gradle to be installed | ||
- JDK 11 or above is required to run the example | ||
- To get started with Momento you will need a Momento API key. You can get one from the | ||
[Momento Console](https://console.gomomento.com). | ||
|
||
### Basic | ||
```bash | ||
MOMENTO_API_KEY=<YOUR API KEY> ./gradlew basic | ||
``` |
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
72 changes: 72 additions & 0 deletions
72
examples/src/main/kotlin/software/momento/example/BasicExample.kt
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,72 @@ | ||
package software.momento.example | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import software.momento.kotlin.sdk.CacheClient | ||
import software.momento.kotlin.sdk.auth.CredentialProvider | ||
import software.momento.kotlin.sdk.config.Configurations | ||
import software.momento.kotlin.sdk.responses.cache.GetResponse | ||
import software.momento.kotlin.sdk.responses.cache.control.CacheCreateResponse | ||
import software.momento.kotlin.sdk.responses.cache.control.CacheListResponse | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
|
||
private val cacheName = "cache" | ||
private val key = "key" | ||
private val value = "value" | ||
|
||
fun main() = runBlocking { | ||
printStartBanner() | ||
|
||
CacheClient( | ||
CredentialProvider.fromEnvVar("MOMENTO_API_KEY"), Configurations.Laptop.latest, 60.seconds | ||
).use { client -> | ||
println("Creating cache '$cacheName'") | ||
when (val response = client.createCache(cacheName)) { | ||
is CacheCreateResponse.Success -> println("Cache '$cacheName' created") | ||
is CacheCreateResponse.AlreadyExists -> println("Cache '$cacheName' already exists") | ||
is CacheCreateResponse.Error -> { | ||
println("An error occurred while attempting to create cache 'test-cache': ${response.errorCode}") | ||
println(response.message) | ||
} | ||
} | ||
|
||
println("Listing caches:") | ||
when (val response: CacheListResponse = client.listCaches()) { | ||
is CacheListResponse.Success -> { | ||
val caches: String = response.caches.joinToString("\n") { cacheInfo -> cacheInfo.name } | ||
println(caches) | ||
} | ||
|
||
is CacheListResponse.Error -> { | ||
println("An error occurred while attempting to list caches: ${response.errorCode}") | ||
println(response.message) | ||
} | ||
} | ||
|
||
println("setting key '$key', value '$value'") | ||
client.set(cacheName, key, value) | ||
|
||
println("getting value for key '$key'") | ||
when (val response = client.get(cacheName, key)) { | ||
is GetResponse.Hit -> println("Hit: ${response.value}") | ||
is GetResponse.Miss -> println("Miss") | ||
is GetResponse.Error -> { | ||
println("An error occurred while attempting to get '$key': ${response.errorCode}") | ||
println(response.message) | ||
} | ||
} | ||
} | ||
printEndBanner() | ||
} | ||
|
||
private fun printStartBanner() { | ||
println("******************************************************************") | ||
println("Basic Example Start") | ||
println("******************************************************************") | ||
} | ||
|
||
private fun printEndBanner() { | ||
println("******************************************************************") | ||
println("Basic Example End") | ||
println("******************************************************************") | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/src/main/kotlin/software/momento/example/doc_examples/CheatSheet.kt
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,18 @@ | ||
package software.momento.example.doc_examples | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import software.momento.kotlin.sdk.CacheClient | ||
import software.momento.kotlin.sdk.auth.CredentialProvider | ||
import software.momento.kotlin.sdk.config.Configurations | ||
import software.momento.kotlin.sdk.responses.cache.GetResponse | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
fun main() = runBlocking { | ||
CacheClient( | ||
CredentialProvider.fromEnvVar("MOMENTO_API_KEY"), | ||
Configurations.Laptop.latest, | ||
60.seconds | ||
).use { client -> | ||
//... | ||
} | ||
} |