-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add disposable-token example for docs
- Loading branch information
1 parent
9a5a271
commit bd5fef2
Showing
2 changed files
with
43 additions
and
0 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
33 changes: 33 additions & 0 deletions
33
examples/token/src/main/java/momento/client/example/docs_examples/DocExamplesJavaAPIs.java
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,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); | ||
} | ||
} | ||
} |