Skip to content

Commit

Permalink
feat: add disposable-token example for docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rishtigupta committed Dec 21, 2023
1 parent 9a5a271 commit bd5fef2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/token/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ task("disposableToken", JavaExec::class) {
mainClass.set("momento.client.example.DisposableTokenExample")
}

task("docExamples", JavaExec::class) {
description = "Validate that the API doc examples run"
classpath = sourceSets.main.get().runtimeClasspath
mainClass.set("momento.client.example.doc_examples.DocExamplesJavaAPIs")
}

task("docsTasks") {
dependsOn("docExamples")
}

task("prepareKotlinBuildScriptModel") {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package momento.client.example.docs_examples;

import momento.sdk.AuthClient;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.auth.accessControl.DisposableTokenScopes;
import momento.sdk.auth.accessControl.ExpiresIn;
import momento.sdk.responses.auth.GenerateDisposableTokenResponse;

public class DocExamplesJavaAPIs {

public static void example_API_GenerateDisposableToken(AuthClient authClient) {
final GenerateDisposableTokenResponse response =
authClient
.generateDisposableTokenAsync(
DisposableTokenScopes.cacheKeyReadWrite("squirrel", "mo"), ExpiresIn.minutes(30))
.join();
if (response instanceof GenerateDisposableTokenResponse.Success success) {
System.out.println("Successfully generated the disposable token: " + success.authToken());
} else if (response instanceof GenerateDisposableTokenResponse.Error error) {
throw new RuntimeException(
"An error occurred while attempting to generate disposable token: "
+ error.getErrorCode(),
error);
}
}

public static void main(String[] args) {
try (final AuthClient authClient =
AuthClient.builder(CredentialProvider.fromEnvVar("MOMENTO_API_KEY")).build()) {
example_API_GenerateDisposableToken(authClient);
}
}
}

0 comments on commit bd5fef2

Please sign in to comment.