Skip to content

Commit

Permalink
Add a cheat sheet example and readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
nand4011 committed Jan 16, 2024
1 parent fb53346 commit 0526827
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 4 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }}
Expand Down
15 changes: 15 additions & 0 deletions examples/README.md
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
```
12 changes: 12 additions & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ kotlin {
jvmToolchain(8)
}

tasks.register<JavaExec>("basic") {
description = "Run the basic example"
classpath = sourceSets["main"].runtimeClasspath
mainClass = "software.momento.example.BasicExampleKt"
}

tasks.register<JavaExec>("docExamples") {
description = "Run the doc examples"
classpath = sourceSets["main"].runtimeClasspath
Expand All @@ -29,3 +35,9 @@ tasks.register<JavaExec>("readmeExample") {
classpath = sourceSets["main"].runtimeClasspath
mainClass = "software.momento.example.doc_examples.ReadmeExampleKt"
}

tasks.register<JavaExec>("cheatSheetExample") {
description = "Run the cheat sheet example"
classpath = sourceSets["main"].runtimeClasspath
mainClass = "software.momento.example.doc_examples.CheatSheetKt"
}
72 changes: 72 additions & 0 deletions examples/src/main/kotlin/software/momento/example/BasicExample.kt
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("******************************************************************")
}
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 ->
//...
}
}

0 comments on commit 0526827

Please sign in to comment.